Reputation: 10564
I'm trying to use prepared statements to retrieve the contents of a MySQL table.
$sql = $mysqli->prepare("SELECT 'number' FROM rooms
WHERE 1
ORDER BY 'number' ASC
LIMIT ?, ? ");
$n1 = 0;
$n2 = 30;
$sql->bind_param('ii', $n1, $n2);
$sql->execute();
$sql->bind_result($number);
while ($sql->fetch()){
printf($number);
}
According to almost 3 different tutorials and manuals I've read, this script should iterate thru the contents of the column "number" of the table "rooms" and print out the result. Instead, it just print out the name of the column itself ("number") for as many time as the number of rows.
This is driving me mad! Why is it doing it?
Upvotes: 2
Views: 567
Reputation: 10084
Use the backticks for column names
$sql = $mysqli->prepare("SELECT `number` FROM rooms
WHERE 1
ORDER BY `number` ASC
LIMIT ?, ? ");
Upvotes: 2
Reputation: 9311
Your quotes are wrong. You actually said in your query "for every entry in table rooms get me the string 'number'.
When you want to quote field- or table names, use oblique quotes instead:
SELECT `field`, 'fixed string'
FROM `table`
Upvotes: 1