Page 1 of 1

php / mysql query

Posted: Thu Jan 22, 2009 3:41 pm
by operationOverkill7
I am currently coding a GCSE project in PHP. I had already coded this before, and it DEFINITELY worked. I may have updated to a new PHP version(I use WAMP and had downloaded a new version). Anyway, I was testing my old code and it wasn't working. No error codes were returned via

Code: Select all

or die(mysql_error())
So I decided to insert

Code: Select all

echo hi;
untill I found the code which was 'broken'. After locating this code I changed

Code: Select all

or die(mysql_error())
to

Code: Select all

or die(NO IDEA)
and finally it talked to me.

I have fixed the problem in the code now, but could someone please tell me what was wrong with this code(which use to work):

Code: Select all

// Checks if user has already registered a match
$warlistedquery = mysql_query("SELECT warlisted FROM users WHERE username='$username'") or die(mysql_error());
$warlisted_fetch = mysql_fetch_array($warlistedquery) or die(mysql_error());
$warlisted = $warlisted_fetch['warlisted'] or die('NO IDEA');
if ($warlisted =='1')
    {
    die('You have already listed a war, delete it via control pannel to list a new war');
    }
AND this code which now works.

Code: Select all

$warlistedquery = mysql_query("SELECT warlisted FROM users WHERE username='$username'") or die(mysql_error());
$warlisted_fetch = mysql_fetch_array($warlistedquery) or die(mysql_error());
if ($warlisted_fetch['warlisted'] =='1')
    {
    die('You have already listed a war, delete it via control pannel to list a new war');
    }
Oh and no flaming my code :D I'm new to PHP, my second program.
Cheers,
Mkay

Re: php / mysql query

Posted: Thu Jan 22, 2009 7:51 pm
by califdon
For one thing, be aware that a properly formed SQL query that doesn't find any matching record(s) will NOT return an error, but when you try to fetch a row from the query result, it will probably produce an error because there is no array for it to operate on. What I'm suggesting is the possibility that you may have searched for a non-existent record, and if so, you would not see any die(mysql_error()) message. You can insert a line to check whether any row(s) were returned, using mysql_num_rows($warlistedquery).