2

I am getting this error Fatal error: Call to undefined function mysql_connect() in /var/www/html/test1.php on line 8 for the following code:

<?php
$host = "localhost";
$user = "user12";
$pass = "34klq*";
ini_set ('display_errors', 1);
$r = mysql_connect($host, $user, $pass);
if (!$r) {
echo "Could not connect to server\n";
trigger_error(mysql_error(), E_USER_ERROR);
} else {
echo "Connection established\n";
}
echo mysql_get_server_info() . "\n";
mysql_close();
?>

I have PHP, MYSQL, php-Mysql installed in my system:

[root@localhost ~]# rpm -qa | grep mysql
php-mysql-5.3.3-3.el6_2.8.i686
mysql-devel-5.1.61-4.el6.i686
mysql-server-5.1.61-4.el6.i686
mysql-bench-5.1.61-4.el6.i686
mysql-libs-5.1.61-4.el6.i686
mysql-5.1.61-4.el6.i686

[root@localhost ~]# rpm -qa | grep php 
php-common-5.3.3-3.el6_2.8.i686
php-5.3.3-3.el6_2.8.i686
php-mysql-5.3.3-3.el6_2.8.i686
php-cli-5.3.3-3.el6_2.8.i686
php-pdo-5.3.3-3.el6_2.8.i686

output of phpinfo() : http://jsfiddle.net/nit8899/GZ4f7/

Also I have edited the /etc/php.ini file:

extension=mysql.so

But even then I am getting this error.

hakre
  • 193,403
  • 52
  • 435
  • 836
nitxx
  • 61
  • 1
  • 1
  • 5

4 Answers4

2

You should open your php.ini file located in php folder, and uncomment this line of code:

;extension=php_mysql.dll

So it looks like this:

extension=php_mysql.dll

Your php folder location depends on your lampp/wampp installation, if using xampp its located in: xampp/php/php.ini

otporan
  • 1,345
  • 2
  • 12
  • 29
  • I don't have any such line in php.ini. Also I tried to find the file php_mysql.dll, but could not locate it in my system. – nitxx Oct 08 '12 at 14:38
  • If you have php installed you should have php.ini somewhere. In php.ini you can set what extensions will be loaded once the apache server/php starts. Do you have this installed on your local machine or on live server? And as other guys told you, mysql extension is no longer supported, so you should think about going with mysqli or PDO for database! – otporan Oct 08 '12 at 14:48
  • It is on my local virtual machine. I have the php.ini file but I could not find "extension=php_mysql.dll" line in that file. – nitxx Oct 08 '12 at 14:53
  • It should be there, in my php.ini, extensions start at around line 960. And ;extension=php_mysql.dll is on line 972. To me it looks like this should be the problem, PHP throws error like that if u use undefined function or functions from extension that is not loaded. And dont forget to restart apache server once you uncomment extension you need. – otporan Oct 08 '12 at 15:56
1

if you are working on linux [debian]

apt-get install php5-mysql

because your PHP is not able to talk with mysql

0

I cheated and looked on my phone

The build of your PHP spectifically says '--without-mysql'

Therefore, mysql functions will not be available.

You should rebuild.

BugFinder
  • 17,474
  • 4
  • 36
  • 51
  • I have freshly installed php, mysql, php-mysql but still the phpinfo page is coming with '--without-mysql'. Do you have any suggestions to fix that. – nitxx Oct 08 '12 at 14:40
  • remove php, and php-mysql, and just install php-mysql – BugFinder Oct 08 '12 at 14:41
  • If I install only php-mysql, the phpinfo page not getting displayed. – nitxx Oct 08 '12 at 14:50
  • It sounds very confusing - what packaged linux are you using? ubunto? rehat? – BugFinder Oct 08 '12 at 15:06
  • I have solved the problem now, I have installed linux freshly, installed php, mysql, php-mysql and it started working good. Thanks for all the suggestions to solve – nitxx Oct 09 '12 at 08:14
0

Your PHP is configured with --without-mysql.

The documentation describes how to configure with mysql extension enabled. You may have your reasons, but PDO would be a better choice here is the documentation for configuring with PDO mysql.

piddl0r
  • 2,431
  • 2
  • 23
  • 35