You will find a solution to your question on the php.net mysqli bind_result page here:
http://www.php.net/manual/en/mysqli-stmt.bind-result.php
check out the comment by nieprzeklinaj at gmail dot com
he/she provides a function fetch() that will work as a fetch all for prepared mysqli statements (returning the full result set in an array). It will work with a dynamic number of selected fields.
You can add the fetch() function to your php code (of course you may call it whatever you like).
Then to use it in the code you provided above you would do something like:
$prename = "Peter";
$rows = array();
$mysqli = new mysqli($server, $user, $pass, $dbase);
if ($stmt = $mysqli->prepare("select lastname where prename = ? order by prename asc")) {
/* bind parameters for markers */
$stmt -> bind_param("s", $prename);
/* execute query */
$stmt -> execute();
/* call the fetch() function provided by (nieprzeklinaj at gmail dot com) */
$rows = fetch($stmt);
}
/* close connection */
$mysqli -> close();
print json_encode($rows);
UPDATED
FULL
I created a test table named "comment" and gave it a "prename" field and some other random fields just for demonstration purposes:
<?php
//fetch function from php.net (nieprzeklinaj at gmail dot com)
function fetch($result)
{
$array = array();
if($result instanceof mysqli_stmt)
{
$result->store_result();
$variables = array();
$data = array();
$meta = $result->result_metadata();
while($field = $meta->fetch_field())
$variables[] = &$data[$field->name]; // pass by reference
call_user_func_array(array($result, 'bind_result'), $variables);
$i=0;
while($result->fetch())
{
$array[$i] = array();
foreach($data as $k=>$v)
$array[$i][$k] = $v;
$i++;
// don't know why, but when I tried $array[] = $data, I got the same one result in all rows
}
}
elseif($result instanceof mysqli_result)
{
while($row = $result->fetch_assoc())
$array[] = $row;
}
return $array;
}
$prename = "Peter";
$rows = array();
$server = 'localhost';
$user = 'user';
$pass = 'pass';
$dbase = 'mydatabase';
$mysqli = new mysqli($server, $user, $pass, $dbase);
$prename = "Peter";
$rows = array();
if ($stmt = $mysqli->prepare("select * from comment where prename = ? order by prename asc")) {
/* bind parameters for markers */
$stmt -> bind_param("s", $prename);
/* execute query */
$stmt -> execute();
/* call the fetch() function provided by (nieprzeklinaj at gmail dot com) */
$rows = fetch($stmt);
}
else{
//print error message
echo $mysqli->error;
}
/* close connection */
$mysqli -> close();
print json_encode($rows);
output is:
[{"prename":"Peter","comment_id":1,"fullname":"Peter 1","email":"some
email"},{"prename":"Peter","comment_id":2,"fullname":"Peter
2","email":"some
email"},{"prename":"Peter","comment_id":3,"fullname":"Peter
3","email":"some email"}]
database table info (so you can check the output):
mysql> describe comment;
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| prename | varchar(100) | YES | | NULL | |
| comment_id | int(11) | YES | | NULL | |
| fullname | varchar(150) | YES | | NULL | |
| email | varchar(150) | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
mysql> select * from comment;
+---------+------------+----------+------------+
| prename | comment_id | fullname | email |
+---------+------------+----------+------------+
| Peter | 1 | Peter 1 | some email |
| Peter | 2 | Peter 2 | some email |
| Peter | 3 | Peter 3 | some email |
+---------+------------+----------+------------+
$lastname?$lastnameis filled with the data from the database.$rowsarray instead of echoing it out.$rows[] = $r;