Use mysql_fetch_array() with foreach() instead of while()

前端 未结 5 665
梦谈多话
梦谈多话 2020-12-05 21:49

i want to know how do we convert the following code to work with foreach

$query_select = \"SELECT * FROM shouts ORDER BY id DESC LIMIT 8;\"; 

    $result_se         


        
5条回答
  •  时光说笑
    2020-12-05 22:20

    You can code like this:

    $query_select = "SELECT * FROM shouts ORDER BY id DESC LIMIT 8;";
    $result_select = mysql_query($query_select) or die(mysql_error());
    $rows = array();
    while($row = mysql_fetch_array($result_select))
        $rows[] = $row;
    foreach($rows as $row){ 
        $ename = stripslashes($row['name']);
        $eemail = stripcslashes($row['email']);
        $epost = stripslashes($row['post']);
        $eid = $row['id'];
    
        $grav_url = "http://www.gravatar.com/avatar.php?gravatar_id=".md5(strtolower($eemail))."&size=70";
    
        echo ('Gravatar'.'
    '); echo $eid . '
    '; echo $ename . '
    '; echo $eemail . '
    '; echo $epost . '



    '; }

    As you can see, it's still need a loop while to get data from mysql_fetch_array

提交回复
热议问题