gpt4 book ai didi

php - 选择特定行的 Mysql 数据

转载 作者:行者123 更新时间:2023-11-29 04:50:56 25 4
gpt4 key购买 nike

我正在尝试构建客户电子商务后端。我以前做过很多次这样的事情,并不认为自己是 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>

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

最佳答案

让我们看看:

  • 第 27 行: undefined variable 'customerSelct'
  • 第 41 行: undefined variable 'customerDdata'
  • 第 43 行: undefined variable '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>

关于php - 选择特定行的 Mysql 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11675954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com