什么是PDO语句等价?

时间:2016-01-02 12:56:38

标签: php

我有这个使用mysql_query的代码已弃用,请告诉我这些代码的等效PDO代码:

$rs = mysql_query('select * from locations');
$result = array();
while($row = mysql_fetch_object($rs)){
    array_push($result, $row);
}

echo json_encode($result);

3 个答案:

答案 0 :(得分:1)

试试这段代码。如果要添加WHERE子句,可以在$ query_args数组中提供参数。

smtp.googlemail.com

答案 1 :(得分:0)

这是等效的。

ALTER TABLE Payments 
ADD FOREIGN KEY (customerNumber) 
REFERENCES Customers (customerNumber);
-- Here! ----------^

答案 2 :(得分:0)

connect.php

<?php
function getConnection(){
$dbhost= "localhost";
$dbuser = "root";
$dbpass="";
$dbname="table";
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8');
$dsn = "mysql:host=$dbhost;dbname=$dbname;charset=utf8";
$dbh = new PDO($dsn, $dbuser, $dbpass,$options);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}

file.php

require "connect.php";
$pdo = getConnection();
$dbh->prepare("SEELCT * FROM locations;");
$dbh->execute();
$result = $dbh->fetchAll(PDO::FETCH_OBJ);`