deprecated

This question already has an answer here:

  • Why shouldn't I use mysql_* functions in PHP? 14 answers

  • According to the PHP Manual, you should use any of the following:

  • mysqli_stmt_num_rows()
  • PDOStatement::rowCount()
  • To be clear though, neither of these is a mere substitute for mysql_num_rows() . Your code must eventually be rewritten entirely to use the MySQLi or PDO API in lieu of mysql_*() .


    Personally, I now use the MySQL Improved extension.

    If you choose to use it in the procedural way it can be used in a very similar manner to how you're currently using the old MySQL extension.

    Example (MySQL):

    $result = mysql_query($query);
    
    if (!$result) die ("Database access failed: " . mysql_error());
    
    $rows = mysql_num_rows($result);
    

    Example (MySQL Improved):

    $result = mysqli_query($query);
    
    if (!$result) die ("Database access failed: " . mysqli_error());
    
    $rows = mysqli_num_rows($result);
    

    However, I use MySQL Improved in an object orientated manner.

    More information can be found here: http://www.php.net/manual/en/book.mysqli.php


    If you understand the idea of database abstraction libraries, use safemysql

    $data = $db->getAll($query,$param1,$param2);
    $rows = count($data);
    

    If using raw API functions is more familiar to you, use PDO

    $stm = $pdo->prepare($query);
    $stm->execute(array($param1,$param2));
    $data = $stm->fetchAll();
    $rows = count($data);
    

    Note 2 important things:

  • requested data already stored in the $data variable.
  • every dynamical query part (ie inserted variables) have to be inserted via placeholder
  • 链接地址: http://www.djcxy.com/p/26586.html

    上一篇: 连接在PHP 5.6 +

    下一篇: 弃用