boyyo Posted April 23, 2011 Share Posted April 23, 2011 Im trying to connect to a database from php. Heres the code: <?php $dbc = mysqli_connect('192.168.0.122', 'boyyo', 'KiaNNa11', 'aliendatabase') or die('Error connecting to MySQL server.'); $query = "INSERT INTO aliens_abduction (first_name, last_name, " . "when_it_happened, how_long, how_many, alien_description, " . "what_they_did, fang_spotted, other, email) " . "VALUES ('Sally', 'Jones', '3 days ago', '1 day', 'four', " . "green with six tentacles', 'We just talked and played with a dog', " . "'yes', 'I may have seen your dog. Contact me.', " . "'sally@gregs-list.net')"; $result = mysqli_query($dbc, $query) or die('Error querying database.'); ?> I think i have a theory that my MySQL server location is wrong but i dont know. I use HostGator to do this and im using Phpmyadmin. But everytime i type in a form that i created it says Error querying database. Can someone tell me whats wrong with this code. Oh by the way im using head first into PHP and MySQL Quote Link to comment https://forums.phpfreaks.com/topic/234534-need-help-on-connecting-to-mysql-database-from-php/ Share on other sites More sharing options...
spiderwell Posted April 23, 2011 Share Posted April 23, 2011 try localhost instead of the ip address, also use phpmyadmin to check the user accounts and privledges match up Quote Link to comment https://forums.phpfreaks.com/topic/234534-need-help-on-connecting-to-mysql-database-from-php/#findComment-1205311 Share on other sites More sharing options...
gizmola Posted April 23, 2011 Share Posted April 23, 2011 First a tip. You will save yourself a lot of time and trouble in realizing that you do not need to concatenate just because you want to have a newline in your string. This is the same as what you have. $query = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long, how_many, alien_description, what_they_did, fang_spotted, other, email) VALUES ('Sally', 'Jones', '3 days ago', '1 day', 'four', green with six tentacles', 'We just talked and played with a dog', 'yes', 'I may have seen your dog. Contact me.', 'sally@gregs-list.net')"; If you could not connect to the db you'd be getting the first error, so there's a problem in your query. Try returning the actual error message. $result = mysqli_query($dbc, $query) or die(mysqli_error($dbc)); Quote Link to comment https://forums.phpfreaks.com/topic/234534-need-help-on-connecting-to-mysql-database-from-php/#findComment-1205313 Share on other sites More sharing options...
boyyo Posted April 24, 2011 Author Share Posted April 24, 2011 when you say try returning the actual error message what do you mean by that? Quote Link to comment https://forums.phpfreaks.com/topic/234534-need-help-on-connecting-to-mysql-database-from-php/#findComment-1205472 Share on other sites More sharing options...
wildteen88 Posted April 24, 2011 Share Posted April 24, 2011 gizmola meant using the function mysqli_error to find out why your query is failing, Checkout the snippet gizmola posted Try returning the actual error message. $result = mysqli_query($dbc, $query) or die(mysqli_error($dbc)); Notice the use of the mysqli_error function. What is the error message? Quote Link to comment https://forums.phpfreaks.com/topic/234534-need-help-on-connecting-to-mysql-database-from-php/#findComment-1205505 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.