gpt4 book ai didi

php - SQL查询无法从数据库接收正确的值?

转载 作者:行者123 更新时间:2023-11-29 22:54:12 24 4
gpt4 key购买 nike

解决方案:我问这个问题时对 PHP 和 SQL 知之甚少,我的答案的解决方案是将变量像 '$PLATE_NUMBER' 一样包装起来,因为它是一个字符串。这也在下面的第一条评论中得到了回答。

正如你从代码中看到的,当我点击搜索时,我收到“没有搜索结果!”我的查询有问题,因为如果我将其更改为

"SELECT * FROM `tires` WHERE PLATE_NUMBER LIKE '%$PLATE_NUMBER%'"

然后我接收mySQL数据库里面的数据?

顺便说一句:我还在 mySQL 中测试了这两个查询,并且它有效。那么这里可能存在什么问题呢?我希望搜索实例“CV33610”,并从我的数据库接收该行..

$query = mysql_query("SELECT * FROM `tires` WHERE PLATE_NUMBER='%$PLATE_NUMBER%'", $connection) or die("Could not search!"); 
$count = mysql_num_rows($query); // Returns an integer of how many rows in your table that is picked up.

if($count == 0){

$output = 'There was no search results!';

} else {

// Collect the data into an array
while($row = mysql_fetch_array($query)){

$platenumber = $row['PLATE_NUMBER'];
$id = $row['id'];

// View the parameters to the screen.
$output .= '<div>'.$id.' '.$platenumber.'</div>';

} // End while statement
} // End if statement

最佳答案

由于我们处理的是字符串值 CV33610,因此您需要使用单引号将 $PLATE_NUMBER 变量括起来。

$query = mysql_query("SELECT * FROM `tires` 
WHERE PLATE_NUMBER = '$PLATE_NUMBER'", $connection)
or die(mysql_error());

在查询中添加 or die(mysql_error()) 将会发出语法错误信号。

旁注:

<小时/>

添加error reporting到文件顶部,这将有助于查找错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

旁注:错误报告只能在暂存阶段完成,而不能在生产阶段完成。

关于php - SQL查询无法从数据库接收正确的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28787336/

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