Php 如何使脚本从MYSQL数据库检索特定数据?
我做了一个简单的搜索,从数据库中检索客户数据。到目前为止,我只能成功地在网页上显示当前表数据,但我无法使用搜索功能检索特定数据。我有0个数据库访问问题 我已经在goggle上搜索了几个小时,尝试了许多不同的组合,但是我找不到任何关于mysqli搜索的教程 客户号为主键varchar、prog_id integer、balancevarcharPhp 如何使脚本从MYSQL数据库检索特定数据?,php,database,get,mysqli,Php,Database,Get,Mysqli,我做了一个简单的搜索,从数据库中检索客户数据。到目前为止,我只能成功地在网页上显示当前表数据,但我无法使用搜索功能检索特定数据。我有0个数据库访问问题 我已经在goggle上搜索了几个小时,尝试了许多不同的组合,但是我找不到任何关于mysqli搜索的教程 客户号为主键varchar、prog_id integer、balancevarchar **Table structure** Query Output: > SELECT prog_id, cust_no, balance
**Table structure**
Query Output:
> SELECT prog_id, cust_no, balance
FROM `tee`.`teecust`
+ ------------ + ------------ + ------------ +
| prog_id | cust_no | balance |
+ ------------ + ------------ + ------------ +
| 220852 | 1184631 | 0
|
| 220853 | 1184693 | 0
|
| 225726 | 1186292 | 0
|
| 220854 | 1233446 | 0
|
| 220856 | 1233672 | 0
<!DOCTYPE html>
<head>
<title>Search the Database</title>
</head>
<body>
<form action="search.php" method="get" enctype="application/x-www-form-urlencoded" target="_self" id="search">
Search: <input type="text" name="term" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
$host="****";
$port="3306";
$socket="";
$user="u*****";
$password="*****";
$dbname="tee";
$mysqli = new mysqli($host, $user, $password, $dbname);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT cust_no FROM teecust LIMIT 20,5";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_row()) {
printf ("Customer Number:%s Billing ID:%s Balance due:%s\n", $row["cust_no"], $row["prog_id"], $row["balance"]);
}
/* free result set */
$result->close();
}
/* close connection */
$mysqli->close();
?>
</body>
</html>
您需要通过get获取搜索条件,因为这是您指定的。虽然您在帖子中没有提到这一点,但我假设您希望在查询中使用搜索条件: 额外注意:您可能还需要阅读SQL中的 我已经向您展示了如何使用以增加安全性
<?php
$searchTerm = $_GET['term']; // GET the search term submitted by the form
$customerNumber = '';
$progID = '';
$balance = '';
// build your query - notice the user of '?' as I'm using prepared statements to query the database. Also notice the 'where' clause.
$sql = "Select cust_no, prog_id, balance From teecust where cust_no = ?";
// Create the prepared statement
if($stmt = $mysqli->prepare($sql)){
$stmt->bind_param("s", $searchTerm); // bind the search term to the query where the '?' once was
if(!$stmt->execute()){ // run the query
echo $mysqli->error;
}
$stmt->bind_result($customerNumber, $progID, $balance ); // retrieve results into variables
while($stmt->fetch()){ // loop through results
// You now have the result in $customerNumber, $progID, and $balance - do what you want with it
echo "customer number: " . $customerNumber . "<br> Program ID: " . $progID . "<br> Balance: " . $balance;
}
$stmt->close();
}
else{
echo $mysqli->error;
}
?>
mysqli没有什么问题-。。。所有偏好
当然这是一个偏好的问题。如果你喜欢臃肿、不可重用和不安全的代码,mysqli就是他们的选择。但如果他们想要别的东西,那么必须选择:
$sql = "Select cust_no, prog_id, balance From teecust where cust_no = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute(array($_GET['term']));
while($row = $stmt->fetch()) {
echo "Customer: $row[cust_no]<br>
Program ID: $row[prog_id]<br>
Balance: $row[balance]";
}
您需要一个更复杂的SQL查询,具体来说,您需要了解WHERE子句:Hello@willkinsiii!正如我在访问您提供给页面的链接时所看到的,此警告:“where子句”中的未知列“term”,因此我可以猜测您的sql是错误构造的,那么您能否告诉我此处发布的代码是否与您指向的页面中使用的代码相同?如果相同,为什么在此代码中我们既找不到where语句也找不到term单词?如果两者都不同,请告诉我解释您在此处发布的代码与您提供链接的页面之间的关系?因此,我们可以更深入地了解您的问题:我做了一些更改,但仍然一无所获:$customerNumber=$progID=$余额=$sql=选择客户编号、项目id、来自teecust的余额,其中术语“%$searchTerm.%”@我不明白这个问题。你能试着用其他的术语解释一下吗?@WillWIlkinsIII你仍然得到MYSQL错误吗?如果不是,那么表和查询都很好。请注意,在我发布的答案中,我没有显示任何信息,如果您愿意,我可以将其添加到代码中。不客气-我只是检查了您所有的问题并投了赞成票,这样您就有足够的声誉来聊天-我想这是一个完美的时机:p小心,祝您好运。@YourCommense mysqli没有问题-。。。所有偏好。另外,我认为这是学习一些基础知识的好方法。你真的试图证明mysqli编写的语句是不安全的吗?只要正确处理所有数据,使用准备好的语句就没有什么不安全的地方。