我正在使用一个文件,我正在尝试一个接一个地执行多个select语句并插入一些值。到目前为止,插入和选择我已经合作但是当试图让最后一个SELECT工作时,我没有得到任何价值。检查工作台中的SQL查询,一切正常。这是代码:
$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];
}
我不想使用$ mysqli-> multi_query,因为我遇到过以前的问题。有什么建议?是的,我知道命名约定是紧密命名的......它们很快就会改变。
答案 0 :(得分:2)
你的代码毫无意义。您反复构建/重新构建$insert_int-User
查询,然后实际上不执行查询。 $ select_userid查询将仅使用第一个查询中的LAST检索的$ row [0]值。由于最后一个“行”将是一个布尔值FALSE,表示没有更多数据可用$row[0]
实际上将尝试将该布尔值FALSE作为数组取消引用。
由于您实际上只有2个选择查询(或者至少尝试过),为什么不重写为单个双值连接查询?
SELECT iduser, idaccount
FROM account
LEFT JOIN user ON user.iduser=account.idaccount
WHERE email='$Email';
答案 1 :(得分: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";
}