Cael
Cael

Reputation: 556

Selecting last row in database not working using PHP

I have a php code that will select the last row in mysql using database but this error comes out:

 syntax error, unexpected '$result' (T_VARIABLE)

My php code:

$con = mysqli_connect("localhost","root","","productno") or die("Error " .     mysqli_error($con));

// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con, "SELECT Alibaba FROM records ORDER BY Date DESC LIMIT 1");

if (mysqli_num_rows($result) > 0) 
{
    $s_Alibaba = mysqli_fetch_row($result);
    $sql_Alibaba = $s_Alibaba[0]; //Compare with the last record
}   

echo $sql_Alibaba;

Any idea how to fix it? thanks

Upvotes: 0

Views: 135

Answers (1)

vaso123
vaso123

Reputation: 12391

The problem is, date is a reserved keyword in mysql. Escape it with ` characters around:

$result = mysqli_query($con, "SELECT Alibaba FROM records ORDER BY `Date` DESC LIMIT 1");

See here: http://dev.mysql.com/doc/refman/5.6/en/reserved-words.html

Upvotes: 1

Related Questions