如何将一组简单的PHP指令转换为PDO功能



我想创建一个PDO函数,我可以在其中传递一个变量并在下面显示结果。

我要通过的变量称为 $range

我想通过:

调用该功能
$range=$row["part_number"];
function get_range($range);

然后获取下面的结果显示:

<?php 
$range = "gmb3-30";
include ("order/connection.php");
// --------------------- Connect to the table-------------------------
$stmt = $pd->prepare('SELECT * FROM mytable WHERE part_number = :part_number');
$stmt->execute(array(
    ':part_number' => $range
));
$row = $stmt->fetch(PDO::FETCH_BOTH);
echo " <select name=select>";
$c = $row["price_each1"]; //  price for single item
for ($i = 1; $i <= 8; $i++)
    {
    $b = $row["price_each" . $i];
    if ($b != 0.00)
        {
        $d = (($c - $b) / $c) * 100;
        $complete = $row[("price_break" . $i) ] . " ," . round($d) . "%";
        echo "<option> ";
        echo $complete . "</option>";
        }
    }
echo “ < / select > ”;
?>

创建这样的类并包括:

<?php
class Database{
// Define configuration
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;
// Declare a new variables at the top of your class for the Database Handler and any errors.
private $dbh;
private $error;
private $stmt;
public function __construct(){
    // Set DSN
    $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname.';charset=utf8;';
    // Set options
    $options = array(
        PDO::ATTR_PERSISTENT => true, 
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    );
    try {
        $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
    }
    // Catch any errors
    catch (PDOException $e) {
        $this->error = $e->getMessage();
    }
}
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
public function bind($param, $value, $type = null){
if (is_null($type)) {
    switch (true) {
        case is_int($value):
            $type = PDO::PARAM_INT;
            break;
        case is_bool($value):
            $type = PDO::PARAM_BOOL;
            break;
        case is_null($value):
            $type = PDO::PARAM_NULL;
            break;
        default:
            $type = PDO::PARAM_STR;
    }
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute(){
return $this->stmt->execute();
}
public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
public function simpleSingle(){
$this->execute();
//$this->stmt->fetch(PDO::FETCH_ASSOC);
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
public function rowCount(){
return $this->stmt->rowCount();
}
public function lastInsertId(){
return $this->dbh->lastInsertId();
}
public function beginTransaction(){
return $this->dbh->beginTransaction();
}
public function endTransaction(){
return $this->dbh->commit();
}
public function cancelTransaction(){
return $this->dbh->rollBack();
}
public function debugDumpParams(){
return $this->stmt->debugDumpParams();
}
}

然后用此代码替换您的代码:

function get_range($range) {
    $database = null;
    $database = new Database();
    $database->query("SELECT * FROM mytable WHERE part_number = :part_number;");
    $database->bind(':part_number', $range);
    $row = $database->resulset();
    if (!$row) {
       // Do whatever needs to be done when you get no results
        die();
    } else {
     echo " <select name=select>";
     $i = 0;
     foreach ($row as $option) {
        if ($i <= 8){
         $b=$option["price_each"];
         if ($b !=0.00)
            {
             $d=(($c-$b)/$c)*100;
             $complete=$option[("price_break"]. " ," .round($d)."%";
             echo "<option> ";
             echo  $complete ."</option>";
          }
          $i = $i + 1;
        }
    }
     echo “</select>”;
    }
}

最后,调用函数:

$range=$row["part_number"]; 
function get_range($range);

最新更新