1

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);
?>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chhatrapati Sharma
  • 605
  • 4
  • 9
  • 18
  • who has told you this "new mysql_connect" ? Basicly removing 'new' will solve your current problem, but mysql_ extension is not recommended to be used anymore. – Royal Bg Aug 12 '13 at 13:28

4 Answers4

8

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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Naftali
  • 144,921
  • 39
  • 244
  • 303
2

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.

Antoine Augusti
  • 1,598
  • 11
  • 13
2

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bere
  • 1,627
  • 2
  • 16
  • 22
2

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.

Scott Helme
  • 4,786
  • 2
  • 23
  • 35