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...)
)
My Answer:
Yes, you can mix MySQL APIs in PHP. You can use both the mysql_ and mysqli_ functions in the same PHP script. However, it is recommended to use mysqli_ functions as they provide better security and performance compared to the older mysql_ functions. It is important to note that the mysql_ functions are deprecated as of PHP 5.5.0 and have been removed in PHP 7.0.0, so it is advisable to use mysqli_ or PDO for database operations.
Rate this post
5 of 5 based on 5256 votesComments