mysql pdo 中文手册_MySQL (PDO)

用户评论:

[#1]

info at westgatesearch dot com [2014-10-31 23:39:29]

Here is a real world example of a PHP PDO MySQL Data Entry App with SQL Insert and redirect to a report to show the data "just entered" as auto-generated by an expert system, WizzyWeb

$handler = new PDO('mysql:host=localhost;dbname=db', 'username', 'password');

$sql = "INSERT INTO Employees (EmployeeFirstName, EmployeeLastName, EmployeeOffice, EmployeeDepartment, EmployeeEmailAddress, EmployeeExtension, EmployeeTitle) VALUES (:EmployeeFirstName, :EmployeeLastName, :EmployeeOffice, :EmployeeDepartment, :EmployeeEmailAddress, :EmployeeExtension, :EmployeeTitle)";

$query = $handler->prepare($sql);

$query->execute(array(

':EmployeeFirstName' => $EmployeeFirstName,

':EmployeeLastName' => $EmployeeLastName,

':EmployeeOffice' => $EmployeeOffice,

':EmployeeDepartment' => $EmployeeDepartment,

':EmployeeEmailAddress' => $EmployeeEmailAddress,

':EmployeeExtension' => $EmployeeExtension,

':EmployeeTitle' => $EmployeeTitle

));

// You can uncomment this code to see which PDO drivers are available and troubleshoot the PDO database connection

//try {

//   print_r(PDO::getAvailableDrivers()); # Enumerates available PDO drivers available

//   $handler = new PDO('mysql:host=localhost;dbname=db1', 'username', 'password');

//   $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

//} catch(PDOException $e) {

//   echo $e->getMessage();

//   die();

//}

header("Location: http://www.wizzyweb.com/php/report.php");

[#2]

georgy dot garnov at gmail dot com [2014-10-14 10:27:04]

PDO::MYSQL_ATTR_LOCAL_INFILE - will not work if you have

open_basedir in your php.ini

[#3]

kat dot shupe dot work at gmail dot com [2014-04-16 14:07:49]

So, I'm trying to get one PHP file to be able to call from two SQL databases at the same time. How can I do that? Or rather, how can I close a connection being made by SqltoAssoc PDO function so I can start a second one with a completely different SQL database?

Thanks in advanced!

[#4]

Gerald Schneider [2013-11-09 08:25:54]

This page suggests that the constant PDO::MYSQL_ATTR_FOUND_ROWS  was always available (no note "exists as of X.X"), but I found the constant missing on an installation with PHP 5.2. After switching the PHP version to 5.3.27 on the webspace the constant was available.

[#5]

david at manifestwebdesign dot com [2011-10-27 02:22:13]

The SSL options are silently ignored in PHP 5.3.8, see https://bugs.php.net/bug.php?id=55870

Looks like it's addressed upstream, I just want to save others the hour and a half I just wasted :)

[#6]

curt at webmasterbond dot com [2011-04-15 17:54:24]

Today's PHP snapshot now has SSL support for PDO. Follow the directions here ( http://dev.mysql.com/doc/refman/5.0/en/secure-create-certs.html ) to set up MySQL and then use the following connection options:

$pdo= newPDO('mysql:host=hostname;dbname=ssldb','username','password',

array(PDO::MYSQL_ATTR_SSL_KEY=>'/path/to/client-key.pem',PDO::MYSQL_ATTR_SSL_CERT=>'/path/to/client-cert.pem',PDO::MYSQL_ATTR_SSL_CA=>'/path/to/ca-cert.pem')

);?>

[#7]

brian at diamondsea dot com [2008-07-25 11:26:51]

SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. ...

After spending hours trying to track down why we were getting this error on a new server, after the same code ran fine on other servers, we found the problem to be an old MySQL _client_ library running on our web server, and a latest-version MySQL _server_ running on the database server's box.

Upgraded the MySQL client on the web server to the current revision and the problem went away.

[#8]

miller_kurt_e at yahoo dot com [2008-07-20 14:03:04]

SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. ...

This one can be a royal pain to deal with.  Never stack statements to be executed in one go.  Nobody ever mentions this possibility in all the posts I've seen dealing with this error.

This example is a Zend Framework example but the theory is the same.

As in:

$sql= <<<____sqlcreate>

`tid` int(11) NOT NULL,

`trqform` varchar(40) NOT NULL,

`trsform` varchar(40) NOT NULL,

`tgen` datetime NOT NULL,

`tterm` datetime,

`tstatus` tinyint(1) NOT NULL

) ENGINE=ARCHIVE COMMENT='ticket archive';

CREATE TABLE IF NOT EXISTS `request_hist` (

`rqid` int(11) NOT NULL,

`rqtid` int(11) NOT NULL,

`rqsid` int(11) NOT NULL,

`rqdate` datetime NOT NULL,

`rqcode` tinyint(1) NOT NULL,

`rssid` int(11) NOT NULL,

`rsdate` datetime,

`rscode` tinyint(1)

) ENGINE=ARCHIVE COMMENT='request archive';

CREATE TABLE IF NOT EXISTS `relay_hist` (

`rqid` int(5) NOT NULL,

`sdesc` varchar(40) NOT NULL,

`rqemail` varchar(40) NOT NULL,

`sid` int(11) NOT NULL,

`rlsid` int(11) NOT NULL,

`dcode` varchar(5) NOT NULL

) ENGINE=ARCHIVE COMMENT='relay archive';____SQL;$result=$this->db->getConnection()->exec($sql);?>

This will run fine but PDO will balk with the 'unbuffered' error if you follow this with another query.

Instead do:

$sql= <<<____sqlcreate>

`tid` int(11) NOT NULL,

`trqform` varchar(40) NOT NULL,

`trsform` varchar(40) NOT NULL,

`tgen` datetime NOT NULL,

`tterm` datetime,

`tstatus` tinyint(1) NOT NULL

) ENGINE=ARCHIVE COMMENT='ticket archive';____SQL;$result=$this->db->getConnection()->exec($sql);$sql= <<<____sqlcreate>

`rqid` int(11) NOT NULL,

`rqtid` int(11) NOT NULL,

`rqsid` int(11) NOT NULL,

`rqdate` datetime NOT NULL,

`rqcode` tinyint(1) NOT NULL,

`rssid` int(11) NOT NULL,

`rsdate` datetime,

`rscode` tinyint(1)

) ENGINE=ARCHIVE COMMENT='request archive';____SQL;$result=$this->db->getConnection()->exec($sql);$sql= <<<____sqlcreate>

`rqid` int(5) NOT NULL,

`sdesc` varchar(40) NOT NULL,

`rqemail` varchar(40) NOT NULL,

`sid` int(11) NOT NULL,

`rlsid` int(11) NOT NULL,

`dcode` varchar(5) NOT NULL

) ENGINE=ARCHIVE COMMENT='relay archive';____SQL;$result=$this->db->getConnection()->exec($sql);?>

Chopping it into individual queries fixes the problem.

[#9]

rmottey at gmail dot com [2008-01-09 03:51:29]

I have been getting the error below when performing multiple queries within a single page.

Setting the attribute below did not seem to work for me.

So building on previous example i am initilizing my stmt  variable on every query and a fetch all into an array. Seems to be working for me.

Error:

PDO Error 1.1: Array ( [0] => xxx[1] => yyy[2] => Lost connection to MySQL server during query )

Fix:

(PDO::setAttribute("PDO::MYSQL_ATTR_USE_BUFFERED_QUERY", true);)

<?phptry  {$dbh= newPDO('mysql:host=xxx;port=xxx;dbname=xxx','xxx','xxx', array(PDO::ATTR_PERSISTENT=>false));$stmt=$dbh->prepare("CALL getname()");// call the stored procedure$stmt->execute();// fetch all rows into an array.$rows=$stmt->fetchAll();

foreach ($rowsas$rs)

{$id=$rs['id'];

}//initilise the statementunset($stmt);$stmt=$dbh->prepare("call secondprocedure(?);");$stmt->bindValue(1,$id);

if ( !$stmt->execute() )

{

echo"PDO Error 1.1:\n";print_r($stmt->errorInfo());

exit;

}

unset($stmt);

} catch (PDOException $e) {

print"Error!: ".$e->getMessage() ."
";

die();

}?>

[#10]

marty at excudo dot net [2007-10-17 05:13:22]

Davey wrote:

> To use "PDO::MYSQL_ATTR_USE_BUFFERED_QUERY" you should call

> PDO::setAttribute("PDO::MYSQL_ATTR_USE_BUFFERED_QUERY", true);

>

>It will not work when passed into PDO::prepare()

>

> - Davey

Almost correct. It should be:

PDO::setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);

so, without the quotes.

otherwise it still won't work :)

[#11]

konrads dot smelkovs at gmail dot com [2007-07-18 03:39:35]

A note for the eager:

There is no way how to get returned row count from an executed prepared statement without fetching the rows.

[#12]

davey at php dot net [2007-06-06 13:46:51]

To use "PDO::MYSQL_ATTR_USE_BUFFERED_QUERY" you should call

PDO::setAttribute("PDO::MYSQL_ATTR_USE_BUFFERED_QUERY", true);

It will not work when passed into PDO::prepare()

- Davey

[#13]

dibakar at talash dot net [2006-09-11 23:31:46]

PDO is much better option for calling procedures, views or triggers of mysql 5.x versions from PHP instead of using mysqli extension. Following is a simple demo script which can  help anybody on how to call and use mysql procedures through php

try {

$dbh = new PDO('mysql:host=xxx;port=xxx;dbname=xxx', 'xxx', 'xxx', array( PDO::ATTR_PERSISTENT => false));

$stmt = $dbh->prepare("CALL getname()");

// call the stored procedure

$stmt->execute();

echo "outputting...
";

while ($rs = $stmt->fetch(PDO::FETCH_OBJ)) {

echo "output: ".$rs->name."
";

}

echo "".date("r")."";

} catch (PDOException $e) {

print "Error!: " . $e->getMessage() . "
";

die();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值