How to read from the database the last line. The browser gives an error:

Warning: mysql_fetch_array () expects parameter 1 to be resource, on line 20

Code:

$query="SELECT id,z,i,count FROM log ORDER BY 'id' DESC limit 1"; while ($row = mysql_fetch_array($query)) { echo $row[0]; echo $row[1]; echo $row[2]; echo $row[3]; } 
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

3 answers 3

You have a problem that mysql_fetch_array () takes the first resource parameter generated by mysql_query, and you pass it a string.

Please stop using the mysql extension ... It is outdated and not supported, it is insecure due to the lack of placeholders.

Use Mysqli or PDO, which in the latter case allows you to implement the OOP approach.

 $dsn = "mysql:host=$host;dbname=$db;charset=$charset"; $opt = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ); $pdo = new PDO($dsn, $user, $pass, $opt); $query="SELECT id,z,i,count FROM log ORDER BY id DESC limit 1"; $stm = $pdo->query($sql); $result = $stm->fetch(PDO::FETCH_OBJ); echo $result->count; 

    And why do you pass the string ($ query) instead of mysql_query ($ query); ?

     $query = mysql_query("SELECT id,z,i,count FROM log ORDER BY 'id' DESC limit 1"); while ($row = mysql_fetch_array($query)) { echo $row[0]; echo $row[1]; echo $row[2]; echo $row[3]; } 

      You are also told that you are passing the string instead of the result of the query. That's what you need

       $sql = "SELECT id,z,i,count FROM log ORDER BY 'id' DESC limit 1"; $query = mysql_query($sql);// Вы забыли выполнить запрос