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:

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

4 comments

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

@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 stackoverflow.com/q/41263771

@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] Reference - What does this error mean in PHP?

34 Answered Questions

[SOLVED] PHP: Delete an element from an array

  • 2008-12-15 20:28:55
  • Ben
  • 2157798 View
  • 2153 Score
  • 34 Answer
  • Tags:   php arrays

18 Answered Questions

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

28 Answered Questions

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

15 Answered Questions

[SOLVED] How to get a list of MySQL user accounts

  • 2009-07-16 03:23:53
  • burntsugar
  • 1386102 View
  • 1271 Score
  • 15 Answer
  • Tags:   mysql mysql5

35 Answered Questions

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

29 Answered Questions

[SOLVED] How Do You Parse and Process HTML/XML in PHP?

15 Answered Questions

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

  • 2012-10-12 13:18:39
  • Madara Uchiha
  • 195893 View
  • 2313 Score
  • 15 Answer
  • Tags:   php mysql database

31 Answered Questions

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

3 Answered Questions

[SOLVED] PHP: mysql v mysqli v pdo

  • 2012-08-23 17:40:14
  • Vlad
  • 10722 View
  • 18 Score
  • 3 Answer
  • Tags:   php mysql pdo mysqli

Sponsored Content