简体   繁体   English

MySQL Multi Query存储结果第二次查询

[英]MySQL Multi Query store result second query

I am querying from two different tables in the database here and I can store the values from the first query but how do I store the information from the second query? 我在这里从数据库的两个不同表中查询,可以存储第一个查询的值,但是如何存储第二个查询的信息?

$query  = "SELECT * ";
$query .= "FROM user_account ";
$query .= "WHERE user_id = $user_id ";
$query .= "SELECT * ";
$query .= "FROM user_profile ";
$query .= "WHERE user_id = $user_id ";


if (mysqli_multi_query($mysqli, $query)) {
    do {
        if ($result = mysqli_store_result($mysqli)) {
            while ($row = mysqli_fetch_row($result)) {
                $Firstname = $row['Firstname'];
                $Lastname = $row['Lastname'];
                $Email = $row['Email'];
                $Birthday = $row['Birthday'];
                $Address = $row['Address'];
                $Zip = $row['Zip'];
                $City = $row['City'];
                $State = $row['State'];
                $Country = $row['Country'];
                $Avatar = $row['Avatar']; Will be added later
                $Phone = $row['Phone'];
                $Website = $row['Website'];
                $Member_level = $row['Member_level'];
            }
            mysqli_free_result($result);
        }
        if (mysqli_more_results($mysqli)) {
        }
        while (mysqli_next_result($mysqli)) ;
    }
}

Just the usual way 只是通常的方式

$query = "SELECT * FROM user_account WHERE user_id = $user_id ";
$account = $mysqli->query($query)->fetch_assoc();

$query = "SELECT * FROM user_profile WHERE user_id = $user_id ";
$profile = $mysqli->query($query)->fetch_assoc();

as simple as that. 就如此容易。

I am really wondering why PHP users are inclined to writing SO MUCH code and to using so much intricate ways for the most trifle operations 我真的很想知道为什么PHP用户倾向于编写如此大量的代码并使用如此复杂的方式来进行最琐碎的操作

On a side note, in your particular case you can write a single JOIN query. 附带一提,在您的特定情况下,您可以编写单个JOIN查询。

In your code, why are you using mysqli_free_result function because it will frees the memory associated with a result object so when while loop run it will not show any result. 在您的代码中,为什么要使用mysqli_free_result函数,因为它会释放与结果对象关联的内存,因此,当while循环运行时while它不会显示任何结果。

$query  = "SELECT * FROM user_account WHERE user_id = $user_id ";
$query .= "SELECT * FROM user_profile WHERE user_id = $user_id ";

if (mysqli_multi_query($mysqli, $query)) {
     do {
      if ($result = mysqli_store_result($mysqli)) {
           while ($row = mysqli_fetch_assoc($result)) {
               var_dump($row); //for checking result 
              // Now use array to show your result
          }

     } }while (mysqli_next_result($mysqli)) ;

You should always free your result with mysqli_free_result(), when your result object is not needed anymore. 当不再需要结果对象时,应始终使用mysqli_free_result()释放结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM