champoi Posted October 9, 2008 Share Posted October 9, 2008 uhmm, help! i'm just starting on mysql and php, but this error is stopping me from practicing more codes, the codes are: $dbc = mysql_connect(); if (!$dbc) { die('Could not connect: ' . mysql_error()); } else { echo "Connected! <br />"; } mysql_select_db('sample_db',$dbc); $un = "cham"; $pw = "poi"; $fn = "bay"; $ln = "bum"; echo $un . " " . $pw . " " . $fn . " " . $ln . '<br />'; mysql_query("INSERT INTO `sample_db` (`username`, `password`, `first_name`, `last_name`) VALUES ('$un', '$pw', '$fn', '$ln')"); if (!mysql_query($sql)) { die(' Error: ' . mysql_error()); } echo "Stored!"; mysql_close($con); there are no data stored in the mysql and the output of this is: "Connected! cham poi bay bum Error: Query was empty" I left the mysql connect blank here, in my site it's not and it works in my site, so i doubt that the mysql is the problem, any suggestions, tips or help? Quote Link to comment https://forums.phpfreaks.com/topic/127689-solved-help-query-was-empty/ Share on other sites More sharing options...
php.ajax.coder Posted October 9, 2008 Share Posted October 9, 2008 you don't need the quote makes around the db name in the insert query INSERT INTO sample_db Quote Link to comment https://forums.phpfreaks.com/topic/127689-solved-help-query-was-empty/#findComment-660823 Share on other sites More sharing options...
Chicken Little Posted October 9, 2008 Share Posted October 9, 2008 Try removing ($sql) from your if statement. Also, check your database to see if the password is being hashed. Quote Link to comment https://forums.phpfreaks.com/topic/127689-solved-help-query-was-empty/#findComment-660828 Share on other sites More sharing options...
PFMaBiSmAd Posted October 9, 2008 Share Posted October 9, 2008 You have two mysql_query() statements. The first one contains the actual query. The second one references a variable $sql that does not exist. When learning php, developing php code, or debugging php code, set error_reporting to E_ALL and set display_errors to ON in your php.ini to get php to help you with errors like this one. The non-existent $sql variable results in an error that would be output if these two settings are on. Quote Link to comment https://forums.phpfreaks.com/topic/127689-solved-help-query-was-empty/#findComment-660839 Share on other sites More sharing options...
champoi Posted October 9, 2008 Author Share Posted October 9, 2008 sorry but i kinda messed up the code before i posted it, here's the correction: $sql = mysql_query("INSERT INTO sample_db (`username`, `password`, `first_name`, `last_name`) VALUES ('$un', '$pw', '$fn', '$ln')"); if (!mysql_query($sql)) { die(' Error: ' . mysql_error()); } echo "Stored!"; i think this is an error handler that checks if data is stored or not ^^ Quote Link to comment https://forums.phpfreaks.com/topic/127689-solved-help-query-was-empty/#findComment-660849 Share on other sites More sharing options...
champoi Posted October 9, 2008 Author Share Posted October 9, 2008 you don't need the quote makes around the db name in the insert query INSERT INTO sample_db i tried it but still does not work, T__T Quote Link to comment https://forums.phpfreaks.com/topic/127689-solved-help-query-was-empty/#findComment-660895 Share on other sites More sharing options...
champoi Posted October 9, 2008 Author Share Posted October 9, 2008 Try removing ($sql) from your if statement. Also, check your database to see if the password is being hashed. how can i check if it's hashed? what's the effect if it is? thnx Quote Link to comment https://forums.phpfreaks.com/topic/127689-solved-help-query-was-empty/#findComment-660901 Share on other sites More sharing options...
Chicken Little Posted October 9, 2008 Share Posted October 9, 2008 Open the database table and view the data. If it's hashed it will look something like 1214300156ac39ff19ab936cc8e82d9d520baa18 Quote Link to comment https://forums.phpfreaks.com/topic/127689-solved-help-query-was-empty/#findComment-661010 Share on other sites More sharing options...
champoi Posted October 9, 2008 Author Share Posted October 9, 2008 Open the database table and view the data. If it's hashed it will look something like 1214300156ac39ff19ab936cc8e82d9d520baa18 uhmm, i've checked it and it does not appear, i entered a data before just to browse the data, i havent seen that yet, Quote Link to comment https://forums.phpfreaks.com/topic/127689-solved-help-query-was-empty/#findComment-661012 Share on other sites More sharing options...
PFMaBiSmAd Posted October 9, 2008 Share Posted October 9, 2008 This has nothing to do with the data. Your code is wrong. As I already wrote, you have two mysql_query() statements. The first one is the actual one that is running the INSERT query. The second mysql_query() has no meaning in the code and since it is using the $sql variable, which is now the TRUE/FALSE result of the INSERT query, the error message is telling you that $sql does not contain anything that is an SQL query. Quote Link to comment https://forums.phpfreaks.com/topic/127689-solved-help-query-was-empty/#findComment-661022 Share on other sites More sharing options...
champoi Posted October 9, 2008 Author Share Posted October 9, 2008 i removed it and still no go, it does not store in the table in my mysql, Quote Link to comment https://forums.phpfreaks.com/topic/127689-solved-help-query-was-empty/#findComment-661173 Share on other sites More sharing options...
Chicken Little Posted October 9, 2008 Share Posted October 9, 2008 Try this $sql="INSERT INTO sample_db (username, password, first_name, last_name) VALUES ('$un', '$pw', '$fn', '$ln')"; $res=mysql_query($sql) or die ("Sorry, it did not work!"); Quote Link to comment https://forums.phpfreaks.com/topic/127689-solved-help-query-was-empty/#findComment-661249 Share on other sites More sharing options...
champoi Posted October 10, 2008 Author Share Posted October 10, 2008 Try this $sql="INSERT INTO sample_db (username, password, first_name, last_name) VALUES ('$un', '$pw', '$fn', '$ln')"; $res=mysql_query($sql) or die ("Sorry, it did not work!"); it told it didnt work, hmmm, maybe the mysql is the problem, but i just cant think of a reason why it wouldnt work >_< Quote Link to comment https://forums.phpfreaks.com/topic/127689-solved-help-query-was-empty/#findComment-661793 Share on other sites More sharing options...
Chicken Little Posted October 10, 2008 Share Posted October 10, 2008 It worked for me. Can you repost your entire code again. Quote Link to comment https://forums.phpfreaks.com/topic/127689-solved-help-query-was-empty/#findComment-661924 Share on other sites More sharing options...
champoi Posted October 10, 2008 Author Share Posted October 10, 2008 It worked for me. Can you repost your entire code again. here: $dbc = mysql_connect(); if (!$dbc) { die('Could not connect: ' . mysql_error()); } else { echo "Connected! <br />"; } mysql_select_db('sample_db',$dbc); $un = "cham"; $pw = "poi"; $fn = "bay"; $ln = "bum"; echo $un . " " . $pw . " " . $fn . " " . $ln . '<br />'; $sql="INSERT INTO sample_db (username, password, first_name, last_name) VALUES ('$un', '$pw', '$fn', '$ln')"; $res=mysql_query($sql) or die ("Sorry, it did not work!"); mysql_close($dbc); ?> this is the print screen of the mysql, in case you find something unusual, Quote Link to comment https://forums.phpfreaks.com/topic/127689-solved-help-query-was-empty/#findComment-662216 Share on other sites More sharing options...
AndyB Posted October 10, 2008 Share Posted October 10, 2008 $res=mysql_query($sql) or die ("Sorry, it did not work!"); Spectacularly unhelpful! Instead, try: $res=mysql_query($sql) or die ("Error: ". mysql_error(). " with query ". $sql); Ah! Now you know the error that occurred AND you know the querystring where it failed. Then you can see what the real problem is. Quote Link to comment https://forums.phpfreaks.com/topic/127689-solved-help-query-was-empty/#findComment-662330 Share on other sites More sharing options...
champoi Posted October 11, 2008 Author Share Posted October 11, 2008 $res=mysql_query($sql) or die ("Sorry, it did not work!"); Spectacularly unhelpful! Instead, try: $res=mysql_query($sql) or die ("Error: ". mysql_error(). " with query ". $sql); Ah! Now you know the error that occurred AND you know the querystring where it failed. Then you can see what the real problem is. thnx!! hmm, it told me this was the error: Error: No database selected with query INSERT INTO sample_db (username, password, first_name, last_name) VALUES ('cham', 'poi', 'bay', 'bum') maybe i messed up on coding the values to connect on mysql, thnx to everyone who helped Quote Link to comment https://forums.phpfreaks.com/topic/127689-solved-help-query-was-empty/#findComment-662501 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.