1

This is my code

$conn = mysql_connect($serverName, $username, $password)
        or die("Unable to connect to MySQL");
    $selected = mysql_select_db($databaseName,$conn)
        or die("Could not find the database");
    $result = mysql_query("INSERT INTO `user` () VALUES ()");
    return mysqli_insert_id($result)

This is the exception

Warning: mysqli_insert_id() expects parameter 1 to be mysqli, boolean given in C:\xampp2\htdocs\XXX\controllers\User.php on line 24

and the line 24 is return mysqli_insert_id($result)

Marco Dinatsoli
  • 10,322
  • 37
  • 139
  • 253
  • You're mixing `mysql` and `mysqli`. They aren't related. – Barmar Dec 11 '14 at 15:37
  • 1
    I think there are thousands of questions on SO posted by beginners that start learning PHP and MySQL with the `mysql_*` functions. Most of these questions have comments and answers and comments to answers that say "DO NOT use mysql, use mysqli or PDO". I bet in 2020 there still will be newbies asking where is the bug in their `mysql_query() or die()` code. :'-( – axiac Dec 11 '14 at 15:50

4 Answers4

6

You are using mysql with mysqli. You cannot use both, you must choose which MySQL extension you want to use.

If mysqli:

Try using the mysqli connect and the mysqli select db

If mysql:

use mysql_insert_id(), not mysqli_insert_id()

I would use PDO myself, mysql is depricated:

try 
{
    // Connect to your database
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);

    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // Put your insert statement
    $sql = "INSERT INTO `user` () VALUES ()";

    // use exec() because no results are returned
    $conn->exec($sql);

    $last_id = $conn->lastInsertId();

    echo "New record created successfully. Last inserted ID is: " . $last_id;
}
catch(PDOException $e)
{
    echo $sql . "<br />" . $e->getMessage();
}

Updated: The MySQL extension: has been removed and is no longer available

Demodave
  • 6,242
  • 6
  • 43
  • 58
3

You have to be consistent about which MySQL extension you're using. If you're using the mysql extension, you must use mysql_insert_id(), not mysqli_insert_id().

It would be better if you rewrote all your code to use the mysqli or PDO extensions, as the mysql extension is deprecated. But it's not a drop-in replacement, the syntax is somewhat different.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

You mix mysql with mysqli and the boolean mentioned in the error message is FALSE. Your INSERT query failed because it does not provide any values to be inserted. It has syntax errors on ().

Try this:

$result = mysql_query("INSERT INTO `user` (`name`) VALUES ('John Doe')");
return mysql_insert_id($result);

DO NOT use mysql_* functions. The mysql extension is deprecated since PHP 5.5 and will be removed in the future. Use either mysqli or PDO.

axiac
  • 68,258
  • 9
  • 99
  • 134
-1

Mixed mysql_ and mysqli. Try

mysql_insert_id();

instead. If failed.

mysql_query("SELECT last_insert_id()");

should work for you.

AkiEru
  • 780
  • 1
  • 9
  • 34