【问题标题】:counting rows and passing information with mysqli使用 mysqli 计算行数和传递信息
【发布时间】:2009-02-28 17:21:08
【问题描述】:

我一直在尝试将 php 页面转换为 mysqli,但遇到了一些问题。鉴于下面的代码,以及我订购的工作方式,我想知道使用 mysqli 方法的更好方法是什么。

是否有 mysqli 替代 mysql_num_rows 或者是计算所需行数的不同方法?

我将如何使用 mysqli 执行以下操作?:

$data = mysql_query($countQuery) or die(mysql_error());
$rowcount = mysql_num_rows($data);

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;
        }
    }
}

【问题讨论】:

  • 我建议使用 mysql,因为 mysqli 有问题。
  • @dusoft,您为什么建议使用旧的、不再处于活动开发中、本质上是过程而非面向对象且不支持准备好的语句的库?如果您要推荐反对 mysqli,那么至少选择另一个实际上更好的库。 :-(

标签: php mysql mysqli


【解决方案1】:

我通常在执行后直接使用它:

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

【讨论】:

    【解决方案2】:

    其实,回答第一个问题:

    $data = mysql_query($countQuery) or die(mysql_error());
    $rowcount = mysql_num_rows($data);
    

    如果您想使用 mysqli 执行此操作,则 mysqli_num_rows 可以正常工作。唯一的区别是,请记住将连接参数放在查询中。

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

    【讨论】:

      【解决方案3】:

      我已经使用正确的属性/函数调用重写了您的代码示例,以解决您在问题中提到的两个问题:

      <?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(至少从 5.1 开始,它是 PHP 原生的),从那以后我们没有遇到任何问题。我强烈建议您查看 PDO 或 Pear 库之一,以实际提供您的数据库连接接口。

      【讨论】:

      • 啊,非常感谢。只是一个简单的问题,但是您看到 $countquery 失败的任何原因吗?我收到一个不正确的语法错误,但是当我手动输入查询时,它可以正常工作。
      • 另外,为什么全局变量一定不好,至少在我的例子中是这样的?
      【解决方案4】:

      你可以使用:

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

      您可以查看文档以获取更多信息 num_rowsfetch_assoc

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-23
        • 2015-07-15
        相关资源
        最近更新 更多