简体   繁体   中英

php not connecting to mysql database

so I installed WAMP and a mysql database (not using the one that comes with wamp). I can connect from MySql Workbench to the server but not from the php page. They are both on the same machine and the script looks like this:

<?php

/**
 * A class file to connect to database
 */
class DB_CONNECT
{

    // constructor
    function __construct()
    {
        // connecting to database
        $this->connect();
    }

    // destructor
    function __destruct()
    {
        // closing db connection
        $this->close();
    }

    /**
     * Function to connect with database
     */
    function connect()
    {
        // Connecting to mysql database
        $con = mysql_connect("127.0.0.1:3306", "userMan", "passDoor") or die(mysql_error());

        // Selecing database
        $db = mysql_select_db("testdb") or die(mysql_error());

        // returing connection cursor
        return $con;
    }

    /**
     * Function to close db connection
     */
    function close()
    {
        // closing db connection
        mysql_close();
    }
}

?>

This row:

$con = mysql_connect("127.0.0.1:3306", "userMan", "passDoor") or die(mysql_error());

Tells me:

Fatal error: Call to undefined function mysql_connect() in C:\\wamp\\www\\website\\db_connect.php on line 32

It worked before but now "suddenly" it stopped working, any thoughts?

UPDATE:

I tried to use mysqli instead since mysql_* is deprecated and so I did this:

I find hte php.ini being used from phpinfo() and make sure these are uncommented:

extension=php_mysql.dll
extension=php_mysqli.dll

I also tried this with commenting php_mysql.dll,

I add this to my php script to see if it works:

if (!function_exists('mysqli_init') && !extension_loaded('mysqli'))
        {
            echo 'We don\'t have mysqli!!!';
        }
        else
        {
            echo 'Phew we have it!';
        }

and my result is "We don't have mysqli!!!".

I am running wamp on windows 8.1, php is version 5.4.12 and I have also checked that the extension_dir in php.ini is correct.

What may I be doing wrong?

Your mysql driver is not loaded. Uncomment extension=php_mysql.dll in php.ini . Also I suggest you to use mysqli or PDO . You are using deprecated one.

In order to find php.ini location, you can use php --ini in commandline

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM