dtzhfb4869
2014-01-29 09:05
浏览 35
已采纳

从DB中返回的值设置PHP变量

Newbie to PHP and trying to learn it. I have returned data from my DB.

So the rows may look something like below

ID--------Name--------PhoneNo
1         Joe         1234
2         Jane        5678
3         Tom         0000

I am using the msql_fetch_array to return the data as below:

    <?php
            while($row = mysql_fetch_array($result))
            {
                $NameOne = $row['Name'];
            }
    ?>

This is placing the name 'Joe' in the variable name NameOne as expected. What is the best way however to get Jane and Tome into variables called $NameTwo and $NameThree that I can then echo these variables further down in my html and similary I want to place the phone number retrieved into seperate variables and refer to them later in my html.

图片转代码服务由CSDN问答提供 功能建议

新手入PHP并尝试学习它。 我从我的数据库返回了数据。

所以行可能看起来像下面的

  ID -------- 名称-------- PhoneNo 
1 Joe 1234 
2 Jane 5678 
3 Tom 0000 
   
 
 

我使用msql_fetch_array返回如下数据 :

 &lt;?php 
 while($ row = mysql_fetch_array($ result))
 {
 $ NameOne = $ row ['Name']; 
  } 
?&gt; 
   
 
 

这将名称'Joe'置于变量名NameOne中,如预期的那样。 然而,将Jane和Tome变成名为$ NameTwo和$ NameThree的变量的最佳方法是,然后我可以在我的html中进一步回显这些变量,类似我想将检索到的电话号码放入单独的变量中,稍后再参考它们 我的HTML。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • dtd14883 2014-01-29 09:09
    已采纳

    The ideal approch will be saving them inside an array, which can be achieved like this:

    $Names = array();
    $i = 0;
    while($row = mysql_fetch_array($result)) {
        $Names[$i] = $row['Name'];
        $i++;
    }
    

    Now you can retrieve your data this way:

    echo $Names[0]; // Will output the first name saved.
    
    点赞 打赏 评论
  • du13520157325 2014-01-29 09:08

    If you want to really persist the results, store them in an array:

    <?php
            $data = array();
            // Note that I've changed "mysql_fetch_array" to "mysql_fetch_assoc" to decrease the number of saved data.
            while($row = mysql_fetch_assoc($result))
            {
                $data[] = $row['Name'];
            }
    
            /* Now you can use the data wherever you want, like: $data[0]['Name'] for first name, $data[1]['Name'] for second name and so on */
    
    ?>
    
    点赞 打赏 评论
  • dongpinyao2203 2014-01-29 09:09

    Rather than creating so many new variables, why not create an array?

    <?php
            $i=1;
            while($row = mysql_fetch_array($result))
            {
                $Names[$i]= $row['Name'];
                $i++;
            }
    ?>
    

    You can then use the array in your code

    echo $Names[1]; // 1 or 2 or 3 etc
    

    For multiple attributes you can use a multidimensional array

            $i=1;
            while($row = mysql_fetch_array($result))
            {
                $Data[$i]["Name"]= $row['Name'];
                $Data[$i]["Phone"]= $row['Phone'];
                $i++;
            }
    

    You can then use the array in your code

    echo $Data[1]["Name"]; // 1 or 2 or 3 etc
    
    点赞 打赏 评论
  • dougengqiu8031 2014-01-29 09:10
    1. You can directly place html inside your while loop

      <?php
              while($row = mysql_fetch_array($result))
              {
                  ?>
                  <div><?php echo  $row['Name']; ?> </div>
                  <div><?php echo  $row['PhoneNo']; ?> </div>
              <?php }
      ?>
      
    2. You can add the details to another array

      <?php
              $names = array();
              $phnos = array();
              while($row = mysql_fetch_array($result))
              {
                   $names[] = $row['Name'];  //$names[0] , $names[1] etc..
                   $phnos[] = $row['PhoneNo']; //$phnos[0] , $phnos[1] etc..
              }
      ?>
      
    点赞 打赏 评论
  • douchu4048 2014-01-29 09:40

    To strictly answer KOL's question:

    <?php
        $i = 0;
        $numbers = array('One', 'Two', 'Three', 'Four', 'Five');
        while($row = mysql_fetch_array($result)) {
            if($i++ > count($numbers) {
                throw new NotMoreNumbersSupportedException();
            }
            $varname = 'Name' . $numbers[$i++];
            $$varname = $row['Name'];
        }
        var_dump($NameOne, $NameTwo, $NameThree, $NameFour, $NameFive); //etc
    ?>
    

    Now shoot me.

    点赞 打赏 评论

相关推荐 更多相似问题