MySQL动态链接获取正确的行 [英] MySQL dynamic link to fetch the right row

查看:43
本文介绍了MySQL动态链接获取正确的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有x行的MySQL数据库. 现在,我想解释一个链接,获取右行并打印此值.

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

例如链接:.html?row = 3->打开获取第3行并打印它们的站点.

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

.html?row = 1000->打开获取第1000行并进行打印的网站

.html?row=1000 -> Opens site which fetch row 1000 and print them

我不知道如何连接此动态链接和数据库. 我只有在手动选择行时才能打印数据库.

I don't know how to connect this dynamic link and the database. I only can print the database when I chose the row manually.

<?php
require_once ('../mysql/config.php');
$db_link = mysqli_connect (
                 MYSQL_HOST, 
                 MYSQL_BENUTZER, 
                 MYSQL_KENNWORT, 
                 MYSQL_DATENBANK
                );
mysqli_set_charset($db_link, 'utf8');
if (!$db_link) {
die('keine Verbindung möglich: ' . mysql_error());
} 

$sql = "SELECT * FROM Tabelle1";
$db_erg = mysqli_query( $db_link, $sql );
?>
if ( ! $db_erg )
      {
        die('Ungültige Abfrage: ' . mysqli_error());
      }
      while ($row = mysqli_fetch_array( $db_erg, MYSQL_ASSOC))
      {
        echo $row['B'];
        echo $row['E'];
      }
      mysqli_free_result( $db_erg );
      ?>

推荐答案

mysql/data_access.php

<?php

/*
 * ---------------------
 * Data access functions
 * ---------------------
 */

/**
 * Create a new db connection.
 * 
 * @param string $host Host.
 * @param string $dbname Database name.
 * @param string $username Username.
 * @param string $password Password.
 * @param string $port [optional] Port.
 * @param array $charset [optional] Character set.
 * @param array $options [optional] Driver options.
 * @return PDO Db connection.
 */
function createConnection($host, $dbname, $username, $password, $port = '3306', $charset = 'utf8', $options = array(
    PDO::ATTR_ERRMODE => 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


error_reporting.php

<?php

/*
 * -------------------------
 * Error reporting functions
 * -------------------------
 */

/**
 * Toggle error reporting.
 * 
 * @param integer $level Error level.
 * @param bool $display_errors Display errors if TRUE, hide them otherwise.
 * @return void
 */
function acivateErrorReporting($level = E_ALL, $display_errors = 1) {
    error_reporting($level);
    ini_set('display_errors', $display_errors);
}


print.php


print.php

<?php

/**
 * Print data on screen.
 * 
 * @param mixed $data Data to print.
 * @param bool $preformatted Print preformatted if TRUE, print normal otherwise.
 * @return void
 */
function printData($data, $preformatted = FALSE) {
    if ($preformatted) {
        echo '<pre>' . print_r($data, true) . '</pre>';
    } else {
        echo $data;
    }
}


deli_list.php


deli_list.php

<?php
require_once ('../mysql/config.php');
require_once ('../mysql/data_access.php');
require_once ('../mysql/print.php');

// Validate the 'id' value from the query string
if (!isset($_GET['id']) || empty($_GET['id']) || $_GET['id'] == 0) {
    echo 'Please provide a valid "id" value!';
} else {
    try {
        // Read the 'id' value from the query string.
        $id = $_GET['id'];

        // Create db connection.
        $connection = createConnection(
                MYSQL_HOST, MYSQL_DATENBANK, MYSQL_BENUTZER
                , MYSQL_KENNWORT, MYSQL_PORT, MYSQL_CHARSET
        );

        // Define sql statement.
        $sql = 'SELECT * FROM Tabelle1 WHERE id = :id';

        // Prepare and check sql statement (returns PDO statement).
        $statement = $connection->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();
    }
}
?>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>App title</title>
    </head>
    <body>

        <!-- Accordion example -->
        <ul class="accordion">
            <?php
            foreach ($fetchedData as $record) {
                ?>
                <li>
                    <?php echo $record['A'] ?> - <?php echo $record['B'] ?> - <?php echo $record['C'] ?>
                </li>
                <?php
            }
            ?>
        </ul>

        <!-- Table example -->
        <table>
            <?php
            foreach ($fetchedData as $record) {
                ?>
                <tr>
                    <td>
                        <?php echo $record['A'] ?>
                    </td>
                    <td>
                        <?php echo $record['B'] ?>
                    </td>
                    <td>
                        <?php echo $record['C'] ?>
                    </td>
                </tr>
                <?php
            }
            ?>
        </table>

    </body>
</html>

这篇关于MySQL动态链接获取正确的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆