请如何在我的代码中将mysql查询转换为pdo? [英] please How to convert mysql query to pdo in my code?
本文介绍了请如何在我的代码中将mysql查询转换为pdo?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在mySQLquery中将代码PHP转换为唯一的产品ID函数
my code PHP in mySQLquery to unique product id function
function kode($tabel, $inisial){
$struktur = mysql_query("SELECT * FROM $tabel");
$field = mysql_field_name($struktur,0);
$panjang = mysql_field_len($struktur,0);
$qry = mysql_query("SELECT MAX(".$field.") FROM ".$tabel);
$row = mysql_fetch_array($qry);
if ($row[0]=="") {
$angka=0;
}
else {
$angka = substr($row[0], strlen($inisial));
}
$angka++;
$angka =strval($angka);
$tmp ="";
for($i=1; $i<=($panjang-strlen($inisial)-strlen($angka)); $i++) {
$tmp=$tmp."0";
}
return $inisial.$tmp.$angka; }
如何将此代码转换为php pdo?请帮助我.
how to convert this code to php pdo? please help me..
推荐答案
以下是我用于PDO的标准代码段:
Here is a standard snippet I use for PDO:
try
{
$dbh = new PDO("mysql:host=xxxxxxxxxxxx;dbname=streaming", "xxxx", "xxxx");
}
catch (Exception $e)
{
throw new Exception( 'Something really gone wrong', 0, $e);
}
$date = reformatDate($_POST['date']);
$sql="SELECT * FROM order_items WHERE date_start = :date";
$sth = $dbh->prepare($sql);
$sth->bindParam(':date', $date, PDO::PARAM_STR, 10);
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
此示例应该为您指明正确的方向,但是您需要进行繁重的工作.
This example should point you in the right direction, but you need to do the heavy lifting.
更新
这也是可以接受的.
$madeupstring = "Mk9285425";
$sql="SELECT * FROM order_items WHERE product = :whatever";
$sth = $dbh->prepare($sql);
$sth->bindParam(':whatever', $madeupstring);
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
您也可以这样做:
$sql="SELECT * FROM order_items WHERE product = 'Mk9285425'";
$sth = $dbh->prepare($sql);
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
这篇关于请如何在我的代码中将mysql查询转换为pdo?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文