MySQL dynamic link to fetch the right row

前端 未结 2 1055
别跟我提以往
别跟我提以往 2021-01-23 23:03

I have a MySQL-database with x rows. Now I want to interpret a link, fetch the right row and print this values.

E.g. Link: .html?row=3 -> Opens site which fetch row 3

2条回答
  •  轮回少年
    2021-01-23 23:29

    mysql/data_access.php

     PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_EMULATE_PREPARES => false,
        PDO::ATTR_PERSISTENT => true,
    )) {
        $dsn = getDsn($host, $dbname, $port, $charset);
        $connection = new PDO($dsn, $username, $password);
        foreach ($options as $key => $value) {
            $connection->setAttribute($key, $value);
        }
        return $connection;
    }
    
    /**
     * Create a mysql DSN string.
     * 
     * @param string $host Host.
     * @param string $dbname Database name.
     * @param string $port [optional] Port.
     * @param array $charset [optional] Character set.
     * @return string DSN string.
     */
    function getDsn($host, $dbname, $port = '3306', $charset = 'utf8') {
        $dsn = sprintf('mysql:host=%s;port=%s;dbname=%s;charset=%s'
                , $host
                , $port
                , $dbname
                , $charset
        );
        return $dsn;
    }
    
    /**
     * Close a db connection.
     * 
     * @param PDO $connection Db connection.
     * @return void
     */
    function closeConnection($connection) {
        $connection = NULL;
    }
    
    /**
     * Get the data type of a binding value.
     * 
     * @param mixed $value Binding value.
     * @return mixed Data type of the binding value.
     */
    function getInputParameterDataType($value) {
        $dataType = PDO::PARAM_STR;
        if (is_int($value)) {
            $dataType = PDO::PARAM_INT;
        } elseif (is_bool($value)) {
            $dataType = PDO::PARAM_BOOL;
        }
        return $dataType;
    }
    

    error_reporting.php


    print.php

    ' . print_r($data, true) . '
    '; } else { echo $data; } }

    deli_list.php

    prepare($sql);
            if (!$statement) {
                throw new Exception('The SQL statement can not be prepared!');
            }
    
            // Bind values to sql statement parameters.
            $statement->bindValue(':id', $id, getInputParameterDataType($id));
    
            // Execute and check PDO statement.
            if (!$statement->execute()) {
                throw new Exception('The PDO statement can not be executed!');
            }
    
            // Fetch person details.
            $fetchedData = $statement->fetchAll(PDO::FETCH_ASSOC);
    
            closeConnection($connection);
        } catch (PDOException $pdoException) {
            printData($pdoException->getMessage());
            exit();
        } catch (Exception $exception) {
            printData($exception->getMessage());
            exit();
        }
    }
    ?>
    
    
    
        
            
            App title
        
        
    
            
            
    • - -

提交回复
热议问题