gpt4 book ai didi

php - 如何从 MySQL 'LIKE' 接收各种结果?

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

所以,我编写了这段代码,并且它有效。但是,它只返回一个结果。所以我的数据库中有 2 个“Eric Clapton”名字,每个名字都有不同的 id。但查询只返回这 2 个中的一个。有什么想法吗?

<?php

$now = htmlentities(rawurldecode($_GET['word'])); // in this case i passed in 'Eric Clapton'
$cat = htmlentities($_GET['cat']); // category, in this case lets say i passed in 'music'

$con = mysql_connect("localhost","USERNAME","PASS");
if (!$con)
{
echo "<pre>An error occured, please try again later. Sorry...</pre>";
}
else{ mysql_select_db("DATABASE", $con);

$result = mysql_query("SELECT id, name FROM $cat WHERE name LIKE '%$now%'");
$row = mysql_fetch_array($result);

echo "<pre>";
print_r($row);
echo "</pre>";

mysql_close($con);
}
?>

说实话,现在我们正在谈论->

<?php

$now = '%'.htmlentities(rawurldecode($_GET['word'])).'%';
$cat = htmlentities($_GET['cat']);

$dsn = 'mysql:dbname=DATABASE;host=localhost';
$user = "USER";
$password = "PASS";

# connect to the database
try {
$DBH = new PDO($dsn, $user, $password);
$DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

# the data we want to insert
$data = array($now);

$STH = $DBH->prepare("SELECT id, name FROM $cat WHERE name LIKE ?");
$STH->execute($data);
$result = $STH->fetchAll();

}
catch(PDOException $e) {
echo "Uh-Oh, something wen't wrong. Please try again later.";
file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
}

echo "<pre>";
print_r($result);
echo "</pre>";

$DBH = null;

?>

最佳答案

您缺少循环,即循环显示结果的位置:

while($row = mysql_fetch_array($result))
{
echo "<pre>";
print_r($row);
echo "</pre>";
}

您当前所做的是从结果中获取第一行。

关于php - 如何从 MySQL 'LIKE' 接收各种结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8203538/

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