MYSQL - Select specific value from a fetched array

后端 未结 9 1397
青春惊慌失措
青春惊慌失措 2021-01-06 01:45

I have a small problem and since I am very new to all this stuff, I was not successful on googling it, because I dont know the exact definitions for what I am looking for.

相关标签:
9条回答
  • 2021-01-06 02:43

    If you want to be able to use 2 consecutive results in one loop, you can store the results of the first loop, and then loop through.

    $initial = true;
    $storedId = '';
    
    while($row = mysql_fetch_array($result)) {
    
      $storedId = $row['id'];
    
      if($initial) {
    
        $initial = false;
        continue;
      }
    
      echo $storedId . $row['name'];
    }
    

    This only works for consecutive things though.Please excuse the syntax errors, i haven't programmed in PHP for a very long time...

    0 讨论(0)
  • 2021-01-06 02:43

    I used the code from the answer and slightly modified it. Thought I would share.

    $result = mysql_query( "SELECT name FROM category;", db_connect() );
    $myrow = array();
    while ($myrow[]  = mysql_fetch_array( $result, MYSQLI_ASSOC )) {}
    $num = mysql_num_rows($result);
    

    Example usage

    echo "You're viewing " . $myrow[$view_cat]['name'] . "from a total of " . $num;
    
    0 讨论(0)
  • 2021-01-06 02:44
    $counter = 0;
    while($row = mysql_fetch_array($result)){
        $counter++;
    
        if($counter == 2){
            echo $row['id']. " - ". $row['name'];
            echo "<br />";
        }
    }
    
    0 讨论(0)
提交回复
热议问题