简体   繁体   中英

Can't connect with MySQL database, getting error “Fatal error: Class 'mysql_connect' not found in…”

I am learning the core PHP and write the below code for testing the connection with the database. I get this error:

Fatal error: Class 'mysql_connect' not found in C:\\xampp\\htdocs\\demo\\index.php on line 4"

The code is below:

<?php
    $dbcon = new mysql_connect("localhost", "root", "");
    mysql_select_db("demo", $dbcon);

    $query = mysql_query("select name FROM test ");
    echo mysql_num_row($query);
    mysql_close($dbcon);
?>

You cannot do new mysql_connect , mysql_connect is a function and not a Class.


Also please, don't use mysql_* functions in new code . They are no longer maintained and are officially deprecated . See the red box ? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial .

mysql_connect is not a class, you should drop the "new". See the documentation: http://fr.php.net/manual/en/function.mysql-connect.php

Also, mysql_ functions are deprecated.

Remove the 'new' keyword. That will be enough.

Change the below:

<?php
$dbcon = mysql_connect("localhost","root",""); <-- remove "new"
mysql_select_db("demo", $dbcon);

$query = mysql_query("select name FROM test ");
echo mysql_num_row($query);
mysql_close($dbcon);
?>

Also, you should use PDO or MySQLi instead of mysql_* as it is now deprecated.

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