2013-04-11 34 views
1

我是mysqli的新手,我寫了一個函數如下。mysqli和提取數據

1 -我不能爲SELECT *查詢找到一種方式,並具有bind_result每一列的值同名變量分配。 (#row存儲到$name例如name列值)

我認爲bind_result()有一個SELECT *查詢中沒有功能?

2 -所以我嘗試了另一個選項,獲取所有行並通過循環手動將它們分配給適當的變量。我想我應該用$query->fetch_all()$query->fetch_assoc()的循環,但我有這種遭遇:

Fatal error: Call to undefined method mysqli_result::fetch_all()

Fatal error: Call to undefined method mysqli_result::fetch_assoc()

但是我做了phpinfo()只見啓用mysqlnd和PHP的版本是5.4 .7(運行XAMPP v1.8.1)

and 3-最後我確實也是不起作用的想法。

function the_names($name) 
{ 
    global $db; 
    if($query = $db->prepare("SELECT * FROM users where name=?")) 
    { 
     $query->bind_param('s', $name); 
     if($query->execute()) 
     { 
      $query->store_result(); 
      if($query->num_rows > 1) 
      { 
       while($row = $query->fetch()) 
       { 
        echo $row['name']; // Here is the problem 
       } 
      } 
      else 
       echo "not valid"; 
      $query->close(); 
     } 
    } 
} 

我需要一種方法來存儲所有獲取的數據作爲什麼bind_result()呢,還是讓他們爲以後使用數組,這是更好的瞭解兩者。 TNX

回答

1

一個字來回答你所有的問題一次 - PDO
它擁有你想從mysqli的(白白)的一切:

function the_names($name) 
{ 
    global $db; 
    $query = $db->prepare("SELECT * FROM users where name=?"); 
    $query->execute(array($name)); 
    return $query->fetchAll(); 
} 

$names = the_names('Joe'); 
foreach ($names as $row) { 
    echo $row['name']; 
} 

注意使用功能的正確方法。應該從來沒有呼應什麼,但只返回數據爲將來使用

+1

那麼創建了什麼mysqli! – revo 2013-04-11 15:34:31

+0

哦,是的,意圖都很好。但實施變成了一場噩夢。所以你可以從你面臨的問題中看出來。 – 2013-04-11 15:44:31

+0

很好的回答,我正在開關! @revo如果它回答了您的問題,那麼接受一個答案(帶有該複選標記)是一種很好的做法 – michaellindahl 2013-08-20 03:01:02

0

如果您mysqli的代碼沒有binding_param()你可以只寫代碼象下面這樣:

$mysqli = new mysqli("localhost" , "root" , "" , "database_name"); 
$result = $mysqli->query("SELECT * FROM users where name=" . $name) ; 
while ($row = $result->fetch_assoc()) { 
    echo $row["name"]; 
} 

如果使用binding_param()代碼,您還需要設置bind_result()

$db = new mysqli("localhost" , "root" , "" , "database_name"); 
function the_names($name){ 
    global $db; 


    /* Prepared statement, stage 1: prepare */ 
    if (!($query = $db->prepare("SELECT * FROM users where name=?"))) { # prepare sql 
    echo "Prepare failed: (" . $db->errno . ") " . $db->error; 
    } 


    /* Prepared statement, stage 2: bind and execute */ 
    if (!$query->bind_param("s", $name)) { # giving param to "?" in prepare sql 
     echo "Binding parameters failed: (" . $query->errno . ") " . $query->error; 
    } 

    if (!$query->execute()) { 
     echo "Execute failed: (" . $query->errno . ") " . $query->error; 
    } 


    $query->store_result(); # store result so we can count it below... 
    if($query->num_rows > 0){ # if data more than 0 [ that also mean "if not empty" ] 

     # Declare the output field of database 
     $out_id = NULL; 
     $out_name = NULL; 
     $out_age = NULL; 
     if (!$query->bind_result($out_id, $out_name , $out_age)) { 
      /* 
      * Blind result should same with your database table ! 
      * Example : my database 
      * -users 
      * id ( 11  int) 
      * name (255 string) 
      * age ( 11  int) 
      * then the blind_result() code is : bind_result($out_id, $out_name , $out_age) 
      */ 
      echo "Binding output parameters failed: (" . $query->errno . ") " . $query->error; 
     } 

     while ($query->fetch()) { 
      # print the out field 
      printf("id = %s <br /> name = %s <br /> age = %s <br />", $out_id, $out_name , $out_age); 
     } 

    }else{ 
     echo "not valid"; 
    } 

} 
the_names("panji asmara"); 

參考:

http://php.net/manual/en/mysqli.quickstart.prepared-statements.php