【问题标题】:Update depreciated mysql_connect() [duplicate]更新已弃用的 mysql_connect() [重复]
【发布时间】:2017-05-14 05:38:16
【问题描述】:

我有以下 php 代码将我的数据库与 mysql_connect() 连接起来,但我不断收到以下警告:

不推荐使用:mysql_connect():不推荐使用 mysql 扩展,并且 将来会被移除:使用mysqli或PDO代替.....

更新此连接的正确方法是什么,这样我以后就不会遇到任何问题?

<?php
//CREATED BY ...
/*
1: "die()" will exit the script and show an error statement if something goes wrong
2: A "mysql_connect()" error usually means your username/password are wrong
3. A "mysql_select_db()" error usually means the database does not exist
*/
//Place db host name. Sometimes "localhost" but
//sometimes looks like this: >> ???mysql??.someserver.net
$db_host = "localhost";
//Place the username for the MySQL database here
$db_username = "...";
//Place the password here:
$db_pass = "...";
//Place the name for the MyS
$db_name = "...";

//Run the connection right here!
mysqli_connect("$db_host","$db_username","$db_pass") or die ("could not connect");
mysql_select_db("$db_name") or die("no databases");
?>

编辑:突出显示错误一目了然。

【问题讨论】:

标签: php mysql database deprecated mysql-connect


【解决方案1】:
try {
    $dbh = new PDO("mysql:dbname=$db_name;host=$db_host", $db_username, $db_pass);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

更多信息请看这里http://php.net/manual/de/pdo.prepare.php

【讨论】:

  • 他要求的是连接和选择功能,仅此而已!更多他可以阅读 php 文档!
  • 很好,您指向站点的链接比 php pdo 文档更有帮助:-/
猜你喜欢
  • 2014-03-14
  • 1970-01-01
  • 2016-06-18
  • 1970-01-01
  • 2013-05-08
  • 2017-10-06
  • 2012-01-24
  • 2021-09-11
  • 1970-01-01
相关资源
最近更新 更多