Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql_ deprecated [duplicate]

Tags:

php

mysql

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);
like image 212
Brobina Avatar asked Mar 08 '13 12:03

Brobina


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

like image 107
2 revs, 2 users 67% Avatar answered Nov 07 '22 06:11

2 revs, 2 users 67%


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

like image 24
EM-Creations Avatar answered Nov 07 '22 06:11

EM-Creations