Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I mix MySQL APIs in PHP?

I have searched the net and so far what I have seen is that you can use mysql_ and mysqli_ 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 with mysqli_close(). For the second one.

What is the problem? Can't I use mysql_ and mysqli together? Or is it normal? Is the way I can check if the connections are valid at all? (the if(mysq...))

like image 266
N3mo Avatar asked Jul 05 '13 23:07

N3mo


2 Answers

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

There is a mysqli_close, though.

like image 54
Explosion Pills Avatar answered Oct 05 '22 23:10

Explosion Pills


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_*, mysqli_*, PDO) 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.

  • http://php.net/manual/en/mysqlinfo.api.choosing.php
like image 38
Rizier123 Avatar answered Oct 06 '22 00:10

Rizier123