11

I have been told that the mysql_ extension is now deprecated in the current version of PHP and will be removed at some point.

What should I use instead of this and how?

For nearly all my queries I use it.

For example:

$result = mysql_query($query);

if (!$result) die ("Database access failed: " . mysql_error());

$rows = mysql_num_rows($result);
EM-Creations
  • 4,195
  • 4
  • 40
  • 56
Brobina
  • 467
  • 6
  • 20

6 Answers6

8

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

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_*().

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
John Woo
  • 258,903
  • 69
  • 498
  • 492
1

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

EM-Creations
  • 4,195
  • 4
  • 40
  • 56
1

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 (i.e. inserted variables) have to be inserted via placeholder
Community
  • 1
  • 1
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

Via php.net/mysql_num_rows manual

mysql_num_rows

This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:

hsz
  • 148,279
  • 62
  • 259
  • 315
0

As at the offical mysql_num_rows function description mentioned you might use Mysqli or PDO_MYSQL as an alternative.

simplyray
  • 1,200
  • 1
  • 16
  • 25
0

don't use mysql_* functions in new code.

They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi -this article will help you decide which.

If you choose PDO, here is a good tutorial

Community
  • 1
  • 1
Nirav Ranpara
  • 13,753
  • 3
  • 39
  • 54