1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

help please! first result not included!!!

Discussion in 'PHP' started by whiteblue1942, Apr 18, 2008.

  1. #1
    ok i am having an issue and cant figure it out. im retrieving my data and using a loop to output it but each time i do this the first result is not included!! why is the first result now showing up????


    <?php
    $query="SELECT firstname, money FROM users ORDER BY money DESC";
    $result=mysql_query($query);
    $firstname=mysql_result($result,0,"firstname");
    
    //loop through each row outputting the data
    for ($x=1; $x<= 3; $x++)
    {
    $firstname=mysql_result($result,$x,"firstname");
    $money=mysql_result($result,$x,"money");
    echo "$x. $money -------- $firstname <br/>";
    }
    ?>
    Code (markup):

     
    whiteblue1942, Apr 18, 2008 IP
  2. phpl33t

    phpl33t Banned

    Messages:
    456
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The proper way:

    
    <?php
    
    $result = mysql_query('SELECT `firstname`,`money` FROM `users` ORDER BY `money` DESC') or die(mysql_error());
    if (mysql_num_rows($result) == 0) {
    	echo 'No results!';
    } else {
    	$x = 0;
    	while ($items = mysql_fetch_array($results)) {
    		$firstname = stripslashes($items['firstname']);
    		$money = stripslashes($items['money']);
    		$x++;
    		echo $x.'. '.$money.' -------- '.$firstname.' <br/>';
    	}
    }
    
    ?>
    
    PHP:
    Also, get in the habit of using single slashes. Then, if you need to echo double quotes, you wont have to backslash the double quotes.
     
    phpl33t, Apr 18, 2008 IP
    whiteblue1942 likes this.
  3. whiteblue1942

    whiteblue1942 Peon

    Messages:
    573
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks a ton rep added!
     
    whiteblue1942, Apr 18, 2008 IP
  4. phpl33t

    phpl33t Banned

    Messages:
    456
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks and your welcome!
     
    phpl33t, Apr 19, 2008 IP