Priya Priya - 1 year ago 630
MySQL Question

mysql_connect in php 5.6 +

I was using PHP 5.4 in Godaddy Hosting. I have one PHP script which was working fine in it. Now I have changed Hosting and New Hosting company Provide PHP 5.6. I do not PHP coding. I am getting error in my script as below

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home4/z4g9f1v6/public_html/ on line 7

My Configure file is like below

$mysql_hostname = "localhost";
$mysql_user = "dbuser";
$mysql_password = "dbpass";
$mysql_database = "dbname";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");

and I am using it in my Search.php like below

mysql_query('SET character_set_results=utf8');
mysql_query('SET names=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_results=utf8');
mysql_query('SET collation_connection=utf8_general_ci');
$q_fix=str_replace(" ","%",$q); // Space replacing with %
$sql=mysql_query("SELECT qu_text FROM quotes WHERE qu_text LIKE '%$q%'");

Please help me. How can I solve the issue ?


Answer Source

For Myqli connection

$mysql_hostname = "localhost";
$mysql_user = "dbuser";
$mysql_password = "dbpass";
$mysql_database = "dbname";
$bd = mysqli_connect($mysql_hostname, $mysql_user, $mysql_password,$mysql_database) or die("Could not connect database");

For Query Please follow this answer How can I prevent SQL injection in PHP? it's really nice.

You could use this for query

$sql=sprintf("SELECT qu_text FROM `quotes` WHERE qu_text LIKE '%s%%'"),mysqli_real_escape_string($bd,$q));

$fetch= mysqli_query($bd,$sql) or die(mysql_error());

while ($row = mysqli_fetch_array($fetch, MYSQLI_ASSOC)) {
//Your Result

Most of mysql_ syntax you could use with mysqli_

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download