php - Issues outputting data after prepared statement with loops -


i having major difficulties figuring out doing wrong in while , foreach loops in code below. having tendency mix object-oriented , procedural mqsqli, everytime think have right, error.

what doing wrong in loops have in code?

right error

warning: mysqli::query() expects parameter 1 string,  

full code

try { $con = new mysqli("localhost", "", "", ""); if (mysqli_connect_errno()) {     throw new exception("connect failed: %s\n", mysqli_connect_error());     exit(); } $cid = $_get['cid']; $tid = $_get['tid']; $userid = ( isset( $_session['user'] ) ? $_session['user'] : "" );     echo $cid . "<br>";     echo $tid; //prepare if ($stmt = $con->prepare("select * forum_topics `category_id`=? , `id`=? limit 1")) {      $stmt->bind_param("ii", $cid, $tid);     //$stmt->fetch();      if (!$stmt) {         throw new exception($con->error);     } } $stmt->store_result(); $numrows = $stmt->num_rows; if($numrows == 1){     echo "<table width='100%'>";     if ( $_session['user'] ) {          echo "<tr><td colspan='2'><input type='submit' value='add reply' onclick=\"window.location =      'forum_post_reply.php?cid=".$cid."$tid=".$tid."'\"> <hr />";     } else {         echo "<tr><td colspan='2'><p>please log in add reply</p><hr /></td></tr>";     }  foreach($stmt $row) {      //prepared select stmt forum posts     if($stmt2 = $con->prepare("select * forum_posts `category_id`=? , `topic_id`=?")) {     //var_dump($stmt2);          $stmt2->bind_param("ii", $cid, $tid);         $stmt2->execute();     }        if ($result = $con->query($stmt)) {         while ($row2 = $result->fetch_assoc() ) {             echo "<tr><td valign='top' style='border: 1px solid #000000;'>             <div style='min-height: 125px;'>".$row['topic_title']."<br />             ".$row2['post_creator']." - " .$row2['post_date']. "<hr />" . $row2['post_content'] ."</div></td>             <td width='200' valign='top' align='center' style='border: 1px solid #000000;'>user info here!</td></tr>             <tr><td colspan='2'><hr /></td></tr>";         } } }        }   else {     echo "<p>this topic not exist.</p>";     } 

if ($stmt = $con->prepare("select * forum_topics `category_id`=? , `id`=? limit 1")) {  $stmt->bind_param("ii", $cid, $tid); //$stmt->fetch();  if (!$stmt) {     throw new exception($con->error); } }      $stmt->store_result();     $numrows = $stmt->num_rows; 

at no point execute $stmt before loop through foreach($stmt $row) {.

you'll want throw in there:

$stmt->execute()

your logic little on place. you're looping through result of query never execute, try re-query original $stmt here:

if ($result = $con->query($stmt)) {

edit: after chatting you, need edit original query query specific columns can reference them this:

if ($stmt = $con->prepare("select topic_creator forum_topics `category_id`=? , `id`=? limit 1")) { 

...

$stmt->bind_result($topic_creator);  while ($stmt->fetch()) {     echo "tc: " . $topic_creator . "<br>";  } 

you can take , apply sub queries within while loop.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -