PHP‎ > ‎

PHP Recordsets Using PDO

If using an ODBC connection:

dbh = new PDO("odbc:Labworksdb") ;
$rs = $dbh->prepare("SELECT top 2 custid, orderid FROM orderinfo");
$rs->execute();
while( $row = $rs->fetch(PDO::FETCH_ASSOC) )
echo $row['custid']."<br>";
echo $row['orderid']."<br>";

NOTE:   in IIS PHP Manager you have to enable the PHP_PDO_ODBC.dll PHP extension


If not using an ODBC connection and connecting to Microsoft SQL Server:

$conn = new PDO( "sqlsrv:Server=server\sqlexpress;Database=yourdatabase", "userid", "password"); 
$rs = $conn->prepare("SELECT top 2 ponumber, customerid FROM orders");
$rs->execute();
while( $row = $rs->fetch(PDO::FETCH_ASSOC) )
echo $row['ponumber']."<br>";
echo $row['customerid']."<br>";

If you are connecting to mysql:

$conn = new PDO( "mysql:host=localhost;dbname=yourdatabase", "youruser", "yourpassword");



IF YOU ARE DOING AN INSERT OR DELETE STATMENT:

$conn = new PDO( "mysql:host=localhost;dbname=yourdatabase", "youruser", "yourpassword");
$strsql="insert into Files (description) values ('DJ44')";
$rs = $conn->prepare($strsql);
$rs->execute();




NOTE:   in IIS PHP Manager you have to enable the PHP_PDO_SQLSRV.dll PHP extension


IMPORTANT NOTE:   you cannot use "select * from table" in your queries.   You have to spell out fieldnames, like this:

"select field1, field2, field3 from table".





Comments