PDA

View Full Version : mysql_fetch_array function failing?


RysChwith
11-27-2004, 07:43 PM
Here is undoubtedly another one to blame on the turkey.

The following code is supposed to query a database and write the results into a page. I know the query works, because I've tested it directly. It returns three records. However, the page absolutely refuses to go into the while loop, despite the fact that I copied and pasted that code from a functioning page. It's probably something stupid, but I'm at a loss to see it.

<?php
$db_error = "<p>Event information is currently unavailable. Please try back later.</p>\n";
$today = date( 'Y-m-d' );

if( $dbConn = @mysql_connect( '*****', '*****', '*****' ) )
{
if( @mysql_select_db( '*****' ) )
{
$query = "SELECT name, url, startdate, enddate FROM Events WHERE 'group' = 002";
print "<!-- " . $query . " -->\n";
if( $results = mysql_query( $query ) )
{
$count = 0;
print "<!-- Results are: " . $results . " -->\n";
while( $row = mysql_fetch_array( $results ) )
{
print "<!-- Printing row. -->\n";
print "<p><em>" . $row[ 'startdate' ];
if( $row[ 'enddate' ] > $row[ 'startdate' ] )
{
print " - " . $row[ 'enddate' ];
}
print ":</em> <a href = \"";
if( $row[ 'url' ] != "" )
{
print $row[ 'url' ];
}
else
{
print "events/index.html";
}
print "\">" . $row[ 'name' ] . "</a></p>\n";
$count++;
}
print "<!-- After while loop. -->\n";
if( $count < 1 )
{
print "<p>There are no events currently scheduled.</p>\n";
}
}
else
{
print $db_error . "<p>Query failed because of: ";
print mysql_error() . "</p>";
}
}
else
{
print $db_error . "<p>DB select failed.</p>";
}
}
else
{
print $db_error . "<p>DB connection failed.</p>";
}
?>

(Note that the actual code contains server information rather than asterisks, but I didn't feel like putting it out in a public forum. I'm sure you'll all understand.)

The HTML comments I've thrown in for testing purposes produce the following results:

<!-- SELECT name, url, startdate, enddate FROM Events WHERE 'group' = '002' -->
<!-- Results are: Resource id #2 -->
<!-- After while loop. -->

Anyone who wants to see the results in situ can check here (http://www.stormsport.org/test/index.php) .

Rys

scoutt
11-27-2004, 07:50 PM
$query = "SELECT name, url, startdate, enddate FROM Events WHERE 'group' = 002";

for one, don't quote it, for two, didn't you just change that name? ;)

tha tis why I don't like to use

if( $results = mysql_query( $query ) ){

if it errors you can't see it.

just do this

$results = mysql_query( $query ) or die("<p>Query failed because of: ".mysql_error());

and you also cut down the code cause you eliminated a if statment

RysChwith
11-27-2004, 08:28 PM
No, see, I had changed the table named Group. This is the field inside a different table. Although the problem ended up being largely the same, solved this time by making it "Events.group". And I actually had something in there to catch the errors. The problem was that it wasn't erroring, it just wasn't finding any matching records.

I think I'm going to start naming all my fields, tables, variables, and functions in Welsh just to avoid accidentally naming them after reserved words. Granted, each one will be three lines long, but still....

Rys

scoutt
11-27-2004, 08:36 PM
I also name then something else, no 3 lines thing either.

it maybe the fact it was hung up and it didn't know what to do seeing it saw a reserved word.