【问题标题】:Select Mysql Data at Specific Row在特定行选择 Mysql 数据
【发布时间】:2012-07-25 10:22:53
【问题描述】:

我正在尝试构建客户电子商务后端。我以前做过很多次这样的事情,并不认为自己对 php 和 mysql 是“新手”,但我被卡住了,不知道哪里出了问题。

我只想在特定位置显示 mysql 行的内容(使用“WHERE”命令)。

但是当我加载页面时,内容部分(在表格中)是空的。该位置的表格中肯定有内容,页面上的所有其他内容都显示,但实际的 customerResults 除外。

这是我的代码:

<head>
<title>Customer Summary</title>
<?php  
    session_start();
    require 'database_connect.php';
    $customerTable = "customer";

    if(isset($_GET['customer_click'])){
        $customerId = $_GET['customer_click']; 
    }
?>
</head>
<h3>Customer <?php echo"$customerId"?></h3>
<table align="center" width="600px">
    <tr>
        <td><a href="index.php">Summary</a></td>
        <td><a href="personal.php">Personal</a></td>
        <td><a href="billing.php">Billing</a></td>
        <td><a href="order_history.php">Order History</a></td>
    </tr>
</table>
<table align="center" width="400px">
    <tr>
        <?php 
            $customerSelect = "SELECT * FROM $customerTable WHERE id = '$customerId' ";
            $customerResult = mysql_query($customerSelect);
            if (!$customerResult){
                echo "No results, but why?!?!? </br/>";

            }
            if  (mysql_num_rows($customerResult)==0){
                echo "Results are empty...but why!?!?!";

            }

            while ($customerData = mysql_fetch_assoc($customerResult)){ 
                echo $customerData['id'];
                echo $customerData['email'];
            }

        ?>      
    </tr>
</table>

我可能忽略了一些简单的事情,但我真的想不通

【问题讨论】:

  • 对我来说足够新:)。欢迎来到堆栈溢出!请不要将mysql_* 函数用于新代码。它们不再维护,社区已经开始deprecation process。看到red box?相反,您应该了解prepared statements 并使用PDOMySQLi。如果您不能决定,this article 将帮助您选择。如果你想学习,here is good PDO tutorial.
  • 那么您遇到了什么错误?你试过什么?
  • 您确认您没有收到任何 Mysql 错误吗?当mysql_query 返回FALSE 结果时,可能意味着没有行,或者查询没有成功。
  • 运行时会发生什么? PHP 错误、SQL 错误、空结果...
  • 您确定 $customerTable 以及更重要的是 $customerId 变量设置正确吗?

标签: php mysql select where


【解决方案1】:

让我们看看:

  • 第 27 行:未定义变量 'customerSelct'
  • 第 41 行:未定义变量 'customerDdata'
  • 第 43 行:未定义变量 'result'

加上Please, don't use mysql_* functions in new code。它们不再维护,deprecation process 已开始使用。看到red box?改为了解prepared statements,并使用PDOMySQLi - this article 将帮助您决定哪个。如果你选择 PDO,here is a good tutorial

使用 PDO 的示例代码:

<?php
    try {
        session_start();
        if (!isset($_GET['customer_click'])) {
            throw new Exception('Customer ID not provided.');
        }
        //Assuming the ID must be a number.
        if (!is_numeric($_GET['customer_click'])) {
            throw new Exception('Customer ID must be numeric.');
        }
        $customerID = $_GET['customer_click'];
        $db         = new PDO("mysql:host=localhost;dbname=database_name_here", "user", "pass");
        //Have PDO to not emulate prepared statements by default.
        //Instead use MySQL's native prepare engine.
        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        //PDO will throw PDOExceptions on every error.
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $query = "SELECT * FROM `customer` WHERE `id` = :id";
        $stmt  = $db->prepare($query);
        //Bind ID as a number and not as string.
        $stmt->bindValue(":id", $customerID, PDO::PARAM_INT);
        $stmt->execute();
        //Fetch all results into $result.
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
    catch (PDOException $e) {
        //A database error has occurred!
        die("Database Error occurred! " . $e->getMessage());
    }

    catch (Exception $e) {
        //General error occurred!
        die("Error! " . $e->getMessage());
    }
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>

    <pre>
        <?php print_r($result); ?>
    </pre>

</body>
</html>

【讨论】:

  • 第 27 行是为了查看 mysql 是否正常运行。第 41 行是一个错字,我在第 34 行看不到任何结果。你所说的 msql_* 代码是什么意思?我应该使用什么?
  • @Shade:请不要将mysql_* 函数用于新代码。它们不再维护,社区已开始deprecation process。看到red box?相反,您应该了解prepared statements 并使用PDOMySQLi。如果您不能决定,this article 将帮助您选择。如果你想学习,here is good PDO tutorial.
  • @Shade:也是我的错,是 43 岁。
  • 是的,这些都是我的错别字,修复后,页面仍然无法正确加载。我将不得不查看你给我的网站,谢谢你的信息。我的mysql_* 代码会导致问题吗?
  • @Shade:可能不是,但这绝对是你头痛的原因。
猜你喜欢
  • 2020-11-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-28
  • 2021-07-09
  • 2014-11-28
  • 1970-01-01
  • 2018-04-01
  • 2022-01-26
相关资源
最近更新 更多