Strange mysql_fetch_array() Problem

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Strange mysql_fetch_array() Problem

Post by Dale »

I've just been messing around with some code and decided to start making a kind of template system that reads from the database. I am using this little bit of code:

Code: Select all

<?php
    mysql_connect("localhost","root","");
    mysql_select_db("data");
    $getTmp = mysql_query("SELECT template FROM ".$dbprefix."templates WHERE title = 'header' LIMIT 1");
    $getTemp = mysql_fetch_array($getTmp);
    $template = $getTemp['template'];
    return $template;
?>
However it is constantly returning this error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\AppServ\www\index.php on line 5
I'm probably being blind or something, but I cannot seem to find what is wrong, anyone care to point it out?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Strange mysql_fetch_array() Problem

Post by Benjamin »

The query is failing.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Strange mysql_fetch_array() Problem

Post by Christopher »

Always check for errors:

Code: Select all

<?php
    mysql_connect("localhost","root","");
    mysql_select_db("data");
    $getTmp = mysql_query("SELECT template FROM ".$dbprefix."templates WHERE title = 'header' LIMIT 1");
    if (! mysql_errno()) {
        $getTemp = mysql_fetch_array($getTmp);
        $template = $getTemp['template'];
    } else {
        $template = mysql_error();
    }
    return $template;
?>
(#10850)
Post Reply