php - can't connect to db in my sql -
i using mamp on mac connect localhost ,i can connect server problem cant connect db, don't know in part of query making mistake since 1 line query.i searched different solutions here on stack overflow couldnt helpful
$username = 'root'; $password = 'root'; $host = 'localhost'; $database = 'trendnow'; $link = mysqli_connect($host, $username, $password); if (!$link) { echo 'can not connect server'; } $db_selected = mysqli_select_db($database,$link); echo $db_selected; if (!$db_selected) { echo 'can not connect trend '; }
felippe duarte correct. database trying connect to?
mysqli_connect("localhost","my_user","my_password","my_db");
- localhost means connect server program running on. people connect database located on server not local, ip address.
- my_db name of database. if database called webstore, may have table located on database called, computers. webstore name of database , must reside in last section, so:
mysqli_connect("localhost","my_user","my_password","webstore");
here tutorial may help. http://www.w3schools.com/php/func_mysqli_connect.asp
getting useful error message
the problem stand alone connection if there error, won't tell is. thankfully there few ways find out error is.
<?php $con = mysqli_connect("localhost","my_user","my_password","my_db"); //this check if able start connection using above line if (mysqli_connect_errno()) { echo "failed connect mysql: " . mysqli_connect_error(); } // example query. check if failed, , if so, why failed // perform query, check error if (!mysqli_query($con,"insert persons (firstname) values ('glenn')")) { echo("error description: " . mysqli_error($con)); } ?>
Comments
Post a Comment