php 5.6 中的 mysql_connect +

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40825445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:15:45  来源:igfitidea点击:

mysql_connect in php 5.6 +

phpmysqlmysqli

提问by Priya

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

我在 Godaddy Hosting 中使用 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

已弃用:mysql_connect():mysql 扩展已弃用,将来会被删除:在第 7 行的 /home4/z4g9f1v6/public_html/mydomain.com/folder/config.php 中使用 mysqli 或 PDO 代替

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

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

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

谢谢

回答by DAKSH

For Myqli connection

用于 Myqli 连接

$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.

For Query 请按照这个答案How can I prevent SQL injection in PHP? 这太好了。

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_

您可以与 mysqli_ 一起使用的大多数 mysql_ 语法

回答by Subhra Jyoti Lahiri

As PHP is becoming a Object Oriented Scripting language, it will be better to make use of PDOs to make connections to Database and perform the operations, for this you have a give a little bit of more effort. Like making Entity Classes for each table's(each column as variable), this is the only hectic part but it will make the program more secure and more readable.

由于 PHP 正在成为一种面向对象的脚本语言,因此最好使用 PDO 连接到数据库并执行操作,为此您需要付出更多的努力。就像为每个表(每列作为变量)制作实体类一样,这是唯一忙碌的部分,但它会使程序更安全和更具可读性。

I am just giving the code for connecting to database and retrieving the dataset :

我只是提供连接到数据库和检索数据集的代码:

1. DBConfig.php

1.DBConfig.php

$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. Search.php

2.搜索.php

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();
}

Thanks and Regards.

感谢致敬。