Reputation:
So, i wrote this code, and it works. But, it only returns one result. So i have 2 'Eric Clapton' name in my database, each with different id's. But the query only returns one of those 2. Any ideas?
<?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);
}
?>
Truth, now we're talking->
<?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;
?>
Upvotes: 0
Views: 99
Reputation: 49919
You are missing your loop, where you loop through your results:
while($row = mysql_fetch_array($result))
{
echo "<pre>";
print_r($row);
echo "</pre>";
}
What you currently did is get the first row from the result.
Upvotes: 2