gpt4 book ai didi

php - MySQL 查询不返回任何内容

转载 作者:行者123 更新时间:2023-11-29 06:59:02 25 4
gpt4 key购买 nike

我在堆栈中搜索有关此问题的信息,但一无所获。这可能是一个语法问题,但我不明白。

//Include necessary files
include_once("inc/mysql.inc.php");
include_once("inc/security.inc.php");

//SECURITY: Check input before adding them into the table.
$u = security_checkinput($u);

//Connect to the MySQL database.
mysql_dbconnection();
//Go get users id corresponding to the search
$u = mysql_query("SELECT id FROM users
WHERE name LIKE \"%$u%\" OR active LIKE \"%$u%\"
ORDER BY name ASC") or die("There is a server error : " . mysql_error());
$u = mysql_fetch_row($u);
//Close database connection
mysql_dbclose();

//Return the array.
return $u;

查询后我在 $u 上尝试了 mysql_num_rows,它返回 0。不幸的是,用户表和名称列中有一些东西。

有什么帮助吗?非常感谢!!

编辑:问题来自 security_check 函数。我想尽可能避免 sql 注入(inject),所以我做了这个:

    function security_checkinput ($s)
{
$s = stripslashes($s);
if (!is_numeric($s))
$s = "'".addslashes($s)."'";
return $s;
}

最佳答案

第 1 点:您正在将查询分配给 $u 并在 LIKE 语句中使用相同的变量。

第 2 点:您正在写 LIKE 语句不正确。应该是

$query = mysql_query("SELECT id FROM users
WHERE name LIKE '%$u%' OR active LIKE '%$u%'
ORDER BY name ASC") or die("There is a server error : " . mysql_error());

$query = mysql_query("SELECT id FROM users
WHERE CONCAT(name, ' ,' , active) LIKE '%$u%'
ORDER BY name ASC") or die("There is a server error : " . mysql_error());

我更喜欢 CONCAT,因为如果有很多变量,您不需要编写 n 个 OR 语句。

关于php - MySQL 查询不返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11161341/

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