我在Godaddy Hosting中使用PHP 5.4。我有一个PHP脚本,它正常工作。现在我已经更改了Hosting和New Hosting公司提供的PHP 5.6。我没有PHP编码。我的脚本中出现错误,如下所示
不推荐使用:mysql_connect():不推荐使用mysql扩展,将来会删除它:在第7行的/home4/z4g9f1v6/public_html/mydomain.com/folder/config.php中使用mysqli或PDO
我的配置文件如下所示
$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中使用它,如下所示
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'];
请帮帮我。我该如何解决这个问题?
由于
答案 0 :(得分:4)
对于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");
查询请按照此回答How can I prevent SQL injection in PHP?,这非常好。
您可以将此用于查询
$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
}
你可以使用mysqli_
的大多数mysql_语法答案 1 :(得分:0)
由于PHP正在成为面向对象的脚本语言,最好利用PDO与数据库建立连接并执行操作,为此您需要付出更多努力。就像为每个表(每列作为变量)制作实体类一样,这是唯一的忙碌部分,但它会使程序更安全,更可读。
我只是提供连接数据库和检索数据集的代码:
<强> 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 强>
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();
}
谢谢和问候。