-1

I've tried to use the solutions presented in this question, to no avail, so I used this:

  $stat = "SELECT MAX(employee_id) FROM employees";
 $querysult = intval($connection, $stat);

Where employee_id is an int(3) in the database table. For some reason, the above code actually gets the values from the database, despite there not being a mysqli_query() in sight. But my question is about what I did immediately after, which was

echo "Id: " . $querysult;

and which output nothing but

Id:

and no number. I've also tried casting the number to a string, and concatenating it to an empty string before the echo statement.

Community
  • 1
  • 1
  • 3
    "For some reason, the above code actually gets the values from the database, despite there not being a mysqli_query() in sight. " NO clearly it does not –  Apr 12 '16 at 00:14
  • Use an alias inside a loop and echo that row, *done like dinner*. – Funk Forty Niner Apr 12 '16 at 00:16
  • _I've tried to use the solutions presented in this question_ But first you have to master querying the database. [Start here, Its a secret source of information about PHP, almost nobody else knows about it, so it can be our secret. Its even in 11 languages](http://php.net/manual/en/book.mysqli.php) – RiggsFolly Apr 12 '16 at 00:18
  • sure but actually run the query to @Fred-ii- –  Apr 12 '16 at 00:18
  • *si, signore* @Dagon – Funk Forty Niner Apr 12 '16 at 00:18
  • We need more information to be able to help you out – Erick Apr 12 '16 at 01:33
  • Dagon the query does work, since it gets the proper ID value (. Erick, I'm going to update it in an hour or so with a lot more code then. @RiggsFolly, I've been using that documentation, but it appears to be COMPLETELY useless as far as getting an individual value out of a database is concerned, since mysqli_result is a cryptic creature where none of the methods return proper types. – Pedro Bento Apr 13 '16 at 11:52

2 Answers2

0

For some reason, the above code actually gets the values from the database, despite there not being a mysqli_query() in sight

This of course is quite impossible, unless you are getting something from a previously executed query that uses the same variable names.

I think your main problem is that accessing the value of the query coded using just SELECT MAX(employee_id) will return a column with the name MAX(employee_id) and that is not a valid PHP variable name. So what you have to do is give that column another name that is a valid PHP variables name using this syntax SELECT MAX(employee_id) as max_empid which renames the column to max_empid

I am assuming nothing so I will also include a connection to the database in my answer. You will need to replace the my_user, my_password and my_db values, or ignore the connection if you have already dont that somewhere else. I have also used the Object Oriented approach to MYSQLI, if you are using the proceedural calls, you may have to amend the code accordingly

// connect to your database
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');

// build query and use an alias for the `MAX(employee_id)` 
// so you can easily use its name in the result set

$sql = "SELECT MAX(employee_id) as max_empid FROM employees";

// Now we must execute this query
$result = $mysqli->query($sql);

// Now we must chech that the query worked
if ( $result === FALSE ) {
    echo sprintf( 'Query Failed: %s<br>%s', $sql, $mysqli->error);
    exit;
}

// now we must read one of the rows from the result set 
// produced by the ->query() command. 
// In this case there of course there is only one result row

$row = $result->fetch_object();

echo 'Id: ' . $row->max_empid;
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
-1

It may be because you are trying to convert a connection to an int value.

Try this $connection = new mysqli(); $querysult =mysqli_query( $stat);

printf("Select returned %d.\n", $querysult->num_rows);

Joker228
  • 1
  • 1