关于mysql:php 5.6 +中的mysql_connect

mysql_connect in php 5.6 +

本问题已经有最佳答案,请猛点这里访问。

我在godaddy主机中使用了php 5.4。我有一个PHP脚本,在其中运行良好。现在我已经更改了托管,新的托管公司提供了php 5.6。我不使用PHP编码。我的脚本出现错误,如下所示

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

我的配置文件如下

1
2
3
4
5
6
$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");

我在search.php中使用它,如下所示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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'];

请帮帮我。我如何解决这个问题?

谢谢


用于myqli连接

1
2
3
4
5
$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");

对于查询,请遵循以下答案:如何在PHP中防止SQL注入?真是太好了。

你可以用这个查询

1
2
3
4
5
6
7
$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
}

大多数mysql_uu语法可以与mysqli一起使用_


随着PHP逐渐成为一种面向对象的脚本语言,最好使用PDO来连接数据库并执行操作,因为您需要付出更多的努力。就像为每个表(每个列都是变量)创建实体类一样,这是唯一繁忙的部分,但它将使程序更安全、更可读。

我只是给出了连接到数据库和检索数据集的代码:

1。pBuff.PHP

1
2
3
4
5
6
7
8
9
10
11
12
$dsn = 'mysql:dbname=<database-name>;host=<host-name>';
$user = '<user-name>';
$password = '<password>';

try
{
    $conn = new PDO($dsn, $user, $password);
}
catch (PDOException $e)
{
    echo 'Connection failed: ' . $e->getMessage();
}

2。搜索引擎

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
require_once 'DBConfig.php';   //If DBConnection is not made in same file
require_once '<name-of-entity-class>.php';

$q = (isset($_POST['q']) && !empty($_POST['q'])) ? $_POST['q'] : NULL;

try
{
   $query ="SELECT qu_text FROM quotes WHERE qu_text LIKE :q";

   $stmt = $conn->prepare($query);

   $stmt->bindValue(':q', $q, PDO::PARAM_STR);

   $stmt->execute();

   while($row = $stmt->fetch())
   {
      $dataset[] = new <name-of-entity-class>($row);
   }

   if(!empty($dataset))
   {
      foreach ($dataset as $data)
      {  
        echo '<p>
'
;
        echo $data->get<var-name>;
        echo '
</p>'
;
      }
   }
   else
      echo 'empty database';
}
catch (Exception $ex)
{
     echo 'Some error occured: ' . $e->getMessage();
}

谢谢和问候。