Weiss Posted August 21, 2017 Share Posted August 21, 2017 I am lost! please help: Hello all, i'm new to this, i would appriciate someone helping me access my sql with php on localhost. ive installed MAMP, i have created databases in my sql. now im trying to reach it thru the php code: <?php //Step1 $db = mysqli_connect('localhost','root','1234','learnning_over_here') or die('Error connecting to MySQL server.'); ?> <html> <head> </head> <body> <h1>PHP connect to MySQL</h1> <?php //Step2 $query = "SELECT * FROM table_name"; mysqli_query($db, $query) or die('Error querying database.'); ?> </body> </html> i have no idea why but it just woldnt connect. ive been trying all day but.. not working. what am i doing wrong? Link to comment Share on other sites More sharing options...
iwato Posted August 21, 2017 Share Posted August 21, 2017 Place the following code before you make your call to the mysqli_connect( ) function and see if it does not give you a better clue: ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); Roddy Link to comment Share on other sites More sharing options...
justsomeguy Posted August 21, 2017 Share Posted August 21, 2017 Do you see your error messages? You can have it print the actual error message from MySQL: http://php.net/manual/en/mysqli.construct.php Link to comment Share on other sites More sharing options...
dsonesuk Posted August 21, 2017 Share Posted August 21, 2017 (edited) $db = mysqli_connect('localhost','root','1234','learnning_over_here'); tired Edited August 21, 2017 by dsonesuk Link to comment Share on other sites More sharing options...
justsomeguy Posted August 21, 2017 Share Posted August 21, 2017 There's an or statement after that line. Link to comment Share on other sites More sharing options...
Weiss Posted August 22, 2017 Author Share Posted August 22, 2017 First- thank you very much for the help!. this is the code now: <?php //Step1 ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); $db = mysqli_connect('localhost','root','arr4shed','aliens_abduction', 3306) or die('Error connecting to MySQL server.'); ?> <html> <head> </head> <body> <h1>PHP connect to MySQL</h1> <?php //Step2 $query = "SELECT * FROM table_name"; mysqli_query($db, $query) or die('Error querying database.'); ?> </body> </html> this is the result in the browser: PHP connect to MySQL Error querying database. any idea? Link to comment Share on other sites More sharing options...
justsomeguy Posted August 22, 2017 Share Posted August 22, 2017 Instead of printing your own error message, print the actual error message from MySQL. http://php.net/manual/en/mysqli.error.php 1 Link to comment Share on other sites More sharing options...
Gabrielphp Posted August 23, 2017 Share Posted August 23, 2017 In your code at the mysqli_connect() it misses this ";" it cannot work if you don't put it. Link to comment Share on other sites More sharing options...
dsonesuk Posted August 23, 2017 Share Posted August 23, 2017 You have been caught by the same error I made but I was tired, that statement flows on to next line, it end with that second line semi colon; post #4 and #5 1 Link to comment Share on other sites More sharing options...
Gabrielphp Posted August 23, 2017 Share Posted August 23, 2017 Ah, true, it has an or die statement. Sorry. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now