Categories
mysql mysqli pdo php

Can I mix MySQL APIs in PHP?

114

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...))

19

  • 7

    mysql is deprecated, it only makes sense they wouldn’t work together. Why are you trying to do so..?

    Jul 5, 2013 at 23:50


  • 9

    You should avoid using mysql_* functions altogether. They’re error-prone and unsafe, and they will be removed from PHP soon (they’re marked as deprecated at the moment). [This great answer][0] goes into way more detail explaining why they are bad. [0]:stackoverflow.com/a/12860046/1055295

    Jul 5, 2013 at 23:52

  • 2

    1) you insist on using an old shoddy interface (mysql) that’s being marked as obsolete in the doc since eons 2) for some odd reason you want to mix it with its successor instead of doing the right thing and convert to the new one 3) you are so surprised it doesn’t work that you ask on SO about it, although it should be pretty obvious that what you’re doing is nonsense.

    – fvu

    Jul 6, 2013 at 0:06

  • 2

    It’s not a superstition. Of course you can write bad code with mysqli_* functions and good code with mysql_* ones. But the latter category is marked as deprecated since it’s the inferior set of functions, not being able to support OO-style invocations or even prepared statements (to name just two examples). Given a choice of two tools to do the same job, one of which is clearly better in the long run and more flexible, isn’t the correct answer obvious?

    Jul 6, 2013 at 8:22

  • 1

    Of course not. However, given the option of choosing between the deprecated functionality and the actively supported one for, say, rolling your own abstraction, why use the old functions?

    Jul 6, 2013 at 9:54

71

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.

3

  • Although you should never need to close the connection anyway; objects clean themselves up when they’re no longer referenced anywhere. (Not sure whether plain old resources do that, but objects can actually take advantage of RAII to a not-insignificant degree.)

    – cHao

    Jul 5, 2013 at 23:54


  • @cHao not only that, but PHP will close any open MySQL connections when the script exits

    Jul 5, 2013 at 23:56

  • 1

    But not always, to my experience, so we always place a close at the bottom of the file.

    Dec 31, 2017 at 21:23

16

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.

6

  • Some guy tried to tell me today that they’ve no problem/errors when mixing mysql_real_escape_string() with what the rest of their code being PDO. Is there something I didn’t get here in my time with working with these different APIs? Am I the ignorant one here? This being for the “now deleted” question stackoverflow.com/q/34209127 only viewable by 10K+ members should anyone wonder. This in relation to $stmt3->execute(array('classID' => $_POST['class'],'studentID' => mysql_real_escape_string($substr))) – Am I missing something here?

    Dec 10, 2015 at 19:05


  • 1

    @Fred-ii- You are right 🙂 Reading the manual shows that you are correct. What probably happened is, that mysql_real_escape_string() will silently try make a connection with the default parameters which then worked for OP. So it just made the connection to get the character set. So OP has 2 connections

    – Rizier123

    Dec 10, 2015 at 19:49


  • If the OP had at least told me/us that they probably had 2 seperate connections, I’d of probably agreed; they decided otherwise. However, I still can’t see how that would still work. If it does, I’m baffled.

    Dec 10, 2015 at 19:53

  • @Fred-ii- See: link_identifier It will use the default connection settings, which it gets with ini_get(). So it probably just works for OP with the default settings. I would just leave it and get some new coffee (☕☕☕).

    – Rizier123

    Dec 10, 2015 at 19:56

  • You might like to add something about sqlsrv_query(). I just closed a question here stackoverflow.com/q/41263771

    Dec 21, 2016 at 13:40

2

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.

1

  • While this is probably a good idea, but connection pooling was developed for this reason. When you have multiple web requests hitting a web server, you cannot easily use the same connection, so you open a new connection. Connection pooling saves the overhead on the app server and the database.

    – Doug

    Jul 9, 2015 at 14:43