在wamp堆栈中运行php代码时出现mysql vs mysqli错误

时间:2015-08-22 06:51:22

标签: php

(mysql vs mysqli error(advait))

大家好,见代码。此代码在我的localhost WAMP Win7 php ver 5.5.12上运行,但它出错:

---
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\wamp\www\get_data02.php on line 9
Call Stack
#   Time    Memory  Function    Location
1   0.0010  135216  {main}( )   ..\get_data02.php:0
2   0.0010  135680  mysql_connect ( )   ..\get_data02.php:9
---

我尝试用mysqli替换mysql,但这只是给出了更多的错误。我该如何解决?谢谢,

<?php
// 1. Enter Database details
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'iamdb_copy_04';
// 2. Create a database connection
$connection = mysql_connect($dbhost,$dbuser,$dbpass);
if (!$connection) {
    die("Database connection failed: " . mysql_error());
}
// 3. Select a database to use 
$db_select = mysql_select_db($dbname,$connection);
if (!$db_select) {
    die("Database selection failed: " . mysql_error());
}
$query = mysql_query("SELECT * FROM users WHERE city = 'city' ");

while ($rows = mysql_fetch_array($query)) { 
   $f_name = $rows['first_name'];
   $address_01 = $rows['address_01'];
   $city = $rows['city'];
   echo "$f_name<br>$address_01<br>$city<br><br>";      
} 
?>

1 个答案:

答案 0 :(得分:0)

有两种方法可以摆脱这个错误。

  1. 在php display_errors启动后使用ini_set("display_errors",0)在php文件中设置<?php

  2. mysql替换为mysqlimysql_connectmysql_select_dbmysql_query中的mysql_fetch_array

  3. 如果您仍然遇到任何问题,请告诉我并说明您的错误。