所以,我写了这段代码,它工作了。但是,它只返回一个结果。所以我有2 '埃里克克莱普顿'的名字在我的数据库中,每个有不同的id。但查询只返回这两个中的一个。什么好主意吗?
<?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>";
}
您当前所做的是从结果中获取第一行。