2010-02-25 49 views
2

以下是我正在使用的代碼。另外,他們都做他們應該做的,但是當我嘗試使用第一條語句的結果時,它不返回任何內容。我知道第一條語句總是返回正確的數據。有人能告訴我我做錯了什麼嗎?由於Mysqli select語句與其他mysqli select語句的結果不能很好地搭配

$connection = mysqli_connect($hostname, $username, $password, $dbname); 

$sql = "SELECT banner".$number_id."_id FROM newcms_projectbanners WHERE region_id = ?"; 

$stmt = mysqli_prepare($connection, $sql); 

    mysqli_stmt_bind_param($stmt, "s", $region_id); 
    mysqli_stmt_execute($stmt); 
    mysqli_stmt_bind_result($stmt, $banner_id); 

    // display the results 
    mysqli_stmt_fetch($stmt); 

$sql1 = "SELECT `title`, `active`, `linkto` FROM newcms_banners WHERE id = ?"; 

$stmt1 = mysqli_prepare($connection, $sql1); 

    mysqli_stmt_bind_param($stmt1, "s", $banner_id); 
    mysqli_stmt_execute($stmt1); 
    mysqli_stmt_bind_result($stmt1, $title, $active, $linkto); 

    // display the results 
    mysqli_stmt_fetch($stmt1); 

編輯

經進一步檢查,似乎我不能運行以這種方式兩個語句。什麼是正確的做法?謝謝

回答

1

下面的代碼是訣竅。

$post_stmt = $db_connection->prepare("SELECT banner".$number_id."_id FROM newcms_projectbanners WHERE region_id = ?"); 
$comment_stmt = $db_connection->prepare("SELECT title, active, linkto FROM newcms_banners WHERE id = ?"); 

$post_stmt->bind_param('i', $region_id); 
if ($post_stmt->execute()) 
{ 
    $post_stmt->store_result(); 
    $post_stmt->bind_result($banner_id); 

    if ($post_stmt->fetch()) 
    { 
     $comments = array(); 

     $comment_stmt->bind_param('i', $banner_id); 
     if ($comment_stmt->execute()) 
     { 
     $comment_stmt->bind_result($title, $active, $linkto); 
     while ($comment_stmt->fetch()) 
     { 
      $html = "<fieldset title='$label'>" . PHP_EOL; 
      $html .= "<form action='$action' id='$id' method='post' class='$class'>" . PHP_EOL; 
      $html .= "<label for'title'>Title</label>" . PHP_EOL; 
      $html .= "<input type='text' name='title' value='$title' /><br />" . PHP_EOL; 
      $html .= "<label for'active'>Active</label>" . PHP_EOL; 
      $html .= "<input type='checkbox' name='active' checked /><br />" . PHP_EOL; 
      $html .= "<label for'linkto'>Link to</label>" . PHP_EOL; 
      $html .= "<input type='text' name='linkto' value='$linkto' /><br />" . PHP_EOL; 
      $html .= "<input type='hidden' name='hidden' value='$banner_id'>" . PHP_EOL; 
      $html .= "<input type='submit' value'Done'>" . PHP_EOL; 
      $html .= "</form>" . PHP_EOL; 
      $html .= "</fieldset>" . PHP_EOL; 

     } 
     } 
     else 
     { 
     printf("Comment statement error: %s\n", $comment_stmt->error); 
     } 
    } 

    $post_stmt->free_result(); 
} 
else 
{ 
printf("Post statement error: %s\n", $post_stmt->error); 
} 

$post_stmt->close(); 
$comment_stmt->close(); 

$db_connection->close();