0

I am having trouble extracting information from a table I have created in MYSQL. I am trying to extract the last entry id number of an individual and want to store it into a variable called $person_id. So far, I have the following:

$res = mysql_query("SELECT max(person_id) FROM Persons;");
$person_id= mysql_query($res,$conn);
echo $person_id;

Nothing shows up when I try to print the variable name person_id.

The connection to the database works fine since I am able to insert data from a form I created. Any advice?

  • You shouldn't be using any `mysql_` functions anymore. You are seeking debigging support, but have not provided enough detail for us to help you. What does the table schema look like? What is in the error logs? – mickmackusa Mar 26 '20 at 04:20
  • **Warning:** `mysql_*` extension is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0. Instead, either the [mysqli](https://www.php.net/manual/en/book.mysqli.php) or [PDO_MySQL](https://www.php.net/manual/en/book.pdo.php) extension should be used. See also the [MySQL API Overview](https://www.php.net/manual/en/mysqlinfo.api.choosing.php) for further help while choosing a MySQL API. – Dharman Mar 28 '20 at 18:50

3 Answers3

0

mysql_query was deprecated in PHP 5.5. It will return a resource on success, or FALSE on error.

So, instead of echo, you could first try a var_dump($person_id).

Source : https://www.php.net/manual/en/function.mysql-query.php

LBS
  • 518
  • 8
  • 17
0

First, avoid using mysql this extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. Now back to your question, you are printing out the wrong value, your query using mysqli should look like this

$res = "SELECT max(person_id) FROM Persons;";
$person = mysqli_query($conn, $res);
echo $person;

this will return nothing because the result is an array(mysqli object) it cannot be converted to string just like that but, using print_r we can print out the value of person as follows

print_r($persons);

you should get something like this

mysqli_result Object ( [current_field] => 0 [field_count] => count [lengths] => [num_rows] => rows [type] => 0 )

the count and rows represents, depending on the results the number of fields and rows used in the query.

To print out the $person variable correctly, as in your query above it should be

$res = "SELECT max(person_id) FROM Persons;";
$result = mysqli_query($conn, $res);
//fetch the database values as an asscoiative array
while($row = mysqli_fetch_assoc($result))
{
$person_id = $row['person_id'];
}
echo $person_id;

this should print the value you wanted

-1

You must have got some error because you are missing something here. Thats how it should work for you.

$res = "SELECT max(person_id) FROM Persons";
$result = mysql_query($res,$conn);
list($person_id) = mysql_fetch_row($result);
echo $person_id;
Muhammad Umair
  • 631
  • 1
  • 8
  • 19
  • **Warning:** `mysql_*` extension is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0. Instead, either the [mysqli](https://www.php.net/manual/en/book.mysqli.php) or [PDO_MySQL](https://www.php.net/manual/en/book.pdo.php) extension should be used. See also the [MySQL API Overview](https://www.php.net/manual/en/mysqlinfo.api.choosing.php) for further help while choosing a MySQL API. – Dharman Mar 28 '20 at 18:50