0

I'm working with a file and I'm attempting to do multiple select statements one after another and insert some values. So far the insert and the select I've got working together but when attempting to get the last SELECT to work I get no value. Checking the SQL query in workbench and everything works fine. Here's the code:

$id = "SELECT idaccount FROM `animator`.`account` WHERE email = '$Email'";
$result = mysqli_query($dbc, $id) or die("Error: ".mysqli_error($dbc));
while($row = mysqli_fetch_array($result))
{
    echo $row[0];
    $insert_into_user = "INSERT INTO `animator`.`user` (idaccount) VALUES ('$row[0]')";
}
    $select_userid = "SELECT iduser FROM `animator`.`user` WHERE iduser = '$row[0]'";

    $results = mysqli_query($dbc, $select_userid) or die("Error: ".mysqli_error($dbc));
while($rows = mysqli_fetch_array($results))
{    
    echo $rows[0];
}

I do not want to use $mysqli->multi_query because of previous problems I ran into. Any suggestions? And yes I know the naming conventions are close naming... They will be changed shortly.

4

2 回答 2

2

你的代码没有意义。您反复构建/重新构建$insert_int-User查询,然后从不实际执行查询。$select_userid 查询将仅使用从第一个查询中检索到的最后一个 $row[0] 值。由于最后一个“行”将是一个布尔值 FALSE,表示没有更多数据可用,$row[0]因此实际上将尝试将该布尔值 FALSE 作为数组取消引用。

由于您实际上只执行了 2 个选择查询(或至少尝试执行),为什么不重写为单个双值连接查询呢?

SELECT iduser, idaccount
FROM account
LEFT JOIN user ON user.iduser=account.idaccount
WHERE email='$Email';
于 2013-09-20T19:11:12.313 回答
0

我不确定你在你的代码中到底想做什么,但看看这个......

// create select statement to get all accounts where email=$Email from animator.account
$id_query = "SELECT idaccount FROM animator.account WHERE email = '$Email'";
echo $id_query."\n"; 

// run select statement for email=$mail 
$select_results = mysqli_query($dbc, $id_query) or die("Error: ".mysqli_error($dbc));


// if we got some rows back from the database... 
if ($select_results!==false)
{ 
   $row_count = 0; 

   // loop through all results 
   while($row = mysqli_fetch_array($result))
   {
       $idaccount = $row[0]; 

       echo "\n\n-- Row #$row_count --------------------------------------------\n";
       echo $idaccount."\n";

       // create insert statement for this idaccount
       $insert_into_user = "INSERT INTO animator.user (idaccount) VALUES ('$idaccount')"; 
       echo $insert_into_user."\n";

       // run insert statement for this idaccount
       $insert_results = mysqli_query($dbc, $insert_into_user) or die("Error: ".mysqli_error($dbc)); 

       // if our insert statement worked... 
       if ($insert_results!==false)
       {
          // Returns the auto generated id used in the last query
          $last_inisert_id = mysqli_insert_id($dbc); 
          echo $last_inisert_id."\n"; 
       }
       else 
       { 
         echo "insert statement did not work.\n"; 
       }

       $row_count++; 
   }

}
// we didn't get any rows back from the DB for email=$Email 
else 
{ 
   echo "select query returned no results...? \n"; 
}
于 2013-09-20T21:39:21.607 回答