2009-02-28 51 views
0

我一直在嘗試將一個PHP頁面轉換爲mysqli,並且引起了一些問題。鑑於下面的代碼,以及我已經命令的東西的工作方式,我想知道什麼是更好的方式使用mysqli方法。計數行和傳遞信息與mysqli

是否有一個mysqli替代mysql_num_rows或是計算所需行數的不同方法?

我會怎麼做以下使用的mysqli ?:

​​

什麼是mysql_fetch_assoc替代?我覺得我不應該使用我正在使用的當前行方法,即使有替換函數,那麼正確的方法是什麼?

我對這些問題表示歉意,但至今我一直無法確定答案。

<?php 
$con = mysqli_connect("localhost", "user", "", "ebay"); 
if (!$con) { 
    echo "Can't connect to MySQL Server. Errorcode: %s\n". mysqli_connect_error(); 
    exit; 
} 
$con->query("SET NAMES 'utf8'"); 
$cmd = "word"; 
//normally retrieved from GET 
if($cmd=="deleterec") { 
    $deleteQuery = "DELETE FROM AUCTIONS1 WHERE ARTICLE_NO = ?"; 
    if ($delRecord = $con->prepare($deleteQuery)) { 
     $delRecord->bind_param("s", $pk); 
     $delRecord->execute(); 
    } 
} 
$table = 'AUCTIONS'; 
$brand = "test"; 
$countQuery = "SELECT ARTICLE_NO FROM ? WHERE upper(ARTICLE_NAME) LIKE '% ? %'"; 
if ($numRecords = $con->prepare($countQuery)) { 
    $numRecords->bind_param("ss", $table, $brand); 
    $numRecords->execute(); 
    $data = $con->query($countQuery) or die(print_r($con->error)); 
    $rowcount = mysql_num_rows($data); 
    $rows = getRowsByArticleSearch($query, $table, $max); 
    $last = ceil($rowcount/$page_rows); 
} 
$self = htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'utf-8'); 
foreach ($rows as $row) // print table rows { 
    echo '<tr>' . "\n"; 
    echo '<td><a href="#" onclick="doThings(\'Layer2\', \'' . $pk . '\')">'.$row['USERNAME'].'</a></td>' . "\n"; 
    // repeated for each column 
} 
function getRowsByArticleSearch($searchString, $table, $max) { 
    global $con; 
    $recordsQuery = "SELECT ARTICLE_NO, USERNAME, ACCESSSTARTS, ARTICLE_NAME, date_format(str_to_date(ACCESSSTARTS, '%d/%m/%Y %k:%i:%s'), '%d %m %Y') AS shortDate FROM ? WHERE upper(ARTICLE_NAME) LIKE '%?%' ORDER BY str_to_date(ACCESSSTARTS, '%d/%m/%Y %k:%i:%s')" . $max; 
    if ($getRecords = $con->prepare($recordsQuery)) { 
     $getRecords->bind_param("ss", $searchString, $table); 
     $getRecords->execute(); 
     $getRecords->bind_result($ARTICLE_NO, $USERNAME, $ACCESSSTARTS, $ARTICLE_NAME, $shortDate); 
     while ($getRecords->fetch()) { 
      $result = $con->query($recordsQuery); 
      $rows = array(); 
      while($row = mysql_fetch_assoc($result)) { 
       $rows[] = $row; 
      } 
      return $rows; 
     } 
    } 
} 
+0

我會建議留在mysql作爲mysqli是越野車。 – dusoft 2009-02-28 17:32:44

+0

@dusoft,爲什麼你建議使用一箇舊的庫,不再是活躍的開發者,程序性的本體而不是面向對象的,並且不支持預處理語句? 如果你打算推薦mysqli,那麼至少選擇另一個更好的庫。 :-( – 2009-02-28 18:40:09

回答

0

我已經重寫與正確的屬性/功能您的代碼示例調用來解決,你在這個問題提兩個問題:

<?php 
$con = mysqli::connect("localhost", "user", "", "ebay"); 

if (!$con) { 
    echo "Can't connect to MySQL Server. Errorcode: %s\n". mysqli_connect_error(); 
    exit; 
} 

$con->query("SET NAMES 'utf8'"); 
$cmd = "word"; 

//normally retrieved from GET 
if($cmd=="deleterec") { 
    $deleteQuery = "DELETE FROM AUCTIONS1 WHERE ARTICLE_NO = ?"; 
    if ($delRecord = $con->prepare($deleteQuery)) { 
     $delRecord->bind_param("s", $pk); 
     $delRecord->execute(); 
    } 
} 

$table = 'AUCTIONS'; 
$brand = "test"; 
$countQuery = "SELECT ARTICLE_NO FROM ? WHERE upper(ARTICLE_NAME) LIKE '% ? %'"; 

if ($numRecords = $con->prepare($countQuery)) { 
    $numRecords->bind_param("ss", $table, $brand); 
    $numRecords->execute(); 
    $data = $con->query($countQuery) or die(print_r($con->error)); 

    // Here is the property that you can reference to determine the affected rows from the previous query. -- gabriel 
    $rowcount = $data->num_rows; 
    $rows = getRowsByArticleSearch($query, $table, $max); 
    $last = ceil($rowcount/$page_rows); 
} 

$self = htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'utf-8'); 

foreach ($rows as $row) // print table rows { 
    echo '<tr>' . "\n"; 
    echo '<td><a href="#" onclick="doThings(\'Layer2\', \'' . $pk . '\')">'.$row['USERNAME'].'</a></td>' . "\n"; 
    // repeated for each column 
} 

function getRowsByArticleSearch($searchString, $table, $max) { 
    //global $con; Globals are BAD!! Please don't use. 
    $con = mysqli::connet("localhost", "user", "", "ebay"); 

    $recordsQuery = "SELECT ARTICLE_NO, USERNAME, ACCESSSTARTS, ARTICLE_NAME, date_format(str_to_date(ACCESSSTARTS, '%d/%m/%Y %k:%i:%s'), '%d %m %Y') AS shortDate FROM ? WHERE upper(ARTICLE_NAME) LIKE '%?%' ORDER BY str_to_date(ACCESSSTARTS, '%d/%m/%Y %k:%i:%s')" . $max; 
    if ($getRecords = $con->prepare($recordsQuery)) { 
     $getRecords->bind_param("ss", $searchString, $table); 
     $getRecords->execute(); 
     $getRecords->bind_result($ARTICLE_NO, $USERNAME, $ACCESSSTARTS, $ARTICLE_NAME, $shortDate); 
     while ($getRecords->fetch()) { 
       $result = $con->query($recordsQuery); 
       $rows = array(); 

       // Notice the adjusted function call to retrieve the associate array for each record within the result set. -- gabriel 
       while($row = $result->fetch_assoc()) { 
         $rows[] = $row; 
       } 
       return $rows; 
     } 
    } 
} 

但是,我想補充一點,dusoft在評估中是正確的,MySQLi是有問題的,並且已知會產生問題。正是由於這個原因,我們不得不從MySQLi切換到PDO(從原生PHP到至少5.1),並且我們從此沒有任何問題。我強烈建議您查看PDO或者其中一個Pear庫,以實際提供您的數據庫連接接口。

0

您可以使用:

$data->num_rows(); 
$data->fetch_assoc(); 

您可以檢查出文檔的更多信息num_rowsfetch_assoc

1

我通常後直接使用我的執行:

$query->execute(); 
// store the result first 
$query->store_result(); 
$rows = $query->num_rows; 
1

其實,要回答第一個問題:

​​

如果你想做到這一點使用的mysqli,mysqli_num_rows工作得很好。唯一的區別是,請記住將連接參數放入查詢中。

$data = mysqli_query($dbc,$countQuery) or die(mysql_error());