Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php+mysql Fatal error: Call to undefined function mysql_connect() in

Tags:

php

mysql

I have searched the internet for troubleshooting with this "Call to undefined function mysql_connect() " error but none of the suggested procedures works...

When I try to access mysql from php, I get this error. I have PHP Version 5.2.17 and MySQL 5.1.68 (mysql is running outside php, I tried creating tables and databases etc.).

I uncomment the extensions in php.ini:

extension=php_mysql.dll
extension=php_mysqli.dll

and specified the path to these extensions.

I also added both PHP and MySQL to my PATH variable. I am using Apache 2.2. and I restarted the server after every change that I made.

This is my code in php for accessing the database (but I suppose that the problem is not in syntax):

<?php     
      $dbhost = 'localhost';
      $dbuser = 'root';
      $dbpass = 'pasw';
      $dbname = 'db';

      $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
      mysql_select_db($dbname);
?>

I am out of ideas how to fix this, can u suggest some other troubleshooting tips? I am quite new to his problematics... I would like to use MySQL, cause thats what I will use on the actual server when I´ll have my website up and running. Thanks in advance!

EDIT: this is from apache log:

[Mon Mar 25 13:50:42 2013] [error] [client 127.0.0.1] PHP Fatal error:  Call to undefined function mysql_connect() in C:\\Web\\thenemis\\dbconnect.php on line 7, referer: http://localhost/thenemis/admin.php
[Mon Mar 25 13:50:42 2013] [error] [client 127.0.0.1] File does not exist: C:/Web/favicon.ico

EDIT2: this is from php info (I think that the problem may be with some unspecified parametres in php.ini file). I havent read anywhere that I should specify some other features than extensions and path to them but maybe I was wrong... what do you suggest?

mysql

MySQL Support   enabled
Active Persistent Links 0
Active Links    0
Client API version  5.0.51a

Directive   Local Value Master Value
mysql.allow_persistent  On  On
mysql.connect_timeout   60  60
mysql.default_host  no value    no value
mysql.default_password  no value    no value
mysql.default_port  no value    no value
mysql.default_socket    no value    no value
mysql.default_user  no value    no value
mysql.max_links Unlimited   Unlimited
mysql.max_persistent    Unlimited   Unlimited
mysql.trace_mode    Off Off
like image 706
Smajl Avatar asked Mar 25 '13 12:03

Smajl


People also ask

What is Call to undefined function mysql_connect ()?

If you get an error like Fatal error: Call to undefined function mysql_connect() when trying to install GFI HelpDesk, it probably means that MySQL support has not been enabled for PHP on your server (that is, the PHP module php-mysql has not been installed).

How do I fix undefined errors in PHP?

As mentioned, this is related to the functions included in PHP version your host installed on your server. Thus, the solution for this is contacting your hosting provider and asking the host to re-/enable the ctype_xdigit function for your website.

What does the mysql_connect () function return?

Description. Returns a MySQL link identifier on success, or FALSE on failure. mysql_connect() establishes a connection to a MySQL server. The following defaults are assumed for missing optional parameters: server = 'localhost:3306', username = name of the user that owns the server process and password = empty password.

Does PHP 7 support mysql_connect?

This extension was deprecated in PHP 5.5. 0, and it was removed in PHP 7.0.


2 Answers

As it was already said use phpinfo() for determining if mysql is running correctly. In general you should not use mysql anymore more but rather the new improved version of mysql, called mysqli http://php.net/manual/de/book.mysqli.php

You can in addition programmatically check if a function exists with method_exists method

like image 142
Chris Avatar answered Oct 26 '22 23:10

Chris


Try to use this:

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'pasw';
$dbname = 'db';

<?php
// Create connection
$con=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);

// Check connection
if (mysqli_connect_errno($con))
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>

MySQL support will be removed of PHP, so start to use MySQLi or PDO instead.

:)

like image 38
Matheus Lopes Avatar answered Oct 27 '22 01:10

Matheus Lopes