简体   繁体   中英

Mysqli_query returns FALSE without reason

I'm creating pagination script and for some reason mysqli_query returns FALSE.

$limit = "LIMIT " . ($pn - 1) * $itemsPerPage . "," . $itemsPerPage; 
            $sql = 'SELECT * FROM blogs ORDER BY BlogID DESC $limit';
            $query2 = mysqli_query($connection, $sql);    

When i Echo $limit it returns 0,2 so LIMIT WORKS, when I echo $sql it returns: SELECT * FROM blogs ORDER BY BlogID DESC 0,2 and for some reason query2 returns FALSE and not true, and because of that i can't print what I want on page.

while($row = mysqli_fetch_array($query2)){
                echo '<b style="font-size: 15pt; text-align: center">' . $row[3] . '</b><br/>
                <p style="text-align: justify"> ' . $row[4] . ' </p> <hr>';
            }

Here's the other part of code... I have no idea why this doesn't work because line: SELECT * FROM blogs ORDER BY BlogID DESC 0,2 WORKS in phpMA.

PLEASE HELP!

$sql variable confliction occur, Change this,

$sql = 'SELECT * FROM blogs ORDER BY BlogID DESC $limit';
            $query2 = mysqli_query($connection, $sql); 

as

$sqlnew = 'SELECT * FROM blogs ORDER BY BlogID DESC '.$limit;
            $query2 = mysqli_query($connection, $sqlnew); 

Why you're facing this problem

$limit is not converted in the corresponding value of the variable

Solution

Set $sql like this:

$sql = 'SELECT * FROM blogs ORDER BY BlogID DESC '.$limit;

You put $limit variable in '' which means that PHP won't parse it so the query is wrong.

Read more on http://php.net/manual/en/language.types.string.php#language.types.string.syntax.single

change

$sql = 'SELECT * FROM blogs ORDER BY BlogID DESC $limit';

to

$sql = "SELECT * FROM blogs ORDER BY BlogID DESC $limit";

and it'll work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM