Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql_connect in php 5.6 + [duplicate]

Tags:

php

mysql

mysqli

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/mydomain.com/folder/config.php 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

include("config.php");
if($_SERVER["REQUEST_METHOD"] == "POST")
{
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=$_POST['q'];
$q=mysql_escape_string($q);
$q_fix=str_replace(" ","%",$q); // Space replacing with %
$sql=mysql_query("SELECT qu_text FROM quotes WHERE qu_text LIKE '%$q%'");
}while($row=mysql_fetch_array($sql)){$title=$row['qu_text'];

Please help me. How can I solve the issue ?

Thanks

like image 264
Priya Avatar asked Nov 27 '16 03:11

Priya


People also ask

Does PHP 5.6 support mysql_connect?

It's still available in 5.6.

Is mysql_connect deprecated?

This extension was deprecated in PHP 5.5. 0, and it was removed in PHP 7.0.

What does the mysql_connect () function do?

mysql_connect() establishes a connection to a MySQL server. The following defaults are assumed for missing optional parameters: server = 'localhost:3306', username = name of the user that owns the server process and password = empty password. The server parameter can also include a port number.

What does the mysqli_connect () function return?

The mysqli_connect() function establishes a connection with MySQL server and returns the connection as an object.


Video Answer


1 Answers

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_

like image 181
DAKSH Avatar answered Oct 16 '22 17:10

DAKSH