Undefined function mysql_connect()

2019-01-01 04:02发布

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.

13条回答
后来的你喜欢了谁
2楼-- · 2019-01-01 04:25

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

查看更多
零度萤火
3楼-- · 2019-01-01 04:26

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

查看更多
柔情千种
4楼-- · 2019-01-01 04:29

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().

查看更多
人气声优
5楼-- · 2019-01-01 04:30

In php.ini file

change this

;extension=php_mysql.dll

into

extension=php_mysql.dll
查看更多
旧时光的记忆
6楼-- · 2019-01-01 04:32

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
查看更多
倾城一夜雪
7楼-- · 2019-01-01 04:33

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 .

查看更多
登录 后发表回答