gpt4 book ai didi

php - 为什么这个脚本没有按预期工作?

转载 作者:太空宇宙 更新时间:2023-11-03 12:15:48 25 4
gpt4 key购买 nike

我对 PHP 和 MySQL 还很陌生,所以我想我应该先放弃它。我正在尝试制作一个 jQuery/PHP/MySQL/Json 自动完成脚本。

我正在关注 this教程。我目前专注于 PHP 方面的事情。一切设置都与教程中的设置相同,但出于某种原因,我不断得到

Notice: Undefined variable: results in localhost/copy.php on line 32

null

每次我用类似的东西调用它

localhost/copy.php?term=arr

这是我的代码:

 <?php if ( !isset($_REQUEST['term']) )   exit;
else {$term = $_REQUEST['term'];}

$host = 'localhost';
$user = 'user_test';
$pass = 'root';
$db = 'testing';
$con = mysql_connect($host, $user, $pass);
mysql_select_db($db);
$query = "SELECT name FROM mytable WHERE name LIKE '$term' ";

$result = mysql_query($query);

if($result === FALSE) {
die(mysql_error());
}

while($row = mysql_fetch_array($result))
{
$results[] = array('label' => $row['name']);
}

echo json_encode($results); // Line 32



?>

数据库 My database

最佳答案

如果查询没有返回任何行,$results 将保持未定义状态。确保变量在其使用的相同编码级别中被初始化或赋值总是一个更好的主意。

关于php - 为什么这个脚本没有按预期工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22283757/

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