简体   繁体   中英

please How to convert mysql query to pdo in my code?

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; }

how to convert this code to php pdo? please help me..

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.

Update

This would also be acceptable.

$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);

You could also do this:

$sql="SELECT * FROM order_items WHERE product = 'Mk9285425'";
$sth = $dbh->prepare($sql);
$sth->execute();

$result = $sth->fetchAll(PDO::FETCH_ASSOC);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM