Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/crysta21/public_html/hospital.phpon line 53

<?php
/*
MCCodes FREE
hospital.php Rev 1.1.0c
Copyright (C) 2005-2012 Dabomstew

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

session_start();
require "global_func.php";
if ($_SESSION['loggedin'] == 0)
{
    header("Location: login.php");
    exit;
}
$userid = $_SESSION['userid'];
require "header.php";
$h = new headers;
$h->startheaders();
include "mysql.php";
global $c;
$is =
        mysql_query(
                "SELECT u.*,us.* FROM users u LEFT JOIN userstats us ON u.userid=us.userid WHERE u.userid=$userid",
                $c) or die(mysql_error());
$ir = mysql_fetch_array($is);
check_level();
$fm = money_formatter($ir['money']);
$cm = money_formatter($ir['crystals'], '');
$lv = date('F j, Y, g:i a', $ir['laston']);
$h->userdata($ir, $lv, $fm, $cm);
$h->menuarea();
print
        "<h3>Hospital</h3>
<table width='75%' border='2'><tr bgcolor=gray><th>ID</th><th>Name</th <th>Level</th> <th>Time</th><th>Reason</th></tr>";
$q =
        mysql_query(
                "SELECT u.*,c.* FROM users u WHERE u.hospital > 0 ORDER BY u.hospital DESC",
                $c);
while ($r = mysql_fetch_array($q))
{
    print
            "\n<tr><td>{$r['userid']}</td><td><a href='viewuser.php?u={$r['userid']}'>{$r['username']}</a> [{$r['userid']}]</td><td>
            {$r['level']}</td><td>{$r['hospital']} minutes</td><td>{$r['hospreason']}</td></tr>";
}
print "</table>";
$h->endpage();

......................................................................
I own and run a text based game while i got this warning
Any Suggestions?

Recommended Answers

All 11 Replies

Member Avatar for LastMitch

@hunterious.latham

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/crysta21/public_html/hospital.phpon line 53

There's an error on line 53 which is this:

while ($r = mysql_fetch_array($q))

When you look it closer you will notice that mysql_fetch_array() has a $q in it.

Now when you look at line 49 which has that $q:

$q =mysql_query("SELECT u.*,c.* FROM users u WHERE u.hospital > 0 ORDER BY u.hospital DESC",$c);

This is the where you need to fixed your $q in order for your code to run properly.

You need to check whether you are connected to the database?

Try to add this or die(mysql_error()); to this:

while ($r = mysql_fetch_array($q))

From this:

while ($r = mysql_fetch_array($q))

To this:

while ($r = mysql_fetch_array($q) or die(mysql_error());

Then run your code again. It will tell you whether you are connected to the database.

It's blank then it's connected, if there's an error then you know that there's something wrong with your MYSQL statement.

I don't have a db to see what's wrong with it but I already pinpoint the issue.

when i did that i got this:
Parse error: syntax error, unexpected ';' in /home/crysta21/public_html/hospital.php on line 53

It probably means $q is empty or null. Try to put it inside an if statement with if ( $q ) as the statement.

You can also add or die ( mysql_error() ); to make sure that the query is not producing an error:

$q = mysql_query( "SELECT u.*,c.* FROM users u WHERE u.hospital > 0 ORDER BY u.hospital DESC" , $c ) or die ( mysql_error() );

This is all pointing to an error in your query which I would say is the fact that you are trying to select all from table c but you don't define table c and your are not joining to to. Firstly try removing c.* from your query and see if it works.

If it works and you do need to select all from table c then you need to add in that table and a join to get the data (you will need to adjust the code below as I don't know what your table c is actually called so I have called it customers and presuming you will join between the id in the customers and users tables):

$q = mysql_query( "SELECT u.*,c.* FROM users u INNER JOIN customers.c ON c.id=u.customer_id WHERE u.hospital > 0 ORDER BY u.hospital DESC" , $c ) or die ( mysql_error() );
SELECT u.*,c.* FROM users u WHERE u.hospital > 0 ORDER BY u.hospital DESC

Remove ,c.* from this query. There is no alias for c.

I've already said that :)

wow thank you guys that worked like a charm

now since im here i have another problem. this acts as a hospital function to the game. players who are beaten in battle go in here for a short ammount of time which is displayed.

<td>{$r['hospital']} minutes</td><td>

But the problem is that the counter is supposed to go down ever minute...but it doesnt. I dont know if it ever has because this is a new page I inserted resently. Any ideas on where to look or how top fix it so the hospital minutes value goes down with time?

You'll need a javascript solution for this if you want to decrement the time without refreshing the page. I suggest you formulate a new question for this.

please mark as solved, thanks:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.