2015-07-20 148 views
1

從MYSQL切換到MYSQLi時出現問題。這些代碼可以在MYSQL下正常工作,但是當我將連接更改爲MYSQLi時,我在獲取我的查詢時收到上述錯誤。我如何使用mysqli函數獲取我的查詢?致命錯誤:調用未定義的方法mysqli_result :: rowCount()

代碼:

 function __construct(){ 
    $this->link = mysqli_connect('localhost', 'root', '', 'ajax_rating'); 

    if (!$this->link) { 
    die('Connect Error (' . mysqli_connect_errno() . ') ' 
     . mysqli_connect_error()); 
    } 

    echo 'Success... ' . mysqli_get_host_info($this->link) . "\n"; 

       } 

    function getItems($id = null){ 
     if(isset($_GET['id'])) 
     { 
      $query = $this->link->query("SELECT * FROM items WHERE id = '$id'"); 
     } 
     else 
     { 
      $query = $this->link->query("SELECT * FROM items"); 
     } 
     $rowCount = $query->rowCount(); 
     if($rowCount >= 1) 
     { 
      $result = $query->fetchAll(); 
     } 
     else 
     { 
      $result = 0; 
     } 
     return $result; 

回答

4

使用num_rows

MySQLi row_count

所以最終的答案是

function getItems($id = null) 
{ 
    if(isset($_GET['id'])) 
    { 
     $query = $this->link->query("SELECT * FROM items WHERE id = '$id'"); 
    } 
    else 
    { 
     $query = $this->link->query("SELECT * FROM items"); 
    } 
    $rowCount = $query->num_rows;//change here 
    if($rowCount >= 1) 
    { 
     $result = $query->fetchAll(); 
    } 
    else 
    { 
     $result = 0; 
    } 
    return $result; 
} 

編輯01

使用代替

mysqli_fetch_allfetchAll()

mysqli_fetch_all($query,MYSQLI_ASSOC); 

這樣回答世界是

if($rowCount >= 1) 
    { 
     $result = mysqli_fetch_all($query,MYSQLI_ASSOC); 
    } 
+0

毫米我早些時候嘗試過,但它給了我「調用未定義的方法mysqli_result :: fetchAll()」我研究了aro和似乎我需要某種本地驅動程序? – suppko

+1

該方法名稱更改。現在它被稱爲'mysqli_result :: fetch_all()'。結果集對象上的所有方法都記錄在這裏:http://php.net/manual/en/class.mysqli-result.php – staticsan

2

您可以使用NUM_ROWS在DB計數行,你可以直接呼叫連接這樣的: -

//for connection 

$con = mysqli_connect("localhost", "root", "", "ajax_rating"); 
if(!$con) 
{ 
echo "connection error"; 
} 


//query 
function getItems($id = null) 
{ 
if(isset($_GET['id'])) 
{ 
    $query = mysqli_query($con,"SELECT * FROM items WHERE id = '$id'"); 
} 
else 
{ 
    $query = mysqli_query($con,"SELECT * FROM items"); 
} 
if (mysqli_num_rows($query) > 0)//change here 
    { 
    while($row = mysqli_fetch_assoc($query)) 
    { 
     $result=$row; 
    } 

} 
else 
{ 
    $result = 0; 
} 
return $result; 
} 
相關問題