2012-09-19 60 views
0

我需要將其轉換爲一個類的PDO形式,但由於我是所有這些的新手,有人可以幫助我嗎?我的教授聲稱每個功能都可以轉換,但是我找不到任何可以幫助我的功能。我想將一個MySQL查詢轉換爲PDO?

<?php 
    // rnfunctions.php 
    $dbhost = 'localhost'; 
    $dbname = 'social_local'; 
    $dbuser = 'root'; 
    $dbpass = 'root'; 
    $appname = "Social Networking Test Site"; 
    try 
    { 
     $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass); 
    } 
    catch (PDOException $e) 
    { 
     echo $e->getMessage(); 
    } 

    function createTable ($name, $query) 
    { 
     if (tableExists($name)) { 
      echo "Table '$name' already exists<br />"; 
     } else { 
      queryMysql("CREATE TABLE $name($query)"); 
      echo "Table '$name' created<br />"; 
     } 
    } 

    function tableExists ($name) 
    { 
     $result = queryMysql("SHOW TABLES LIKE '$name'"); 
     return mysql_num_rows($result); 
    } 

    function queryMysql ($query) 
    { 
     $result = mysql_query($query) or die(mysql_error()); 
     return $result; 
    } 

    function destroySession() 
    { 
     $_SESSION = array(); 
     if (session_id() != "" || isset($_COOKIE[session_name()])) { 
      setcookie(session_name(), '', time() - 2592000, '/'); 
     } 
     session_destroy(); 
    } 

    function sanitizeString ($var) 
    { 
     $var = strip_tags($var); 
     $var = htmlentities($var); 
     $var = stripslashes($var); 
     return $var; 
    } 

    function showProfile ($user) 
    { 
     if (file_exists("$user.jpg")) { 
      echo "<img src='$user.jpg' border='1' align='left' />"; 
     } 
     $result = queryMysql("SELECT * FROM rnprofiles WHERE user='$user'"); 
     if (mysql_num_rows($result)) { 
      $row = mysql_fetch_row($result); 
      echo stripslashes($row[1]) . "<br clear=left /><br />"; 
     } 
    } 
    ?> 

回答

0

像這樣的東西可以幫助你:

$dbh = new PDO("mysql:host=localhost;dbname=yourBaseName", 'someLogin', 'somePassword'); 
$dbh->prepare('SELECT login FROM members WHERE login=? AND password=?'); 
$stm = $dbh->execute(array($login, $pass)); 
if (($row = $stm->fetchObject())) { 
    echo "You are loggued!"; 
    // stuffs with loggued user 
} else { 
    echo "Invalid user or password"; 
    // login form again 
} 
+0

,謝謝你的這種感受。幫助我超越文檔! –

1

您需要先創建一個PDO類。然後學習PDO功能。

$dbh = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS); 
$stmt = $dbh->prepare($sql); 
$stmt->execute(); 
return $stmt; 

使用類似$stmt->FetchAll()得到的結果。

+0

我已經這樣做了,我需要幫助的是如何在單一功能轉化爲PDO的形式?我的教授說這是可以做到的,但是我在網上找的所有資源根本不能幫到我 –

+0

@EliasBaez你知道OOP嗎? –

+0

剛剛開始學習這個學期所以,不是真的 –