How to select more than two variables in a mysql_fetch_object while loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BaseballGraphs
    New Member
    • Sep 2010
    • 75

    How to select more than two variables in a mysql_fetch_object while loop

    Hello,

    I am trying to select three variables from my mySQL table. I have am creating a function so that I can display the three values later in a foreach loop. I am trying to pull the variables url, name, and s_name.

    I can't seem to get all three variables in one array. Could you please help me figure out how to improve the function and then show me how to display it.

    Here is the function:
    Code:
    function getURL(){
      $sql = 'SELECT * FROM searches LIMIT 10';
      $result = mysql_query( $sql );
      $searches = array();
      while( $obj = mysql_fetch_object( $result ) ){
             $searches[ $obj->url ] = $obj->name, $obj->s_name;
             }
             return $searches;
         }
    And here is how I plan on displaying it:
    Code:
    <?php foreach( $searches as $url => $name, $s_name ) { ?>
       <a href="<?= $url; ?>"><?= $name; ?>, <?= $s_name ?></a>
    <?php } ?>
    Any assistance would be greatly appreciated.
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    try this
    Code:
      $i=0;
      while( $obj = mysql_fetch_object( $result ) )
      {
       $searches[$i]['url'] = $obj->url;
       $searches[$i]['name'] = $obj->name;
       $searches[$i]['s_name'] = $obj->s_name;
      }
    Code:
    <?php foreach( $searches as $url) { ?>
       <a href="<?= $url['url']; ?>"><?= $url['name']; ?>, <?= $url['s_name'] ?></a>
    <?php } ?>
    by the way the produced URL will be erroneous. there is space in your url and comma

    Comment

    • BaseballGraphs
      New Member
      • Sep 2010
      • 75

      #3
      Thanks very much for the reply. It seems to be working, however, it doesn't appear to be returning the last 10 values in my table, it is only returning the most recent value.

      How can I get the output to display the last 10 values in my table?

      Here is the code:
      Code:
      $sql = 'SELECT * FROM searches LIMIT 10';
        $result = mysql_query( $sql );
        $searches = array();
        $i=0;
        while( $obj = mysql_fetch_object( $result ) )  {
        $searches[$i]['url'] = $obj->url;
        $searches[$i]['name'] = $obj->name;
        $searches[$i]['s_name'] = $obj->s_name; }
      Code:
      <?php foreach( $searches as $url) { ?>
        <a href="<?= $url['url']; ?>"><?= $url['name']; ?>, <?= $url['s_name'] ?></a>
      <?php } ?>

      Comment

      • johny10151981
        Top Contributor
        • Jan 2010
        • 1059

        #4
        you have limit your search to 10. It mean it will display only top 10 query output. remove the limit 10.

        Comment

        • BaseballGraphs
          New Member
          • Sep 2010
          • 75

          #5
          It is still only outputting the last value from my table. I want it to output the last 10 values in my table. How do I alter the code to do that?

          Code:
          $sql = 'SELECT * FROM searches';
            $result = mysql_query( $sql );
            $i=0;
            while( $obj = mysql_fetch_object( $result ) )  {
            $searches[$i]['url'] = $obj->url;
            $searches[$i]['name'] = $obj->name;
            $searches[$i]['s_name'] = $obj->s_name; }
          Code:
          <?php foreach( $searches as $url) { ?>
           <a href="<?= $url['url']; ?>"><?= $url['name']; ?>, <?= $url['s_name'] ?></a>
          <?php } ?>

          Comment

          • BaseballGraphs
            New Member
            • Sep 2010
            • 75

            #6
            This has been resolved:
            Code:
             $sql = 'SELECT * FROM searches ORDER BY id DESC LIMIT 5';
              $result = mysql_query( $sql );
              while( $obj = mysql_fetch_object( $result ) )  {
              $data = array();
              $data['url'] = $obj->url;
              $data['name'] = $obj->name;
              $data['s_name'] = $obj->s_name;
              $data['id'] = $obj->id;
              $data['team'] = $obj->team;
              $searches[] = $data;
              ksort($searches);
              }

            Comment

            Working...