2014-10-29 153 views
0

我有很多需要轉換爲PDO的腳本。我的腳本在mysqli_中,我一直在網上閱讀不同的方式來做同樣的事情。有人能解釋一下這些差異嗎?如何正確地將PHP腳本轉換爲PDO?

再次,對不起,如果我要求簡單的東西,但在線文件混淆了我。

在此先感謝。

我原來的PHP腳本mysqli_:

<?php 
include 'db_conn_pdo.php'; 

//preparing query 


$desk_query = "SELECT 
           coordinate_id, 
           x_coord, 
           y_coord, 
           section_name, 
           station_number, 
           ver_hor 
          FROM coordinates"; 


$station_query = "SELECT 
            coordinate_id, 
           section_name, 
           station_number, 
           x_coord, 
           y_coord, 
           username, 
           hostname 
           FROM 
           sandbox_maesc4.coordinates c 
           INNER JOIN 
           softphone_materials_updater.workstations w 
           ON w.pod = c.station_number 
           INNER JOIN 
           sandbox_maesc4.workstation_userlogged wsu 
           ON w.ws = wsu.hostname 
           WHERE wsu.lastupdate >= CURRENT_TIMESTAMP - INTERVAL 10 MINUTEs";  
/**************************/ 

$desk_stmt = $dbh->query($desk_query); 

while($row = $desk_stmt -> fetch(PDO::FETCH_ASSOC)){ 
       $id  = $row['coordinate_id']; 
       $x_pos = $row['x_coord']; 
       $y_pos = $row['y_coord']; 
       $sec_name = $row['section_name']; 
       $sta_num = $row['station_number']; 
       $position = $row['ver_hor']; 

       $class = 'desk_box_ver'; 
       if($position == 'horizontal'){ 
        $class = 'desk_box_hor'; 
       } 
        echo '<div class="' . $class . '" data-rel="' . $id . '" style="left:' . $x_pos . 'px;top:' . $y_pos.'px;">' . $sta_num . '</div>' . "\n"; 
      } 

$station_stmt = $dbh->query($station_query); 

while($row = $station_stmt -> fetch(PDO::FETCH_ASSOC)){ 
$id  = $row['coordinate_id']; 
    $sec_name = $row['section_name']; 
    $sta_num = $row['station_number']; 
    $x_pos = $row['x_coord']; 
    $y_pos = $row['y_coord']; 

echo '<div class="station_info_" id="station_info_' . $id . '" style="left:' . $x_pos . 'px;top:' . $y_pos . 'px;"><p class="numb">Section:' . $sec_name . '<br>Station:' . $sta_num . '<br></p></div>' . "\n"; 
}//end while 
?> 

是什麼在PDO這兩者之間的區別:

$station_stmt = $dbh->query($station_query); 
//and 
$station_stmt = self::$tdb->prepare($station_query); 

,我怎麼能使用mysqli_這兩條線檢查我的查詢在PDO中是否有效

$station_result = mysqli_query($conn,$station_query); 

if($station_result === false) { 
    die(mysqli_error()); 
} 

對於mysqli_ while循環PDO我用這個:

while($row = $station_stmt -> fetch(PDO::FETCH_ASSOC)) 

有沒有這樣做它的另一種方式?

最後,在腳本的頂部是否包含連接,如mysqli_中的一個好主意?

謝謝

+0

我不能回答你的questions.But我剛去海槽PDO此YouTube教程。很好。如果您對https://www.youtube.com/playlist感興趣?list = PLfdtiltiRHWF5Rhuk7k4UAU1_yLAZzhWc – EL3PHANTEN 2014-10-29 13:51:26

+2

反問題:你真的把'mysqli_'與PDO混合了嗎? – 2014-10-29 13:54:14

+0

@ Fred-ii-不,我不是,我在純mysqli_中發佈了我的原始腳本,並在其下面使用了我在PDO中使用的腳本。我得到一些錯誤 – mario 2014-10-29 13:55:56

回答

1

What is the difference between these two in PDO:

$station_stmt = $dbh->query($station_query); $station_stmt = self::$tdb->prepare($station_query);

第一種是直的查詢,其不備(如:SELECT * FROM station)。

第二個正在準備(如:SELECT * FROM desk WHERE id = ?

僅當查詢沒有參數時才使用query()

當您想要綁定val時使用prepare() UE能夠查詢

and how can I use these two lines in mysqli_ to check if my query is good in PDO

$station_result = mysqli_query($conn,$station_query); if($station_result === false) { die(mysqli_error()); }

它是一回事,是否query()回報false

$station_stmt = $dbh->query($station_query); 
if (!$station_stmt) { 
    echo "\nPDO::errorInfo():\n"; 
    print_r($dbh->errorInfo()); 
    die(); 
} 

For mysqli_ while loop in PDO I used this: while($row = $station_stmt -> fetch(PDO::FETCH_ASSOC))

PDO提供fetchAll(),你可以用它在一次獲取所有行:

$row = $station_stmt -> fetchAll(PDO::FETCH_ASSOC); 


MINUTE概不 s

CURRENT_TIMESTAMP - INTERVAL 10 MINUTE 

試試這個方法:

$dsn = 'mysql:dbname=testdb;host=127.0.0.1'; 
$user = 'dbuser'; 
$password = 'dbpass'; 

try { 
    $dbh = new PDO($dsn, $user, $password); 
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

    if(!$dbh){ 
     echo "\nPDO::errorInfo():\n"; 
     print_r($dbh->errorInfo()); 
     die(); 
    } 

    get_desk_coordinates($dbh); 
    get_station_coordinates($dbh); 


} catch (PDOException $e) { 
    echo 'Connection failed: ' . $e->getMessage(); 
} 


/** 
* @param $dbh 
*/ 
function get_desk_coordinates($dbh) 
{ 
    $desk_query = "SELECT 
        coordinate_id, 
        x_coord, 
        y_coord, 
        section_name, 
        station_number, 
        ver_hor 
       FROM coordinates"; 

    $desk_stmt = $dbh->query($desk_query); 

    while ($row = $desk_stmt->fetch(PDO::FETCH_ASSOC)) { 
     $id = $row['coordinate_id']; 
     $x_pos = $row['x_coord']; 
     $y_pos = $row['y_coord']; 
     $sec_name = $row['section_name']; 
     $sta_num = $row['station_number']; 
     $position = $row['ver_hor']; 

     $class = 'desk_box_ver'; 
     if ($position == 'horizontal') { 
      $class = 'desk_box_hor'; 
     } 
     echo '<div class="' . $class . '" data-rel="' . $id . '" style="left:' . $x_pos . 'px;top:' . $y_pos . 'px;">' . $sta_num . '</div>' . "\n"; 
    } 
} 



/** 
* @param $dbh 
*/ 
function get_station_coordinates($dbh) 
{ 
    $station_query = " SELECT 
        coordinate_id, 
        section_name, 
        station_number, 
        x_coord, 
        y_coord, 
        username, 
        hostname 
        FROM sandbox_maesc4.coordinates c 
        INNER JOIN 
        softphone_materials_updater.workstations w 
        ON w.pod = c.station_number 
        INNER JOIN 
        sandbox_maesc4.workstation_userlogged wsu 
        ON w.ws = wsu.hostname 
        WHERE wsu.lastupdate >= CURRENT_TIMESTAMP - INTERVAL 10 MINUTE"; 

    $station_stmt = $dbh->query($station_query); 

    while ($row = $station_stmt->fetch(PDO::FETCH_ASSOC)) { 
     $id = $row['coordinate_id']; 
     $sec_name = $row['section_name']; 
     $sta_num = $row['station_number']; 
     $x_pos = $row['x_coord']; 
     $y_pos = $row['y_coord']; 

     echo '<div class="station_info_" id="station_info_' . $id . '" style="left:' . $x_pos . 'px;top:' . $y_pos . 'px;"><p class="numb">Section:' . $sec_name . '<br>Station:' . $sta_num . '<br></p></div>' . "\n"; 
    } 
    //end while 
} 
+0

真棒非常感謝您的時間和幫助解釋這一點。它幫助我發揮了重要作用,我將詳細瞭解PDO。再次感謝 – mario 2014-10-29 16:43:42

+1

你做出了正確的選擇,PDO遠遠優越並有據可查,享受 – meda 2014-10-29 16:48:49