By N3mo

2013-07-05 23:48:36 8 Comments

I have searched the net and so far what I have seen is that you can use mysql_ and mysqli_ together meaning:

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

if( mysqli_connect_errno( $con ) ) {
    echo "failed to connect";
    echo "connected";
echo "Done";


$con=mysql_connect("localhost", "root" ,"" ,"mysql");
if( mysqli_connect_errno( $con ) ) {
    echo "failed to connect";
    echo "connected";
echo "Done";

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

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

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


@user2555269 2013-07-05 23:55:38

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

@Rizier123 2015-07-09 13:59:15

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.

@Funk Forty Niner 2015-12-10 19:05:44

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 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?

@Rizier123 2015-12-10 19:49:32

@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

@Funk Forty Niner 2015-12-10 19:53:01

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.

@Rizier123 2015-12-10 19:56:40

@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 (☕☕☕).

@Funk Forty Niner 2016-12-21 13:40:42

You might like to add something about sqlsrv_query(). I just closed a question here

@hanshenrik 2019-04-04 08:27:28

@FunkFortyNiner you're correct, mysql_real_escape_string should absolutely not be mixed with PDO, and if you do then SQL injection vulnerabilities and incorrectly escaped strings are likely to occur. that said, the PDO-version of mysql_real_escape_string() is called PDO::quote(), but warning: quote() adds quotes around the string, mysql_real_escape_string does not, so $str="'".mysql_real_escape_string($str)."'"; translates to $str=$pdo->quote($str);, NOT $str="'".$pdo->quote($str)."'"; as one might assume when porting it.

@Your Common Sense 2015-07-09 14:08:54

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.

@Doug 2015-07-09 14:43:19

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.

@Explosion Pills 2013-07-05 23:53:05

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.

@cHao 2013-07-05 23:54:59

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

@Explosion Pills 2013-07-05 23:56:09

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

@RationalRabbit 2017-12-31 21:23:46

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

Related Questions

Sponsored Content

32 Answered Questions

[SOLVED] How do I get a YouTube video thumbnail from the YouTube API?

38 Answered Questions

[SOLVED] Deleting an element from an array in PHP

  • 2008-12-15 20:28:55
  • Ben
  • 2663743 View
  • 2541 Score
  • 38 Answer
  • Tags:   php arrays unset

48 Answered Questions

[SOLVED] How do I import an SQL file using the command line in MySQL?

20 Answered Questions

[SOLVED] Reference — What does this symbol mean in PHP?

28 Answered Questions

[SOLVED] How can I prevent SQL injection in PHP?

36 Answered Questions

[SOLVED] Reference - What does this error mean in PHP?

13 Answered Questions

[SOLVED] Why shouldn't I use mysql_* functions in PHP?

  • 2012-10-12 13:18:39
  • Madara's Ghost
  • 222571 View
  • 2511 Score
  • 13 Answer
  • Tags:   php mysql

30 Answered Questions

[SOLVED] How do you parse and process HTML/XML in PHP?

39 Answered Questions

[SOLVED] Should I use the datetime or timestamp data type in MySQL?

7 Answered Questions

[SOLVED] How does PHP 'foreach' actually work?

Sponsored Content