3

i'm using MAMP on mac os This is the error while making database connection

Fatal error: Uncaught Error: Call to undefined function mysql_connect() in /Applications/MAMP/htdocs/databases.php:3 Stack trace: #0 {main} thrown in /Applications/MAMP/htdocs/databases.php on line 3

Code used

<?php 

$connection = mysql_connect("localhost","root","");

if(!$connection)
{

    die("Database connection failed" . mysql_error());
}
?>
Om Komawar
  • 251
  • 3
  • 19
  • 1
    Fortunately you are using a version of PHP that this bad function is deprecated and removed. Time to learn new things. – Royal Bg Jan 01 '16 at 10:35
  • So where can i find that new information.. I'm a newbie it might help if you could provide certain resources @RoyalBg. Thanks in advance. – Om Komawar Jan 01 '16 at 10:37
  • 1
    Here you are : http://php.net/manual/en/mysqlinfo.api.choosing.php – Royal Bg Jan 01 '16 at 10:39
  • Thanks @RoyalBg That really helped me. – Om Komawar Jan 01 '16 at 10:43
  • check Output of phpinfo() – devpro Jan 01 '16 at 10:53
  • 1
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jan 01 '16 at 13:16

3 Answers3

5

just change from

$connection = mysql_connect("localhost","root","");

to

$connection = mysqli_connect("localhost","root","");

notice the 'i' in mysqli

Omar N Shamali
  • 503
  • 5
  • 11
3

If you happen to use PHP 7, then it is time to learn something new, as mysql_* functions have been deprecated since a while, and are now removed from PHP 7 as seen in this RFC.

If you are unsure about which version you are using, you can call phpinfo(); and this will show your current PHP version.

Alternatives:

codez
  • 1,440
  • 2
  • 19
  • 34
0

Try this code

<?php  

   $database = mysql_connect("hostname","username","password");
   mysql_select_db("databasename",$database);

?>
Divyesh
  • 329
  • 3
  • 17
  • no need to use password, he is using php 7, mysql_query is not available in php 7 – devpro Jan 01 '16 at 11:11
  • Why should the OP "try this"? A ***good answer*** will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO. – Jay Blanchard Jan 01 '16 at 13:17