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,030
  • 21
  • 100
  • 124
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 stands with Ukraine
  • 25,310
  • 18
  • 114
  • 149
Naftali
  • 142,114
  • 39
  • 237
  • 299
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,558
  • 10
  • 12
2

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

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Bere
  • 1,522
  • 2
  • 14
  • 20
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,756
  • 2
  • 22
  • 35