简体   繁体   English

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

[英]mysql vs mysqli error when running php code in wamp stack

( mysql vs mysqli error (advait) ) (mysql vs mysqli错误(advait))

Hi All, See code. 大家好,请参见代码。 This code runs on my localhost WAMP Win7 php ver 5.5.12 but it gives an error: 这段代码在我的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
---

I tried replacing mysql with mysqli but that just gave more errors. 我尝试用mysqli替换mysql,但这只会带来更多错误。 How do I fix this? 我该如何解决? Thanks, 谢谢,

<?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>";      
} 
?>

There are two options to get out from this error. 有两种方法可以解决此错误。

  1. Set display_errors off in php file using ini_set("display_errors",0) after start of php <?php . 设置display_errors使用PHP文件关闭ini_set("display_errors",0)的PHP的开始后<?php

  2. Replace mysql with mysqli in mysql_connect , mysql_select_db , mysql_query , mysql_fetch_array . 更换mysqlmysqlimysql_connectmysql_select_dbmysql_querymysql_fetch_array

Let me know if you still face any problem and also comment your error. 让我知道您是否仍然遇到任何问题,并评论您的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM