Saturnix
Saturnix

Reputation: 10564

MySQLi fetching column name instead of column contents?

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

Answers (2)

ozahorulia
ozahorulia

Reputation: 10084

Use the backticks for column names

$sql = $mysqli->prepare("SELECT `number` FROM rooms
        WHERE 1 
        ORDER BY  `number` ASC 
        LIMIT ?, ? ");

Upvotes: 2

Till Helge
Till Helge

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

Related Questions