Can I mix MySQL APIs in PHP?

725

I have searched the net and so far what I have seen is that you can usemysql_ andmysqli_ together meaning:

<?php
$con=mysqli_connect("localhost", "root" ,"" ,"mysql");

if( mysqli_connect_errno( $con ) ) {
    echo "failed to connect";
}else{
    echo "connected";
}
mysql_close($con);
echo "Done";
?>

or

<?php
$con=mysql_connect("localhost", "root" ,"" ,"mysql");
if( mysqli_connect_errno( $con ) ) {
    echo "failed to connect";
}else{
    echo "connected";
}
mysqli_close($con);
echo "Done";
?>

Are valid but when I use this code what I get is:

Connected
Warning: mysql_close() expects parameter 1 to be resource, object given in D:\************.php on line 9
Done

For the first and the same except withmysqli_close(). For the second one.

What is the problem? Can't I usemysql_ andmysqli together? Or is it normal? Is the way I can check if the connections are valid at all? (theif(mysq...))

51

Answer

Solution:

No, you can't usemysql andmysqli together. They are separate APIs and the resources they create are incompatible with one another.

There is amysqli_close, though.

51

Answer

Solution:

Just to give a general answer here about all three MYSQL API's with a reference:

You can't mix any of the three (, , ) MYSQL API's from PHP together, it just doesn't work. It's even in the manual FAQ:

It is not possible to mix the extensions. So, for example, passing a mysqli connection to PDO_MySQL or ext/mysql will not work.


You need to use the same MySQL API and its related functions, from connection to querying.




913
votes

Answer

Solution:

Technically you can use as many separate connections as you want, while your problem is caused by a mere typo - you only cannot use resources from one extension with functions from another, which is quite obviously.

However, you should avoid multiple connections from the same script, no matter from single API or different ones. As it will burden your database server and exhaust its resources. So, although technically you can, you shouldn't mix different extensions in your code, save for the short period of refactoring.




302
votes

Answer

Solution:

MySQLi is a lot more secure thanMySQL which is anyway now deprecated. That's why you should stick withMySQLi and also you can't mix them as they are both different.

536

Answer

Solution:

No, you can't usemysql andmysqli together. They are separate APIs and the resources they create are incompatible with one another.

There is amysqli_close, though.

820

Answer

Solution:

Just to give a general answer here about all three MYSQL API's with a reference:

You can't mix any of the three (, , ) MYSQL API's from PHP together, it just doesn't work. It's even in the manual FAQ:

It is not possible to mix the extensions. So, for example, passing a mysqli connection to PDO_MySQL or ext/mysql will not work.


You need to use the same MySQL API and its related functions, from connection to querying.

Write your answer




135
votes

Answer

Solution:

Technically you can use as many separate connections as you want, while your problem is caused by a mere typo - you only cannot use resources from one extension with functions from another, which is quite obviously.

However, you should avoid multiple connections from the same script, no matter from single API or different ones. As it will burden your database server and exhaust its resources. So, although technically you can, you shouldn't mix different extensions in your code, save for the short period of refactoring.

Write your answer




308
votes

Answer

Solution:

MySQLi is a lot more secure thanMySQL which is anyway now deprecated. That's why you should stick withMySQLi and also you can't mix them as they are both different.

People are also looking for solutions to the problem: php - What does { } do within a string?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.