PHP SQL虽然循环不起作用

时间:2014-06-10 15:37:10

标签: php mysql sql

我有一个代码可以从favourites获取所有用户的收藏夹,然后它应该使用该信息从menus获取信息以将其显示为图片。

它应该做的只是显示用户的收藏夹,但目前它只会在收藏夹中有很多时显示一张图片。

<?php
$con=mysqli_connect("localhost","UN","PW","DB");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$id=$_SESSION['user']['id'];
$result = mysqli_query($con,"SELECT * FROM favourites WHERE user='$id'");

while($row = mysqli_fetch_array($result)) {
    $code=$row['gamecode'];
    $con=mysqli_connect("localhost","UN","PW","DB");
    // Check connection
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $result = mysqli_query($con,"SELECT * FROM menus WHERE code='$code'");

    while($row = mysqli_fetch_array($result)) {
?>
<a href="<?php echo $row['link']; ?>">
<img src="<?php echo $row['picture']; ?>" alt="<?php echo $row['game']; ?>"      height="120" width="150" class="fade"></a>
<?php
    }
    mysqli_close($con);
}

mysqli_close($con);

?>

3 个答案:

答案 0 :(得分:4)

您通过重新连接到循环中的数据库来终止查询

$con = mysqli_connect(...) // connection #1
$result = mysqli_query(...);
while($row = mysqli_fetch($result)) {
   $con = mysqli_connect(...); // connection #2

当您再次连接时,您将终止原始连接,这会终止您的查询。

除非您需要使用不同凭据连接两次,否则无需第二次连接。一个连接可以处理多个查询。

顺便提一下,如果您使用了不同的连接句柄变量,例如

$con = mysqli_connect(...);
    $othercon = mysqli_connect(...);
你不会遇到这个问题。您可以有多个连接,但不能使用相同的单个变量。

答案 1 :(得分:1)

您有两个MySQL连接,其变量名称相同$con以及$result&amp; $row。所以我只是更改内部循环中的变量名称,这样它们就不会发生冲突。都应该工作; $con_inside$result_inside&amp; $row_inside

我还在您的or die(mysqli_error());行中添加了mysqli_query,以便在您的查询终止时可以返回错误。

<?php
$con = mysqli_connect("localhost","UN","PW","DB");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$id = $_SESSION['user']['id'];

$result = mysqli_query($con, "SELECT * FROM favourites WHERE user='$id'") or die(mysqli_error());

while ($row = mysqli_fetch_array($result)) {

  $code = $row['gamecode'];

  $con_inside = mysqli_connect("localhost","UN","PW","DB");

  // Check connection
  if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

  $result_inside = mysqli_query($con_inside, "SELECT * FROM menus WHERE code='$code'") or die(mysqli_error());

  while($row_inside = mysqli_fetch_array($result_inside)) {
?>

  <a href="<?php echo $row_inside['link']; ?>">
  <img src="<?php echo $row_inside['picture']; ?>" alt="<?php echo $row_inside['game']; ?>" height="120" width="150" class="fade"></a>

<?php
  }

  mysqli_close($con_inside);

}

mysqli_close($con);

此外,这是一个稍微重写的代码版本,应该可以更好地工作。我从循环和放大器中删除了内部数据库连接。将它设置在脚本的顶部。不必在每个循环上重置连接。另外,我使用mysqli_stmt_bind_param添加了行,这是使用mysqli_*查询而不是设置字符串的首选方式。还使用mysqli_free_result释放每个循环上的查询内存。这些都是小事,但它们会增加更好的代码。

<?php

// Main DB connection.
$con = mysqli_connect("localhost","UN","PW","DB") or die(mysqli_connect_error());

// Inside DB connection.
$con_inside = mysqli_connect("localhost","UN","PW","DB") or die(mysqli_connect_error());

// Set the $id variable.
$id = $_SESSION['user']['id'];

// Set the query string.
$query = "SELECT * FROM favourites WHERE user='$id'";

// Bind the values to the query.
mysqli_stmt_bind_param($query, 's', $id);

// Get the result.
$result = mysqli_query($con, $query) or die(mysqli_error());

// Roll through the results.
while ($row = mysqli_fetch_array($result)) {

  // Set the $code variable.
  $code = $row['gamecode'];

  // Set the query string.
  $query_inside = "SELECT * FROM menus WHERE code='$code'";

  // Bind the values to the query.
  mysqli_stmt_bind_param($query_inside, 's', $code);

  // Get the result.
  $result_inside = mysqli_query($con_inside, $query_inside) or die(mysqli_error());

  // Roll through the results.
  while($row_inside = mysqli_fetch_array($result_inside)) {
?>

  <a href="<?php echo $row_inside['link']; ?>">
  <img src="<?php echo $row_inside['picture']; ?>" alt="<?php echo $row_inside['game']; ?>" height="120" width="150" class="fade"></a>

<?php
  }

  // Free the result set.
  mysqli_free_result($result_inside);

  // Close the connection.
  mysqli_close($con_inside);

}

// Free the result set.
mysqli_free_result($result);

// Close the connection.
mysqli_close($con);

答案 2 :(得分:0)

查看它是否使用不同的结果变量和仅一个数据库连接。

<?php
$con=mysqli_connect("localhost","UN","PW","DB");
// Check connection
if (mysqli_connect_errno()) {  echo "Failed to connect to MySQL: " . mysqli_connect_error(); }
$id = $_SESSION['user']['id'];
$result1 = mysqli_query($con,"SELECT * FROM favourites WHERE user='$id'");
while($row = mysqli_fetch_array($result1)) {
    $result2 = mysqli_query($con,"SELECT * FROM menus WHERE code='".$row['gamecode']."");
    while($row2 = mysqli_fetch_array($result2)) { ?>
<a href="<?php echo $row2['link']; ?>">
<img src="<?php echo $row2['picture']; ?>" alt="<?php echo $row2['game']; ?>"      height="120" width="150" class="fade"></a>
<?php }
}
mysqli_close($con);
?>