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());
}
?>
just change from
$connection = mysql_connect("localhost","root","");
to
$connection = mysqli_connect("localhost","root","");
notice the 'i' in mysqli
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:
Try this code
<?php
$database = mysql_connect("hostname","username","password");
mysql_select_db("databasename",$database);
?>
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.