将MySQLi转换为PDO会带来两条错误消息

时间:2016-12-12 06:39:34

标签: php mysql mysqli pdo

我刚刚开始学习PDO,我有一个小脚本,我做了一段时间,我想将它转换为PDO兼容代码,到目前为止,我已经转换了数据库连接,当我刷新它似乎打破了我的代码,我收到以下错误消息,

  

警告:mysqli_query()要求参数1为mysqli,对象为

  

警告:mysqli_fetch_array()期望参数1为mysqli_result,给定为null

我觉得有点尴尬地问这个问题,有人可以帮我解决我做错了什么,非常感谢,非常感谢。

$dbconnect = new PDO('mysql:host=localhost;dbname=db', 'user', 'pw');
$record = mysqli_fetch_array(mysqli_query($dbconnect, "SELECT record FROM record"));
$record = $record[0];
$xml = simplexml_load_string($record);

1 个答案:

答案 0 :(得分:0)

$servername = "localhost";
$username = "username";
$password = "password";

try {
    $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }