Undefined function mysql_connect() [duplicate] - php

This question already has answers here:
Fatal error: Uncaught Error: Call to undefined function mysql_connect()
(9 answers)
Closed 3 months ago.
I have ran aptitude install php5-mysql (and restarted MySQL/Apache 2), but I am still getting this error:
Fatal error: Call to undefined function mysql_connect() in /home/validate.php on line 21
phpinfo() says the /etc/php5/apache2/conf.d/pdo_mysql.ini file has been parsed.

In case, you are using PHP7 already, the formerly deprecated functions mysql_* were removed entirely, so you should update your code using the PDO-functions or mysqli_* functions instead.
If that's not possible, as a workaround, I created a small PHP include file, that recreates the old mysql_* functions with mysqli_*()-functions: fix_mysql.inc.php

I see that you tagged this with Ubuntu. Most likely the MySQL driver (and possibly MySQL) is not installed. Assuming you have SSH or terminal access and sudo permissions, log into the server and run this:
sudo apt-get install mysql-server mysql-client php5-mysql
If the MySQL packages or the php5-mysql package are already installed, this will update them.
UPDATE
Since this answer still gets the occasional click I am going to update it to include PHP 7. PHP 7 requires a different package for MySQL so you will want to use a different argument for the apt-get command.
# Replace 7.4 with your version of PHP
sudo apt-get install mysql-server mysql-common php7.4 php7.4-mysql
And importantly, mysql_connect() has been deprecated since PHP v5.5.0. Refer the official documentation here: PHP: mysql_connect()

Well, this is your chance! It looks like PDO is ready; use that instead.
Try checking to see if the PHP MySQL extension module is being loaded:
<?php
phpinfo();
?>
If it's not there, add the following to the php.ini file:
extension=php_mysql.dll

If someone came here with the problem of docker php official images, type below command inside the docker container.
$ docker-php-ext-install mysql mysqli pdo pdo_mysql
For more information, please refer to the link above How to install more PHP extensions section(But it's a bit difficult for me...).
Or this doc may help you.
https://docs.docker.com/samples/library/php/

I was also stuck with the same problem of undefined MySQL_connect().I tried to make changes in PHP.ini file but it was giving me the same error.
Then I came to this solution where I changed my code from depreciated php functions to new functions.
$con=mysqli_connect($host,$user,$password);
mysqli_select_db($con,dbname);
//To select the database
session_start(); //To start the session
$query=mysqli_query($con,your query);
//made query after establishing connection with database.
I hope this will help you .
This solution is correctly working for me .
EDIT:
If you upgrade form old php you need to apt-get install php7.0-mysql

Try:
<?php
phpinfo();
?>
Run the page and search for mysql. If not found, run the following in the shell and restart the Apache server:
sudo apt-get install mysql-server mysql-client php5-mysql
Also make sure you have all the following lines uncommented somewhere in your apache2.conf (or in your conf.d/php.ini) file, from
;extension=php_mysql.so
to
extension=php_mysql.so

In php.ini file
change this
;extension=php_mysql.dll
into
extension=php_mysql.dll

My guess is your PHP installation wasn't compiled with MySQL support.
Check your configure command (php -i | grep mysql). You should see something like '--with-mysql=shared,/usr'.
You can check for complete instructions at http://php.net/manual/en/mysql.installation.php. Although, I would rather go with the solution proposed by #wanovak.
Still, I think you need MySQL support in order to use PDO.

The question is tagged with ubuntu, but the solution of un-commenting the extension=mysqli.dll is specific to windows. I am confused here?!, anyways, first thing run <? php phpinfo ?> and search for mysql* under Configuration heading. If you don't see such a thing implies you have not installed or enabled php-mysql. So first install php-mysql
sudo apt get install php-mysql
This command will install php-mysql depending on the php you have already installed, so no worries about the version!!.
Then comes the unix specific solution, in the php.ini file un-comment the line
extension=msql.so
verify that msql.so is present in /usr/lib/php/<timestamp_folder>,
ELSE
extension=path/to/msql.so
Then finally restart the apache and mysql services, and you should now see the mysql section under Configrations heading in phpinfo page

I was getting this error because the project I was working on was developed on php 5.6 and after install, the project was unable to run on php7.1.
Just for anyone that uses Vagrant with ubuntu/nginx, in the nginx directory(/etc/nginx/), there is a directory named "sites-available" which contains a file named like the url configured for the vagrant maschine. In my case was homestead.app. Within this file there is a line that says something like
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
There you can change the php version to the desired for that particular site.
Googled this but wasnt really able to find a simple answer that said where to look and what to change.
Hope that this helps anyone.
Thanks.

If you are getting the error as
Fatal error: Call to undefined function mysql_connect()
Kindly login to the cPanel >> Click on Select Php version >> select the extension MYSQL

For CentOS 7.8 & PHP 7.3
yum install rh-php73-php-mysqlnd
And then restart apache/php.

(Windows mysql config)
Step 1 : Go To Apache Control Panel > Apache > Config > PHP.ini
Step 2 : Search in Notepad (Ctrl+F) For: ;extension_dir = "" (could be commented with a ;). Replace this line with: extension_dir = "C:\php\ext" (please do not you need to remove the ; on the beginning of the sentence).
Step 3 : Search For: extension=php_mysql.dll and remove the ; in the beginning.
Step 4 : Save and Restart You Apache HTTP Server. (On Windows this usually done via a UI)
That's it :)
If you get errors about missing php_mysql.dll you'll probably need to download this file from either the php.net site or the pecl.php.net. (Please be causius about where you get it from)
More info on PHP: Installation of extensions on Windows - Manual

There must be some syntax error. Copy/paste this code and see if it works:
<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect:' . mysql_error());
}
echo 'Connected successfully';
?
I had the same error message. It turns out I was using the msql_connect() function instead of mysql_connect().

Related

What other way can I use to enable mysqli extension in php 8? [duplicate]

For 2 days now I'm trying to solve this, but unfortunately no result. Let me tell you my story about the problem. I've bulid an application on a site, and the application deals with the reviews. But, I'm trying to put it on another site, and I copyed the php files, the sql file from the old site, and moved them to the new site (they are on different FTP servers). When I'm trying to go to the pages from the application, I receive this FATAL ERROR:
Fatal error: Call to undefined function mysqli_connect()
The code that I wrote to connect to the database is this (with hidden credentials):
$con = mysqli_connect("","*the_name*","*the_pass*","*the_database*");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Why do I get the error? It works on the old server, and the code I think it's not the problem, because it works on localhost, and on the new server it doesn't. Can anyone help me?
Simply do it
sudo apt install php-mysqli
It works perfectly and it is version independent
On Windows, the extension extension=php_mysqli.dll can be enabled by removing the semicolon ';' at the beginning of the extension name from the php.ini file.
For CentOS, do:
sudo yum install php-mysqli
On Ubuntu I had to install php5 mysql extension:
apt-get install php5-mysql
Happens when php extensions are not being used by default. In your php.ini file, change ;extension=php_mysql.dll to extension=php_mysql.dll. **If this error logs, then add path to this dll file, eg extension=C:\Php\php-???-nts-Win32-VC11-x86\ext\php_mysql.dllDo same for php_mysqli.dll and php_pdo_mysql.dll. Save and run your code again.
If you have the module installed and set your PHP.INI file properly, check your apache error log for something like the following:
PHP Warning: PHP Startup: Unable to load dynamic library 'C:\\php\\ext\\php_mysqli.dll' - The specified module could not be found.
In this case, your extension directory is not what you think it is. You may neeed to set it explicitly, like so:
extension_dir="C:\xampp\php\ext"
Mine was a bit different for php7 on centos7.
In /etc/php.ini
; extension=mysqli
to
extension=mysqli
On Raspberry Pi I had to install php5 mysql extension.
apt-get install php5-mysql
After installing the client, the webserver should be restarted. In case you're using apache, the following should work:
sudo service apache2 restart
If you host the server yourself, and that server happens to be Apache, you can also get this error even if you have uncommented extension=php_mysqli.dll in php.ini.
You also need to tell Apache where to find php.ini by using the PHPIniDir directive in Apache's httpd.conf
AddHandler application/x-httpd-php .php
PHPIniDir "<path-to-folder-where-php-ini-lives>"
if you use ubuntu 16.04 (maybe and above),you have this module already but not enabled by default. Just do this:
sudo phpenmod mysqli
There is no error in the, but the mysqli PHP extension is not installed on your machine. Please contact your service provider to fix this issue.
Mysqli isn't installed on the new server. Run phpinfo() to confirm.
<?php
phpinfo();
So this may not be the issue for you, but I was struggling with this error. I discovered what was causing my problem, though I can't really explain as to why.
For me, mysqli_connect was working fine where the connection was made on pages in any various sub-directory. For some reason though, the same code referenced on pages in the root directory was returning this error. The strange thing is that it was working fine on my localhost environment in MAMP in the root directory, however on my shared host it was not.
After struggling to figure out what was giving me "Error 500" white screen from this "PHP Fatal Error," I went through the code and stumbled upon this code in the error handling that was suggested by the PHP Manual (https://www.php.net/manual/en/mysqli.error.php).
if (!mysqli_query($link, "SET a=1")) {
printf("Error message: %s\n", mysqli_error($link));
}
I randomly decided to remove it and, voila, connection to the database working in root directories. Maybe someone smarter than me can explain this, for anyone struggling with a similar issue.
I followed below link to fix this problem. Make sure to enable following Module.
php -r 'phpinfo();' | grep -i mysqli
Link : https://askubuntu.com/questions/773601/php-mysqli-extension-in-ubuntu-16-04-not-working-after-upgrade-to-version-7-0-6

PHP: Call to undefined function mysqli_connect() - On WINDOWS [duplicate]

For 2 days now I'm trying to solve this, but unfortunately no result. Let me tell you my story about the problem. I've bulid an application on a site, and the application deals with the reviews. But, I'm trying to put it on another site, and I copyed the php files, the sql file from the old site, and moved them to the new site (they are on different FTP servers). When I'm trying to go to the pages from the application, I receive this FATAL ERROR:
Fatal error: Call to undefined function mysqli_connect()
The code that I wrote to connect to the database is this (with hidden credentials):
$con = mysqli_connect("","*the_name*","*the_pass*","*the_database*");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Why do I get the error? It works on the old server, and the code I think it's not the problem, because it works on localhost, and on the new server it doesn't. Can anyone help me?
Simply do it
sudo apt install php-mysqli
It works perfectly and it is version independent
On Windows, the extension extension=php_mysqli.dll can be enabled by removing the semicolon ';' at the beginning of the extension name from the php.ini file.
For CentOS, do:
sudo yum install php-mysqli
On Ubuntu I had to install php5 mysql extension:
apt-get install php5-mysql
Happens when php extensions are not being used by default. In your php.ini file, change ;extension=php_mysql.dll to extension=php_mysql.dll. **If this error logs, then add path to this dll file, eg extension=C:\Php\php-???-nts-Win32-VC11-x86\ext\php_mysql.dllDo same for php_mysqli.dll and php_pdo_mysql.dll. Save and run your code again.
If you have the module installed and set your PHP.INI file properly, check your apache error log for something like the following:
PHP Warning: PHP Startup: Unable to load dynamic library 'C:\\php\\ext\\php_mysqli.dll' - The specified module could not be found.
In this case, your extension directory is not what you think it is. You may neeed to set it explicitly, like so:
extension_dir="C:\xampp\php\ext"
Mine was a bit different for php7 on centos7.
In /etc/php.ini
; extension=mysqli
to
extension=mysqli
On Raspberry Pi I had to install php5 mysql extension.
apt-get install php5-mysql
After installing the client, the webserver should be restarted. In case you're using apache, the following should work:
sudo service apache2 restart
If you host the server yourself, and that server happens to be Apache, you can also get this error even if you have uncommented extension=php_mysqli.dll in php.ini.
You also need to tell Apache where to find php.ini by using the PHPIniDir directive in Apache's httpd.conf
AddHandler application/x-httpd-php .php
PHPIniDir "<path-to-folder-where-php-ini-lives>"
if you use ubuntu 16.04 (maybe and above),you have this module already but not enabled by default. Just do this:
sudo phpenmod mysqli
There is no error in the, but the mysqli PHP extension is not installed on your machine. Please contact your service provider to fix this issue.
Mysqli isn't installed on the new server. Run phpinfo() to confirm.
<?php
phpinfo();
So this may not be the issue for you, but I was struggling with this error. I discovered what was causing my problem, though I can't really explain as to why.
For me, mysqli_connect was working fine where the connection was made on pages in any various sub-directory. For some reason though, the same code referenced on pages in the root directory was returning this error. The strange thing is that it was working fine on my localhost environment in MAMP in the root directory, however on my shared host it was not.
After struggling to figure out what was giving me "Error 500" white screen from this "PHP Fatal Error," I went through the code and stumbled upon this code in the error handling that was suggested by the PHP Manual (https://www.php.net/manual/en/mysqli.error.php).
if (!mysqli_query($link, "SET a=1")) {
printf("Error message: %s\n", mysqli_error($link));
}
I randomly decided to remove it and, voila, connection to the database working in root directories. Maybe someone smarter than me can explain this, for anyone struggling with a similar issue.
I followed below link to fix this problem. Make sure to enable following Module.
php -r 'phpinfo();' | grep -i mysqli
Link : https://askubuntu.com/questions/773601/php-mysqli-extension-in-ubuntu-16-04-not-working-after-upgrade-to-version-7-0-6

Fatal error: Call to undefined function mysqli_connect() in /home/galaxy/public_html/writersPortal/php/connect.php on line 9 [duplicate]

For 2 days now I'm trying to solve this, but unfortunately no result. Let me tell you my story about the problem. I've bulid an application on a site, and the application deals with the reviews. But, I'm trying to put it on another site, and I copyed the php files, the sql file from the old site, and moved them to the new site (they are on different FTP servers). When I'm trying to go to the pages from the application, I receive this FATAL ERROR:
Fatal error: Call to undefined function mysqli_connect()
The code that I wrote to connect to the database is this (with hidden credentials):
$con = mysqli_connect("","*the_name*","*the_pass*","*the_database*");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Why do I get the error? It works on the old server, and the code I think it's not the problem, because it works on localhost, and on the new server it doesn't. Can anyone help me?
Simply do it
sudo apt install php-mysqli
It works perfectly and it is version independent
On Windows, the extension extension=php_mysqli.dll can be enabled by removing the semicolon ';' at the beginning of the extension name from the php.ini file.
For CentOS, do:
sudo yum install php-mysqli
On Ubuntu I had to install php5 mysql extension:
apt-get install php5-mysql
Happens when php extensions are not being used by default. In your php.ini file, change ;extension=php_mysql.dll to extension=php_mysql.dll. **If this error logs, then add path to this dll file, eg extension=C:\Php\php-???-nts-Win32-VC11-x86\ext\php_mysql.dllDo same for php_mysqli.dll and php_pdo_mysql.dll. Save and run your code again.
If you have the module installed and set your PHP.INI file properly, check your apache error log for something like the following:
PHP Warning: PHP Startup: Unable to load dynamic library 'C:\\php\\ext\\php_mysqli.dll' - The specified module could not be found.
In this case, your extension directory is not what you think it is. You may neeed to set it explicitly, like so:
extension_dir="C:\xampp\php\ext"
Mine was a bit different for php7 on centos7.
In /etc/php.ini
; extension=mysqli
to
extension=mysqli
On Raspberry Pi I had to install php5 mysql extension.
apt-get install php5-mysql
After installing the client, the webserver should be restarted. In case you're using apache, the following should work:
sudo service apache2 restart
If you host the server yourself, and that server happens to be Apache, you can also get this error even if you have uncommented extension=php_mysqli.dll in php.ini.
You also need to tell Apache where to find php.ini by using the PHPIniDir directive in Apache's httpd.conf
AddHandler application/x-httpd-php .php
PHPIniDir "<path-to-folder-where-php-ini-lives>"
if you use ubuntu 16.04 (maybe and above),you have this module already but not enabled by default. Just do this:
sudo phpenmod mysqli
There is no error in the, but the mysqli PHP extension is not installed on your machine. Please contact your service provider to fix this issue.
Mysqli isn't installed on the new server. Run phpinfo() to confirm.
<?php
phpinfo();
So this may not be the issue for you, but I was struggling with this error. I discovered what was causing my problem, though I can't really explain as to why.
For me, mysqli_connect was working fine where the connection was made on pages in any various sub-directory. For some reason though, the same code referenced on pages in the root directory was returning this error. The strange thing is that it was working fine on my localhost environment in MAMP in the root directory, however on my shared host it was not.
After struggling to figure out what was giving me "Error 500" white screen from this "PHP Fatal Error," I went through the code and stumbled upon this code in the error handling that was suggested by the PHP Manual (https://www.php.net/manual/en/mysqli.error.php).
if (!mysqli_query($link, "SET a=1")) {
printf("Error message: %s\n", mysqli_error($link));
}
I randomly decided to remove it and, voila, connection to the database working in root directories. Maybe someone smarter than me can explain this, for anyone struggling with a similar issue.
I followed below link to fix this problem. Make sure to enable following Module.
php -r 'phpinfo();' | grep -i mysqli
Link : https://askubuntu.com/questions/773601/php-mysqli-extension-in-ubuntu-16-04-not-working-after-upgrade-to-version-7-0-6

Getting an error Fatal error: Call to undefined function mysqli_connect()

First off I would like to mention that I have used the search option and looked at multiple different posts as to why I have this problem yet it is still occurring.
In my PHPInfo() I have mySQL installed. In my PHP.ini I have it in the correct directory as per my PHPInfo and I also removed the semicolon in front of extension=php_mysql.so.
I also have the extensions pointing to my extension folder with all the various modules. After all this I still have this error.
Are there any other ways I can go about attempting to fix this issue?
I think you have missing the mysqli extension. You need to installed first sqli extension than you can use its functions..
Please have a look on the link to configured of all version of mysqli.
http://php.net/manual/en/mysqli.installation.php
Contact to your server administrator or do:
sudo apt-get install php5-mysql
Will install package containing mysqli and mysql, so afterwards all you need to do is restart apache and it should work.
sudo service apache2 restart

Cannot call an sql connection from php

(Running ubuntu server 12.04)
This line in particular is giving me issues in php:
$con = mysql_connect("localhost:3306","root","root");
From turning on error reporting, I only got this returned (as it was killed at that line and nothing afterwards would print):
Fatal error: Call to undefined function mysql_connect() in /var/www/sql/login.php on line 7
I'm not sure where to go from there, as it is just killed at the line I provided. I have used just "localhost" as well, and I have successfully logged in from the command line. Could this be a php permissions issue, or something like configuration? If so, where would that be?
I think this happen because you don't have the mysql extension loaded on your php environment.
To fix this, in your ubuntu open the shell and type this:
sudo apt-get install php5-mysql libapache2-mod-auth-mysql
After that, just restart apache with the next command:
sudo /etc/init.d/apache2 restart
Good luck!
Your version of PHP was compiled without MySQL support. You need to recompile it, following the setup instructions for including MySQL.
The php_mysql library is not installed on your server. Try to take a look at the output of phpinfo() if it is really missing.

Categories