genzedu777 Posted September 23, 2010 Share Posted September 23, 2010 Hi guys, I need help in my code, it states 'Error querying database' when I set $result = mysql_query($query, $dbc). And strange thing happens when I switched $query and $dbc place, and I got a different error msg $result = mysql_query($dbc, $query). 'Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in D:\inetpub\vhosts\championtutor.com\httpdocs\report.php on line 31 Error querying database' Any idea what is happening? Below is my code Lastly, do you guys have any debug tool software to recommend, so that it can assist me in debugging my code. Thanks <?php $first_name = $_POST['firstname']; $last_name = $_POST['lastname']; $when_it_happened = $_POST['whenithappened']; $how_long = $_POST['howlong']; $how_many = $_POST['howmany']; $alien_description = $_POST['aliendescription']; $what_they_did = $_POST['whattheydid']; $fang_spotted = $_POST['fangspotted']; $email = $_POST['email']; $other = $_POST['other']; $dbc = mysql_connect('*******', '*******', '*******', '*******') 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 ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', " . "'$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')"; $result = mysql_query($query, $dbc) or die('Error querying database.'); mysql_close($dbc); echo 'Thanks for submitting the form.<br />'; echo 'You were abducted ' . $when_it_happened; echo ' and were gone for ' . $how_long . '<br />'; echo 'Number of aliens: ' . $how_many . '<br />'; echo 'Describe them: ' . $alien_description . '<br />'; echo 'The aliens did this: ' . $what_they_did . '<br />'; echo 'Was Fang there? ' . $fang_spotted . '<br />'; echo 'Other comments: ' . $other . '<br />'; echo 'Your email address is ' . $email; ?> Quote Link to comment https://forums.phpfreaks.com/topic/214161-error-in-querying-database/ Share on other sites More sharing options...
PFMaBiSmAd Posted September 23, 2010 Share Posted September 23, 2010 Temporarily change your or die(...); statement to the following to find out why the query is failing - or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/214161-error-in-querying-database/#findComment-1114363 Share on other sites More sharing options...
sasa Posted September 23, 2010 Share Posted September 23, 2010 you must select database bvefore use it Quote Link to comment https://forums.phpfreaks.com/topic/214161-error-in-querying-database/#findComment-1114372 Share on other sites More sharing options...
genzedu777 Posted September 23, 2010 Author Share Posted September 23, 2010 Hi, The error states 'No database selected', but that is weird. I have clearly added the database in the server. Just to check with you guys the sequence of the database details $dbc = mysql_connect('localhost', 'DATABASE USERNAME', PASSWORD', 'DATABASE NAME') Is it correct?, first i will imply 'localhost', thereafter the username, perhaps 'cat', then password '1234567', then name of database 'cat_database'. Is there a way to track why my database is not connected? Quote Link to comment https://forums.phpfreaks.com/topic/214161-error-in-querying-database/#findComment-1114374 Share on other sites More sharing options...
PFMaBiSmAd Posted September 23, 2010 Share Posted September 23, 2010 The 4th parameter of mysql_connect() is NOT the database name. You must use mysql_select_db() Quote Link to comment https://forums.phpfreaks.com/topic/214161-error-in-querying-database/#findComment-1114377 Share on other sites More sharing options...
chintansshah Posted September 23, 2010 Share Posted September 23, 2010 Hey, I found unnecessary string concatenation in query. Use below string as your query. $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 ('".$first_name."', '".$last_name."', '".$when_it_happened."', '".$how_long."', '".$how_many."', '".$alien_description."', '".$what_they_did."', '".$fang_spotted."', '".$other."', '".$email."')"; Quote Link to comment https://forums.phpfreaks.com/topic/214161-error-in-querying-database/#findComment-1114378 Share on other sites More sharing options...
Pikachu2000 Posted September 23, 2010 Share Posted September 23, 2010 You just created unnecessary string concatenation. Quote Link to comment https://forums.phpfreaks.com/topic/214161-error-in-querying-database/#findComment-1114470 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.