PDA

View Full Version : Having a problem running a query


!Alive
03-09-2012, 02:28 PM
So what I'm trying to accomplish is every time the page changes/reloaded check to see if the user has won a random, mystery prize. Right now I have got the random prize page working and I already have the piece of code that randomly decides if the user wins something.
My problem is that it doesn't run my query to update the info in the table. I think my problem may be where I have put the code, which I currently have located in the functions.php.

Any help would be greatly appreciated.
Thank you much in advance.

SilverDragonTears
03-09-2012, 11:39 PM
We probably need to see the code snippet...

Hall of Famer
03-10-2012, 11:16 AM
umm the mysql update query wont work? Well I've had problems like this at times but theres really an easy way to debug. The mysql query string may have a typo in it, or the variables used to locate or update the database contain wrong information. To debug, lets consider the example below:


runquery("UPDATE {$prefix}adoptables_conditions SET promocode='{$code}' WHERE type='{$type}'");


The query string is enclosed in the function runquery. The variable $code is used to update database info, while the other variable $type is used to locate the specific row in a table. You can debug by outputting values inside the variables $code and $type to the screen and see if they contain the correct information. If so, you've apparently made a typo with the query string. Otherwise, chances are you've incorrectly assigned values to $code or $type.

!Alive
03-10-2012, 12:30 PM
$number=rand(1,2);
if ($number == 2){
runquery("UPDATE ".$GLOBALS['prefix']."users SET explore='1' WHERE username".$GLOBALS['loggedinname']);

$query = "SELECT * FROM ".$GLOBALS['prefix']."users WHERE username".$GLOBALS['loggedinname'];
$result = mysql_query($query);
$explore=@mysql_result($result,0,"explore");
echo "$explore";

return $explore;
if ($explore == 1){
header( 'Location: browseprize.php' ) ;
}
}

That's my code I'm running. I don't think I have any sytax errors, but I'm still new to this so I could be wrong.

Hall of Famer
03-10-2012, 09:49 PM
umm your query is wrong, try this:


runquery("UPDATE {$GLOBALS['prefix']}users SET explore='1' WHERE username = '{$loggedinname}'");
$query = "SELECT * FROM {$GLOBALS['prefix']}users WHERE username = '{$loggedinname}'";

!Alive
03-11-2012, 01:54 AM
That works perfectly. Thanks a bunch HoF. <3 :usedusedused:

Hall of Famer
03-11-2012, 09:16 AM
You are very welcome, glad it works out for you. Another quick note is that at times it does not work because you are using mysql's reserved word. I ran into this problem once, in which the field name 'usage' is a reserved word and thus cannot be used. Because of this I could not manage to create the table prefix.items_functions. I changed it to 'intent' instead and the problem is all gone.

fadillzzz
03-11-2012, 09:31 AM
You are very welcome, glad it works out for you. Another quick note is that at times it does not work because you are using mysql's reserved word. I ran into this problem once, in which the field name 'usage' is a reserved word and thus cannot be used. Because of this I could not manage to create the table prefix.items_functions. I changed it to 'intent' instead and the problem is all gone.

This might be slightly out of topic, but it's also worth noting that you can still use a reserved keyword if you enclose it with backticks.

Hall of Famer
03-11-2012, 02:39 PM
@ Fadillzzz: Yeah you are right, I actually tried that before but it still gave me an error. Turned out that I was using single quote instead of backticks, oh my...