0
<?php
//wall ===================================================
if($_POST['submit'] == "submit"){
// connect to the database
include("dbinfo.inc.php");

  $client_id = $_POST['client_id'];
  echo $client_id;
  $query="select * from messages where client_id='$client_id'";
  $result = $mysqli->query( $query );
  $row = $result->fetch_assoc();
                                        while ($row = $result->fetch_object())
                                        {
                                                $id = $row->msg_id;
                                                $mes = $row->message;
                                                $mes = nl2br($mes);
                                                $cdate = $row->date_post;
                                                $msg ="{$mes} <br> . {$cdate}";

//wall ===================================================
?>
<li class="bar<?php echo $id; ?>">
<div align="left" class="post_box">
<span style="padding:10px"><?php echo $msg; ?> </span>
<span class="delete_button"><a href="#" id="<?php echo $id; ?>" class="delete_update">X</a></span>
<span class='feed_link'><a href="#" class="comment" id="<?php echo $id; ?>">comment</a></span>
</div>
<div id='expand_box'>
<div id='expand_url'></div>
</div>
<div id="fullbox" class="fullbox<?php echo $id; ?>">
<div id="commentload<?php echo $id; ?>" >

</div>
<div class="comment_box" id="c<?php echo $id; ?>">
<form method="post" action="" name="<?php echo $id; ?>">
<textarea class="text_area" name="comment_value" id="textarea<?php echo $id; ?>">
</textarea><br />
<input type="submit" value=" Comment " class="comment_submit" id="<?php echo $id; ?>"/>
</form>
</div>
</div>
</li>
<?php                                   }
//wall ===================================================
     $mysqli->close();
}
//wall ===================================================
?>

My script is supposed to output all data that is equivalent to the input of the user.

EX input client_id "2"

query:

msg_id  message  date_sent  client_id

1        a        1/1/1         1  
2        b        2/2/2         1  
3        c        3/3/3         1
4        d        1/2/3         1   
5        e        2/2/2         2    
7        e        2/2/2         2    
8        g        2/2/2         2    
9        f        8/8/8         3

it will only display

  7        e        2/2/2         2    
  8        g        2/2/2         2

and skip the very 1st one which is

 5        e        2/2/2         2

if I input client_id "3"

there will be no output, but it should have displayed:

 9        f        8/8/8         3

could you please check my script to see what I'm doing wrong here?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
telexper
  • 2,381
  • 8
  • 37
  • 66

2 Answers2

3

You are calling

$row = $result->fetch_assoc();

before you start looping on the results:

while ($row = $result->fetch_object()) {
...
}

This has the effect of skipping the first row in the result set (since you don't do anything with $row from the first call).

Peter Gluck
  • 8,168
  • 1
  • 38
  • 37
0

Use either fetch_assoc() or fetch_object(). Here is an example from PHP manual:

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";

if ($result = $mysqli->query($query)) {

    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {
        printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
    }

    /* free result set */
    $result->free();
}

/* close connection */
$mysqli->close();
Vaibhav Desai
  • 2,618
  • 1
  • 16
  • 16