Welcome to WebmasterWorld Guest from 35.171.45.91

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Problem with WHILE loop

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result

     
7:06 pm on Jun 15, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:May 21, 2005
posts:130
votes: 0


Hi Folks

I know I'm almost there with this. Please cast your eye over it. The problem seems to be with the relationship between the lines:

FROM tab_1, tab_2

[and]

while ($row= mysql_fetch_array($result, MYSQL_ASSOC))

[which supplies: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result]

When I alter FROM as follows:

FROM tab_1

[or]

FROM tab_2

It works perfectly (i.e. FROM a single table). . .

<?
include 'config.php';
include 'opendb.php';
$result = mysql_query("SELECT eventID, eventNAME,
DATE_FORMAT(datefrom, '%W, %D %M %Y.') as formatted_date_from,
DATE_FORMAT(dateto, '%W, %D %M %Y.') as formatted_date_to,
description
FROM tab_1, tab_2
WHERE datefrom <='2007-07-31'
AND dateto >='2007-07-01'
ORDER BY datefrom ASC");
while ($row= mysql_fetch_array($result, MYSQL_ASSOC))
{
print "<br>
<h2><u>".$row{'eventNAME'}."</u></h2>
Run Begins: <i>".$row{'formatted_date_from'}."</i>
Run Ends: <i>".$row{'formatted_date_to'}."</i><br>
<strong>Further Details: </strong>".$row{'description'}."
<br>
";
}
include 'closedb.php';
?>

Help would be greatly appreciated.

W.

(maybe I need mulitple $results1, $reslults2, etc?)

[edited by: Wonderstuff at 7:08 pm (utc) on June 15, 2007]

8:34 pm on June 15, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:July 18, 2006
posts:63
votes: 0


Hi,

You need to do your join properly. Not an expert but I had a prob like this last week. Try doing:
SELECT table1.var1, table1.var2, table2.var3, etc, i.e. for your example you might try:

SELECT tab_1.eventID, tab_1.eventNAME, tab_2.description

Hope it works.

10:22 pm on June 15, 2007 (gmt 0)

Senior Member from FR 

WebmasterWorld Senior Member henry0 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 19, 2003
posts:4449
votes: 11



SELECT table1.var1, table1.var2, table2.var3, etc, i.e. for your example you might try:

SELECT tab_1.eventID, tab_1.eventNAME, tab_2.description

//Don't forget calling the tables as follow:

"SELECT
t_1.eventID, t_1.eventNAME, t_2.description
FROM
t_1.tab_1, t_2.tab_2
WHERE
datefrom <='2007-07-31'
AND dateto >='2007-07-01'
ORDER BY datefrom ASC";

For this you need to have both conditions in each table
If not you need to call each condition while naming its pertaining table
For example
WHERE
t_1. your condition = t_2. your other condition

12:52 am on June 16, 2007 (gmt 0)

New User

10+ Year Member

joined:July 21, 2005
posts:30
votes: 0



$result = mysql_query("SELECT eventID, eventNAME,
DATE_FORMAT(datefrom, '%W, %D %M %Y.') as formatted_date_from,
DATE_FORMAT(dateto, '%W, %D %M %Y.') as formatted_date_to,
description
FROM tab_1, tab_2
WHERE datefrom <='2007-07-31'
AND dateto >='2007-07-01'
ORDER BY datefrom ASC");

I would recommend running this query directly in mysql and seeing what the error message is -- usually it's fairly helpful.

If you want PHP to spit out the error, you can do something like this:
$result = mysql_query("SELECT eventID, eventNAME,
DATE_FORMAT(datefrom, '%W, %D %M %Y.') as formatted_date_from,
DATE_FORMAT(dateto, '%W, %D %M %Y.') as formatted_date_to,
description
FROM tab_1, tab_2
WHERE datefrom <='2007-07-31'
AND dateto >='2007-07-01'
ORDER BY datefrom ASC") or die("MySQL Error: ".mysql_error());

As for what's wrong with the query -- as the above poster mentioned, maybe you need to work on your joins. The basic structure of a join query should be:

select ... from tab_1 join tab_2 on #*$!=#*$! where ... order by ...

(fill in the #*$!s with the columns you want to key the two tables on).

3:49 am on June 16, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:May 18, 2007
posts:49
votes: 0


Please also mention the table names before the columns datefrom and dateto like tab_1.datefrom and tab_2.dateto