doupo2157
2014-01-11 23:43
浏览 37
已采纳

使用MySQLi和PHP将行分配给变量

I'm just starting to dab into MySQLi after using MySQL for a while. I found this little code snippet on a tutorial, and just had a question about it (since Google won't return any results for me on this...).

<?php 

$link = mysqli_connect("127.0.0.1", "user", "password", "world"); 

if (!$link)
{
    $error = mysqli_connect_error();
    $errno = mysqli_connect_errno();
    print "$errno: $error
";
    exit();
}

$query = "SELECT Name, Population, Continent FROM Country WHERE Continent=? ORDER BY Name LIMIT 1";

$stmt = mysqli_stmt_init($link);
if(!mysqli_stmt_prepare($stmt, $query))
{
    print "Failed to prepare statement
";
}
else
{
    mysqli_stmt_bind_param($stmt, "s", $continent);

    $continent_array = array('Europe','Africa','Asia','North America');

    foreach($continent_array as $continent)
    {
        mysqli_stmt_execute($stmt);
        $result = mysqli_stmt_get_result($stmt);
        while ($row = mysqli_fetch_array($result, MYSQLI_NUM))
        {
            foreach ($row as $r)
            {
                print "$r ";
            }
            print "
";
        }
    }
}
mysqli_stmt_close($stmt);
mysqli_close($link);
?>

Courtesy of http://www.php.net/manual/en/mysqli-stmt.get-result.php

If I'm understanding this correctly then $r will now do an array of all of the results found in all of the rows called. How can I assign a variable to each row? Can I still do something similar like I did in MySQL?

$username = $row['username'];

Or is there something completely different now?

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

在使用MySQL一段时间之后,我才开始尝试使用MySQLi。 我在教程中找到了这个小代码片段,并且对它有一个疑问(因为Google不会在此返回任何结果......)。

 &lt  ;?php 
 
 $ link = mysqli_connect(“127.0.0.1”,“user”,“password”,“world”);  
 
if(!$ link)
 {
 $ error = mysqli_connect_error(); 
 $ errno = mysqli_connect_errno(); 
 print“$ errno:$ error 
”; 
 exit(); \  n} 
 
 $ query =“SELECT Name,Population,Continent FROM Country WHERE Con​​tinent =?ORDER BY Name LIMIT 1”; 
 
 $ stmt = mysqli_stmt_init($ link); 
if(!mysqli_stmt_prepare($ stmt)  ,$ query))
 {
 print“无法准备语句
”; 
} 
else 
 {
 \ mysqli_stmt_bind_param($ stmt,“s”,$ continent); 
 
 $ continent_array = 数组('欧洲','非洲','亚洲','北美'); 
 
 foreach($ continent_array as $ continent)
 {
 mysqli_stmt_execute($ stmt); 
 $ result = mysqli_stmt_get_result(  $ stmt); 
 while($ row = mysqli_fetch_array($ result,MYSQLI_NUM))
 {
 foreach($ row as $ r)
 {
 print“$ r”; 
} 
 print“  
“; 
} 
} 
} 
mysqli_stmt_close($ stmt); 
mysqli_close($ link); 
?&gt; 
   
 
 

礼貌 of http://www.php.net/manual/en/mysqli-stmt.get-result.php < 如果我正确理解了这一点,那么 $ r 现在会在所有被调用的行中找到所有结果的数组。 如何为每一行分配变量? 我还可以像在MySQL中那样做类似的事情吗?

  $ username = $ row ['username']; 
   
 \  n 

或者现在有什么完全不同的东西吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • donglu9825 2014-01-11 23:57
    已采纳

    for example; we have Name, Population, Continent columns that we are selecting.

    Name | Population | Continent
    X    |  1         |  A
    Y    |  2         |  B
    Z    |  3         |  C
    

    and these are the rows returned as result of this query..

    in first iteration, you will get $row like this

    array("Name"=> "X", "Population"=>"1", "Continent"=>"A");
    

    and in second iteration, you will get $row like this

    array("Name"=> "Y", "Population"=>"2", "Continent"=>"B");
    

    and so on. so, you can use it as you mentioned(like $row["Name"])

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dtmm0148603 2014-01-11 23:48

    Within that while loop, you can use the variable $row and it will represent the contents of the current row. So, you can create an array before the loop and assign each row to a new index in the array within that loop. Once the loop is done executing, your array will be populated.

    $arrayVar = array();
    while ($row = mysqli_fetch_array($result, MYSQLI_NUM))
    {
      $arrayVar[] = $row[];
    }
    
    评论
    解决 无用
    打赏 举报