Forum Moderators: coopster

Message Too Old, No Replies

Couldn't fetch mysqli stmt

I'm doing something wrong?

         

bethesda

1:04 pm on Dec 7, 2011 (gmt 0)

10+ Year Member



Hello again.
I try to make loop with the results obtained from the database. Code below.
Unfortunately, receives only the first result, no loops, and error message:


Warning: mysqli_stmt::fetch() [mysqli-stmt.fetch]: Couldn't fetch mysqli_stmt in /home/#*$!/#*$!/htdocs/projects/page/script.php on line 100



$db = new mysqli('X', 'X', 'X', 'X');
$db -> query("SET NAMES 'latin2'");
$stmt = $db->stmt_init();
if($stmt->prepare("SELECT `table`, `table2`, `table3`, `table4` FROM `database` WHERE `user` = ?")) {
$stmt->bind_param('s', $u);
$u = "$username";
$stmt->execute();
$stmt->bind_result($a_var, $b_var, $c_var, $d_var);
while($stmt->fetch()) {
echo '
<tr class="main_row_color">
<td style="width:2%;"><a href="#" style="color: #fff;"><b>ID</b></a></td>
<td style="width:20%; padding-left: 10px;" colspan="2"><a href="#" style="color: #fff;"><b>Something</b></a></td>
<td style="width:20%;"><a href="" style="color: #fff;"><b>Something2</b></a></td>
<td style="width:20%;"><a href="" style="color: #fff;"><b>Status</b></a></td>
<td style="width:1%;"><b>Something3</b></td>
</tr>
<tr class="row1_color">
<td style="color: #000;"><font style="margin-left: 2px;">' . stripslashes($a_var) .'</font></td>
<td style="width:1%;"></td>
<td style="color: #7f7f7f;"><font style="margin-left: 5px;">' . stripslashes($b_var) .'</font></a></td>
<td><font style="margin-left: 5px;">' . stripslashes($c_var) .'</font></td>
<td style="color: #7f7f7f;"><font style="margin-left: 5px;">' . stripslashes($d_var) .'</font></a></td>
<td><center><input name="do[]" type="checkbox" id="do[]" value="287" class="searchreg"></center></td>
</tr>
<tr class="row2_color">
<td style="color: #000;"><font style="margin-left: 2px;">' . stripslashes($a_var) .'</font></td>
<td style="width:1%;"></td>
<td style="color: #7f7f7f;"><font style="margin-left: 5px;">' . stripslashes($b_var) .'</font></a></td>
<td><font style="margin-left: 5px;">' . stripslashes($c_var) .'</font></td>
<td style="color: #7f7f7f;"><font style="margin-left: 5px;">' . stripslashes($d_var) .'</font></a></td>
<td><center><input name="do[]" type="checkbox" id="do[]" value="286" class="searchreg"></center></td>
</tr>
';
$stmt->close();
$db->close();
}} else {
echo '<p>Error<br /></p><p>' . mysql_error() . '</p>';
}


I'm doing something wrong?
Please help.

bethesda

4:14 pm on Dec 7, 2011 (gmt 0)

10+ Year Member



Ok, my bad - i forgot about
}
at the end of code:


</tr>
';
}
$stmt->close();
$db->close();
} else {
echo '<p>Error<br /></p><p>' . mysql_error() . '</p>';
}

eelixduppy

4:42 pm on Dec 7, 2011 (gmt 0)



Glad you were able to resolve the issue :)

bethesda

4:55 pm on Dec 7, 2011 (gmt 0)

10+ Year Member



:) i have another problem.

I wanted to do background changing tables.
Responsible code:


row1_color and row2_color


How can I put it to a variable that will change with each new record is displayed?

eelixduppy

5:08 pm on Dec 7, 2011 (gmt 0)




$rowCount = 0;
while(...) {
...do stuff...
if($rowCount++ % 2 == 0) { //even rows
//print color one row
} else {
// print color two row
}
...do more stuff...
}


The modulus operator (%) is your friend :)

bethesda

6:15 pm on Dec 7, 2011 (gmt 0)

10+ Year Member



Yes but how can i put it to this code ?


$db = new mysqli('x', 'x', 'x', 'x0');
$db -> query("SET NAMES 'latin2'");
$stmt = $db->stmt_init();
if($stmt->prepare("SELECT `table`, `table2`, `table3`, `table4` FROM `database` WHERE `user` = ?")) {
$stmt->bind_param('s', $u);
$u = "$username";
$stmt->execute();
$stmt->bind_result($a_var, $b_var, $c_var, $d_var);
while($stmt->fetch()) {
echo '
<tr class="">
<td style="width:2%;"><a href="#" style="color: #fff;"><b>ID</b></a></td>
<td style="width:20%; padding-left: 10px;" colspan="2"><a href="#" style="color: #fff;"><b>Something</b></a></td>
<td style="width:20%;"><a href="" style="color: #fff;"><b>Something2</b></a></td>
<td style="width:20%;"><a href="" style="color: #fff;"><b>Status</b></a></td>
<td style="width:1%;"><b>Something3</b></td>
</tr>
';
}


Given that the code of style name must be in this place:


<tr class="here must be var">

bethesda

7:22 pm on Dec 7, 2011 (gmt 0)

10+ Year Member



I'm asking a dump questions - thank you for #:4395258.
It works.

No please explain how it work ? ;)

We give a variable a value of zero.
That I don't understand - if ($ rowCount + +% 2 == 0)
If the variable will have a positive value, divide it into 2?

eelixduppy

7:26 pm on Dec 7, 2011 (gmt 0)



$rowCount starts at 0 and increments each time that expression is read ($rowCount++).

The trick comes from the use of the modulus operator, %, which provides us with the remainder after division. For example,

0 % 2 = 0
1 % 2 = 1
2 % 2 = 0
3 % 2 = 1
etc...

When the result is 0, we are in an even row in the table.


[php.net...]

bethesda

7:52 pm on Dec 7, 2011 (gmt 0)

10+ Year Member



Cooool ! Thanks very much..