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);
?>
1
  • 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. Commented Aug 12, 2013 at 13:28

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

4 Comments

thanks scott, now getting this "Call to undefined function mysql_num_row() in .." error
It's mysql_num_rows() see here: php.net/manual/en/function.mysql-num-rows.php
You really should look to migrate to MySQLi or PDO, it's not too hard. Here is the equivalent MySQLi page: php.net/manual/en/mysqli-result.num-rows.php
@ChhatrapatiSharma Why you don't read the docs before asking?
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.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.