1

The following code gives me a fatal error:call to undefined function mysql_connect. My php version is 5.4.20. php.ini file has extension to php_mysql.dll and the php_mysql.dll file is physically present. what do i need to do?

<?php
    $connection=mysql_connect('localhost','php','foo')or die('no connection made');
?>
upinair
  • 11
  • 1
  • [Fatal error: Call to undefined function mysql_connect() in C:\Apache\htdocs\test.php on line 2](http://stackoverflow.com/questions/5179535/fatal-error-call-to-undefined-function-mysql-connect-in-c-apache-htdocs-test) , [Fatal error: Call to undefined function mysql_connect()](http://stackoverflow.com/questions/10615436/fatal-error-call-to-undefined-function-mysql-connect) , [Fatal error: Call to undefined function mysql_connect()](http://stackoverflow.com/questions/12783589/fatal-error-call-to-undefined-function-mysql-connect) – Uours Oct 13 '13 at 08:48

3 Answers3

0

I would just like to say that connecting like that is Deprecated. Please connect like this and you should have no error if your mysql works:

function db()
{
   try
   {
      $db = new PDO('mysql:host=localhost; dbname=test; charset=utf8', 'username', 'password', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
   }

   catch (Exception $e)
   {
       die('Error : ' . $e->getMessage());
   }
   return $db;
}
sanchixx
  • 265
  • 2
  • 5
  • 12
0

For Fatel Error you are facing :-

Please verify from phpinfo() whether mysql is running correctly or not. If you made any changes in Php.ini then you should restart apache server.

Suggestion :-

Please avoid use of mysql_* functions. You can use mysqli .

mysql_connect is deprecated and it will be completely removed from PHP 5.5

How you can write your DB connection in Mysqli are :-

<?php
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');

/*
 * This is the "official" OO way to do it,
 * BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
 */
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
        . $mysqli->connect_error);
}

/*
 * Use this instead of $connect_error if you need to ensure
 * compatibility with PHP versions prior to 5.2.9 and 5.3.0.
 */
if (mysqli_connect_error()) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
        . mysqli_connect_error());
}

echo 'Success... ' . $mysqli->host_info . "\n";

$mysqli->close();
?>

You can also refer below links:-

http://www.pontikis.net/blog/how-to-use-php-improved-mysqli-extension-and-why-you-should http://www.a2hosting.com/kb/developer-corner/mysql/connect-to-mysql-using-php

Roopendra
  • 7,490
  • 16
  • 62
  • 87
0

Uncomment the line extension=php_mysql.dll in your php.ini file and restart Apache.

Additionally, libmysql.dll file must be available to Apache, i.e., it must be either in available in Windows systems PATH or in Apache working directory.

See more about installing MySQL extension in manual.

P.S. I would advise to consider MySQL extension as deprecated and to use MySQLi or even PDO for working with databases (I prefer PDO).

Chewpers
  • 2,372
  • 4
  • 22
  • 30