Jump to content

[SOLVED] help! query was empty


champoi

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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,

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

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.