Jump to content


mysql_fetch_array() error


6 replies to this topic

#1 Hacker-X

    Young Padawan

  • Members
  • Pip
  • 174 posts

Posted 25 November 2005 - 06:27 AM

I just uploaded Gio's Affiliate script, and I got this error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/name/public_html/display.php on line 5

This is the display.php file:

<?
include "connect.php";

$result = mysql_query("SELECT * FROM affilliates ORDER by RAND() LIMIT 0,3");
while($row = mysql_fetch_array($result)){

echo "<p><a href=\"www.domain.com/counter.php?outid=" . $row['id'] . "\" target=\"_blank\">
<img src=\"" . $row['ws_button'] . "\" width=88 height=31 alt=\"". $row['ws_name'] ."\" border=\"0\"></a><br>in: " . $row['countin'] . " | out: " . $row['countout'] . "</p>";
}
?>

The same error occurs when I attempt to open Affiliate.php the code can be found here:

<?
include "connect.php";

$result = mysql_query(" SELECT * FROM affilliates ORDER by RAND();");
while($row = mysql_fetch_array($result)){

echo "<p><a href=\"www.domain.com/counter.php?outid=" . $row['id'] . "\" target=\"_blank\">
<img src=\"" . $row['ws_button'] . "\" width=88 height=31 alt=\"". $row['ws_name'] ."\" border=\"0\"></a><br>in: " . $row['countin'] . " | out: " . $row['countout'] . "</p>";
}
?>

Edited by Hacker-X, 25 November 2005 - 06:36 AM.


#2 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 25 November 2005 - 11:13 AM

RAND(); that is causing your problem, its not correct MySQL syntax...

#3 Chaos King

    Senior Programmer

  • P2L Staff
  • PipPipPip
  • 676 posts
  • Gender:Male
  • Location:Florida

Posted 25 November 2005 - 11:19 AM

Actually, it is correct synthax. That is how you select random rows from the selected query.

Are you sure that there are actually affiliates in your database? ;) That is usually the error, also, you should add or die(mysql_error()); to the end of your queries to ensure that it is accurate also.

$result = mysql_query("MYSQL STUFF GOES HERE") or die(mysql_error());

Edited by Chaos King, 25 November 2005 - 12:51 PM.


#4 liveman

    Young Padawan

  • Members
  • Pip
  • 246 posts
  • Location:New Jersey

Posted 25 November 2005 - 12:29 PM

try making it
$result = mysql_query("SELECT * FROM `affilliates` ORDER BY RAND() LIMIT 0,3");
while($row = mysql_fetch_array($result,.MYSQL_NUM))
$result = mysql_query(" SELECT * FROM affilliates ORDER by RAND();");
while($row = mysql_fetch_array($result)){
Get rid of that ; after the RAND() and the bottom should work ;) to see if its actaully your sql query ... try running
SELECT * FROM `affilliates` ORDER BY RAND() in phpmyadmin and if it works, then its somethin in your php coding :)

Edited by liveman, 25 November 2005 - 12:31 PM.


#5 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 25 November 2005 - 12:55 PM

1. RAND() is correct, i use it myself.
2. MYSQL_NUM, won't fix there error.
3. That thing with the semi-colon may fix the bottom query.

To figure out what WILL fix the error. Try using this
$result = mysql_query("SELECT * FROM affilliates ORDER BY RAND() LIMIT 0,3") or die(mysql_error());
while($row = mysql_fetch_array($result)){
Also note, i'm assuming the actual query i selected for the example is to select random affiliates. If that's the case, you'll only randomly select the first 3 affiliates, no matter what.
To select any 3 affiliates, remove the "0," from the query.

#6 Ruben K

    Cliff

  • Twodded Staff
  • PipPip
  • 438 posts

Posted 28 November 2005 - 11:33 AM

Note that 'order by RAND() limit 3' counts as 3 queries instead of just one :P

Edited by Cliff, 28 November 2005 - 11:33 AM.


#7 HaloprO

    Requires Armed Escort

  • Members
  • PipPip
  • 310 posts
  • Gender:Male
  • Location:California, USA

Posted 28 November 2005 - 03:19 PM

<?php
include 'connect.php';
$q = mysql_query('SELECT * FROM `affiliates` ORDER BY RAND() LIMIT 3') or die(mysql_error());
$r = mysql_fetch_assoc($q);
do {
	echo "<p><a href=\"www.domain.com/counter.php?outid=" . $r['id'] . "\" target=\"_blank\"> 
	<img src=\"" . $r['ws_button'] . "\" width=88 height=31 alt=\"". $r['ws_name'] ."\"
	border=\"0\"></a><br>in: " . $r['countin'] . " | out: " . $r['countout'] . "</p>";
} while($r = mysql_fetch_assoc($q));
?>
This should work with what you are doing, if you get errors it has something to do with the database or table..





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users