gpt4 book ai didi

php - PHP 文件上的 LIKE 查询问题

转载 作者:行者123 更新时间:2023-11-29 23:20:45 26 4
gpt4 key购买 nike

我正在开发一个通过 php 文件连接到 sql 数据库的 Android 应用程序。目前,我的队列之一上的 like 函数遇到问题。

以下是出现问题的文件的代码:

$con = mysqli_connect($host,$uname,$pwd,$db) or die("connection failed");

$like = $_REQUEST['like'];

$sql_q = mysqli_query($con,"SELECT `ID`, `Value`, `Value_Complete` FROM `products` WHERE `ID` LIKE '$like'");

if($sql_q)
{
while($result = mysqli_fetch_assoc($sql_q))
{
$output[] = $result;
}
if($output)
{
print(json_encode($output));
}
}
else
{
echo 'Invalid query: ' . mysqli_error() . "\n";
}

mysqli_close($con);
?>

此代码适用于此查询 - SELECT IDValueValue_Complete FROM products WHERE ID LIKE '11/02/__/00/%' - 并返回:

[{"ID":"11\/02\/00\/00\/00\/0\/0\/0","Value":"Tradicionais","Value_Complete":""},     
{"ID":"11\/02\/01\/00\/00\/0\/0\/0","Value":"Caipis","Value_Complete":""},
{"ID":"11\/02\/02\/00\/00\/1\/0\/0","Value":"Daiquiri","Value_Complete":""},
{"ID":"11\/02\/03\/00\/00\/1\/0\/0","Value":"Gin Tonico","Value_Complete":""},
{"ID":"11\/02\/04\/00\/00\/1\/0\/0","Value":"Long Island Ice Tea","Value_Complete":""},
{"ID":"11\/02\/05\/00\/00\/1\/0\/0","Value":"Manhattan","Value_Complete":""},
{"ID":"11\/02\/06\/00\/00\/1\/0\/0","Value":"Margarita","Value_Complete":""},
{"ID":"11\/02\/07\/00\/00\/1\/0\/0","Value":"Martini Seco","Value_Complete":""},
{"ID":"11\/02\/08\/00\/00\/1\/0\/0","Value":"Black Russian","Value_Complete":""},
{"ID":"11\/02\/09\/00\/00\/1\/0\/0","Value":"White Russian","Value_Complete":""},
{"ID":"11\/02\/10\/00\/00\/1\/0\/0","Value":"Sex on the Beach","Value_Complete":""},
{"ID":"11\/02\/11\/00\/00\/1\/0\/0","Value":"Sidecar","Value_Complete":""},
{"ID":"11\/02\/12\/00\/00\/1\/0\/0","Value":"Sakerinha","Value_Complete":""},
{"ID":"11\/02\/13\/00\/00\/1\/0\/0","Value":"Tequila Sunrise","Value_Complete":""},
{"ID":"11\/02\/14\/00\/00\/0\/0\/0","Value":"Vodka","Value_Complete":""}]

相同的查询在 phpMyAdmin 中返回此:

working sql query result

但是,相同的代码不适用于查询 - SELECT ID, Value, Value_Complete FROM products > WHERE ID LIKE '$like'。它返回一个空结果集,但在 phpMyAdmin 中返回: not working sql query result

我只是无法弄清楚这里的问题...即使这样 - SELECT ID, Value, Value_Complete FROM products WHERE ID LIKE '%' - 当它应该返回所有内容时不返回任何内容。

你能帮我吗?谢谢。

最佳答案

尝试更改您的查询:

$con = mysqli_connect($host,$uname,$pwd,$db) or die("connection failed");

$like = $_REQUEST['like'];

$sql_q = mysqli_query($con,"SELECT `ID`, `Value`, `Value_Complete` FROM `products` WHERE `ID` LIKE '%".$like."%'");

if($sql_q)
{
while($result = mysqli_fetch_assoc($sql_q))
{
$output[] = $result;
}
if($output)
{
print(json_encode($output));
}
}
else
{
echo 'Invalid query: ' . mysqli_error() . "\n";
}

mysqli_close($con);
?>

关于php - PHP 文件上的 LIKE 查询问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27331052/

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