In PHP, what are the differences between using mysql_num_rows with PHP 4 and PHP 5, and how can developers adapt their code to work with PHP 5?
In PHP 4, developers used mysql_num_rows() to get the number of rows in a result set, but in PHP 5, this function was deprecated in favor of using mysqli_num_rows() or PDOStatement::rowCount(). To adapt code to work with PHP 5, developers should update their code to use mysqli or PDO for database interactions.
// PHP 4 code using mysql_num_rows
$result = mysql_query("SELECT * FROM table");
$num_rows = mysql_num_rows($result);
// PHP 5 code using mysqli_num_rows
$conn = new mysqli($host, $user, $password, $database);
$result = $conn->query("SELECT * FROM table");
$num_rows = $result->num_rows;
// PHP 5 code using PDOStatement::rowCount()
$conn = new PDO("mysql:host=$host;dbname=$database", $user, $password);
$stmt = $conn->query("SELECT * FROM table");
$num_rows = $stmt->rowCount();
Keywords
Related Questions
- What is the difference between using a variable function and call_user_func in PHP?
- How can the use of different types of quotation marks affect the execution of SQL queries in PHP?
- Are there any standard ports or protocols that should be followed when implementing a chat feature using sockets in PHP?