Jump to content

"Query Failed" - but it works!?!


Acute Chaos

Recommended Posts

I posted problems earlier and I got them working except for one issue...

 

When this code runs it puts the information into the database perfectly but the user gets a returned error message saying the final query failed.  I can't figure out why it says it failed when it worked?!

 

I am building a member site that members must register at before using.

 

I have already in the db - their firstname, lastname and an id number (unique number).

 

The plan is to have them supply their first name, last name, id number, email address and password when they register and authenticate who they are with the information I already have.  If they have all three bits of information matching the database we add the email address and password they supply and send them to the log in page.

 

This is what I have:

 

 

	if($login != '') {
	$qry = "SELECT fname, lname, login FROM user WHERE fname='$fname' AND lname='$lname' AND login='$login'";
	$result = mysql_query($qry);
	if($result) {
		if(mysql_num_rows($result) != 1) {
			$errmsg_arr[] = 'This didn't work try again';
			$errflag = true;
		}
		@mysql_free_result($result);
	}
	else {
		die("Query failed - 1");
	}
}


if($errflag) {
	$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
	session_write_close();
	header("location: register-form.php");
	exit();
}


//	$qry = "INSERT INTO user (email, password) VALUES ('$email', '".md5($_POST['password'])."')";
$qry = mysql_query("UPDATE user SET email='$email', password='".md5($_POST['password'])."' WHERE login='$login'");
$result = @mysql_query($qry);

if($result) {
	header("location: register-success.php");
	exit();
}else {
	die("Query failed - 2");
}

 

 

It is the 'query failed - 2' that is displayed after the form is submitted but... the information updates in the db perfectly.

 

The register-success.php has no code to counter anything.  At this point is just says great you made it here. (except i don't lol)

 

Please help me!  thanks.

 

 

 

 

Link to comment
Share on other sites

$qry = mysql_query("UPDATE user SET email='$email', password='".md5($_POST['password'])."' WHERE login='$login'");
$result = @mysql_query($qry);

 

Seems pretty obvious

 

Why are you using error suppressors? Bad idea, don't you think?

Link to comment
Share on other sites

I am very new to this and am using tutorials and examples to get me going, so nothing is 'pretty obvious' to me at this point. That is why I am here looking for help.

 

This code is a reworked example I was experimenting with in a tutorial.

 

I understand most of what I have going here but there are a few lines I don't  -  I assume @mysql_free_result($result); and $result = @mysql_query($qry); are the suppressors you are refering to?

 

Either way I am still looking for help to fix my problem.

 

Anyone?  Thanks!

 

 

 

Link to comment
Share on other sites

@ is the error suppressor. You put it in front of a function to stop it from spitting out errors.

 

Sounds like you need to find a tutorial that explains what's going on.

 

Read the code I highlighted aloud. Do you notice a slight redundancy? Have you check PHP's manual regarding mysql_query() and how to use it?

Link to comment
Share on other sites

Here's the obvious part that was mentioned.. and yes, it is what you pointed out.

 

But to elaborate more, it is the @ symbol that suppresses the errors.  When you write @mysql_query, no errors are reported.  Sure, the query might return false and cause your error message to come through, but you really have no idea whatsoever WHY.

 

Take out the @ symbol first.  Then you'll want to make use of a PHP funciton called mysql_error.  This function will tell you exactly, if any, what error there is..... and there are pretty much two ways of using this function.  You can either halt your script completely with the die function

$qry = mysql_query("UPDATE user SET email='$email', password='".md5($_POST['password'])."' WHERE login='$login'");
$result = mysql_query($qry) or die("Your SQL error is: " . mysql_error());

Or you can put it into your error message variable.

$result = mysql_query($qry);

if($result) {
	header("location: register-success.php");
	exit();
}else {
	die("Query failed - 2 .... because
\n" . mysql_error() );
}

 

 

 

After c/ping that I realized you already using the die function, but you're not using mysql_error.  Regardless, don't suppress errors unless you absolutely need to.

Link to comment
Share on other sites

Thanks Zanus - Makes sense now.  I appreciate the clarity.

 

I did what you said and my error is:

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1

 

There are three pages working here:

 

The form page, the exe page, and the final destination page when successful.  I don't see anything wrong at the beginning of any of the pages.

 

Form page and destination pages have the same first few lines:

<?php
session_start();
?>
<!DOCTYPE...

 

 

 

The exe page that I've been posting code from - first few lines:

 

<?php
    session_start();
    require_once('config.php');
    $errmsg_arr = array();

 

 

Any ideas?  Thanks.

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

The $qry variable in your code is the TRUE/FALSE value from the first mysql_query() statement and since that is your UPDATE query that actually works, it is a TRUE or 1.

 

You are putting $qry into a second mysql_query() statement as through it was a SQL statement, but it is not, so of course you are getting a failure message. Why do you have two mysql_query() statements in there?

Link to comment
Share on other sites

Have you looked at the PHP manual's entry for mysql_query()?

 

Until you do, I'm done helping. Your error is so glaringly obvious to even a beginner programmer :( Look over the code I highlighted. DO YOU SEE ANY REDUNDANCY?!

 

Never mind, PFMaBiSmAd spelled it out for you. If you can't debug something like this, I'd suggest starting here and working your way up.

Link to comment
Share on other sites

Thanks PFMaGiSmAd!

 

I really appreciate the help.  Once explained well I totally see the issue and it is working perfectly now.  It is remedial but when you are at my level (and age...) things don't always stand out.

 

I'm getting there thanks to folks like you and Zanus taking the time to spell it out for noobs like me.  There's lot I can help others in but not this! lol

 

 

NOTE:  xyph if you are so far above someone that responding to their posts leaves you with nothing but the need to belittle and get frustrated that we aren't all as great as you, you should consider just skipping over posts as 'glaringly obvious' as mine and stick to saving the world with bigger problems.

 

 

This forum and the contributors have been a brilliant help to me as I try to find my through php and mysql.  Today is the first day I really feel like I ran into a knob.

 

 

Thanks again for all the help guys!

Link to comment
Share on other sites

I'm here to offer people who need honest help. Sadly, the majority of posts here are people with no coding background trying to get us to fix issues they've attempted to strap together via copy+paste from various tutorials, or programs they've previously hired others to do.

 

I helped you. I honestly did. I isolated your problem to two lines, and directed you towards the manual. If you had even a basic understanding of the language - Keep in mind, the code you posted is above introductory stuff  - you would understand what a function() is, and what it does. If you had checked the manual, like I suggested, you would notice that mysql_query() expects a STRING as input, along with an optional resource, and returns a resource (on SELECT queries).

 

With the above knowledge, the secretary here, who's programming experience end with Microsoft Excel, was able to point out the error in those lines. You've defined a variable using a function that will NEVER return a string, and trying to use it in a function that ONLY TAKES a string.

 

The reason I'm getting frustrated? You're not learning the language. You're throwing together other people's code in an attempt to get something to work a certain way without really understanding why. When something EXTREMELY basic goes wrong - and with hundreds of lines of code, typos happen - you don't have the slightest clue how to fix it.

 

The reason I'm greater than you (you put it that way, not me) is because I started small and worked my way up. I didn't have to bug someone about every single minor issue because I attempted to learn what I was using before I tried to use it. Did I ever have to ask questions others found trivial? Of course! Did I ever have a parse error I couldn't find myself? An error thrown from a function that I couldn't debug? Very rarely.

 

You not being able to spot the error after it was isolated to two lines shows me that you have no interest to learn on your own. I don't care to help you until you're ready to do so. Don't want my help? So be it. Keep this kinda stuff up and you'll find more and more people who just get fed up.

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.