如何将一组简单的php指令转换为PDO函数

时间:2017-06-27 09:21:04

标签: 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 > ”;
?>

1 个答案:

答案 0 :(得分:0)

创建一个这样的类并包含它:

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