Jump to content

Recommended Posts

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?  :D

Link to comment
https://forums.phpfreaks.com/topic/127689-solved-help-query-was-empty/
Share on other sites

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.

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 ^^

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.

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 >_<

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,

mysqlprintscreen.JPG

$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.

$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  :D

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.