Does mysql_connect() showing call to undefined function error?
All MySQL related extensions were deprecated in PHP 5.5.0, and it was removed in
PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. If you are
still using MySQL extensions like mysql_connect
, mysql_affected_rows
, mysql_fetch_array
,
mysql_fetch_assoc
, mysql_num_rows
etc etc now it is the time to change your
code from mylsq to mysqli otherwise it will show deprecated error in PHP 5.5
onwards.
The following functions show how to connect to MySQL database and retrieve data
from database using MySQLi.
<?php $host = "Your Host Name"; $user = "Your Username"; $pass = "Your Password"; $dbname = "Your Database Name" function db_connect() { static $connection; if(!isset($connection)) { $connection = mysqli_connect($host,$user,$pass,$dbname); } if($connection === false) { return mysqli_connect_error(); } return $connection; } ?>
<?php function db_query($query) { $connection = db_connect(); $result = mysqli_query($connection,$query); return $result; } ?>
<?php $host = "Your Host Name"; $user = "Your Username"; $pass = "Your Password"; $dbname = "Your Database Name" function db_connect() { static $connection; if(!isset($connection)) { $connection = mysqli_connect($host,$user,$pass,$dbname); } if($connection === false) { return mysqli_connect_error(); } return $connection; } function db_query($query) { $connection = db_connect(); $result = mysqli_query($connection,$query); return $result; } function db_error() { $connection = db_connect(); return mysqli_error($connection); } $connect = db_connect(); ?>
Find | Replace With |
mysql_query() | db_query() |
mysql_fetch_array | mysqli_fetch_array |
mysql_fetch_assoc | mysqli_fetch_assoc |
mysql_num_rows | mysqli_num_rows |
mysql_fetch_row | mysqli_fetch_row |
mysql_insert_id | mysqli_insert_id(db_connect()) |
mysql_real_escape_string() | $connect->real_escape_string() |