<?php
$stmt = $mysqli->prepare("SELECT * FROM test WHERE dataStr = ? LIMIT 20");
$stmt->bind_param("s", $search);
$stmt->execute();
$stmt->store_result();
echo "rows are " . $stmt->num_rows;
$rs = $stmt->get_result();
while($r = $rs->fetch_object()) print_r($r);
?>
When I call $stmt->store_result() to let $stmt->num_rows work, the subsequent call of $stmt->get_result() returns FALSE. If I comment the store_result() row, everything works fine (except of course num_rows)
How can I access the mysqli_result class instance, and its fetch_array(), fetch_assoc(), fetch_object() methods after I called $stmt->store_result() ?
Try this,
<?php
$stmt = $mysqli->prepare("SELECT * FROM test WHERE dataStr = ? LIMIT 20");
$stmt->bind_param("s", $search);
$stmt->execute();
$stmt->store_result();
echo "rows are " . $stmt->num_rows;
$result = $stmt->get_result();
while ($row = $result->fetch_assoc())
{
echo 'ID: '.$row['id'].'<br>';
}
$stmt->free_result();
?>
Related
Below is my php code:
public function getMainChatList($myPhoneNo){
$stmt = $this->conn->prepare("SELECT receiverPhoneNo,name FROM users,friend WHERE users.phoneNo=friend.receiverPhoneNo AND senderPhoneNo=? AND chatted = 'y' ORDER BY update_time DESC");
$stmt->bind_param("s", $myPhoneNo);
$stmt->execute();
$stmt->store_result();
$result = array();
while($row = $stmt->fetch()){
array_push($result,array('receiverPhoneNo'=>$row['receiverPhoneNo'],'name'=>$row['name'],));
}
//echo json_encode(array("result"=>$result));
echo json_encode($result);
echo json_last_error();
$stmt->close();
}
And the json return
[{"receiverPhoneNo":null,"name":null},{"receiverPhoneNo":null,"name":null}]0
json_last_error() returns 0. I have no idea why it return null.
And I execute the sql statement on xampp MySQL server directly.
Below is the result.
result.jpg
Thanks!
Just use bind_result() instead:
public function getMainChatList($myPhoneNo)
{
$stmt = $this->conn->prepare("
SELECT receiverPhoneNo, name FROM users, friend
WHERE users.phoneNo = friend.receiverPhoneNo
AND senderPhoneNo = ?
AND chatted = 'y'
ORDER BY update_time DESC
");
$stmt->bind_param('s', $myPhoneNo);
$stmt->execute();
// bind
$stmt->bind_result($receiverPhoneNo, $name);
$result = array();
while($stmt->fetch()){
$result[] = array(
'receiverPhoneNo' => $receiverPhoneNo,
'name' => $name,
);
}
echo json_encode($result);
}
this should put the associative array from the entire query directly into your $result array
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
I am having some trouble getting some values from my sql query in php
This is my php code:
<?php
include "init.php";
$stmt = "SELECT email FROM Customer ";
$result = $dbcon -> prepare($stmt);
$result->execute();
$result->store_result();
while($result -> fetch()){
// This line below works
echo "Works";
// This doesn't work
echo $result['email'];
}
?>
You need to use bind_result() to retrieve the values returned by the query.
include "init.php";
$stmt = "SELECT email FROM Customer ";
$result = $dbcon -> prepare($stmt);
$result->execute();
$result->store_result();
$result->bind_result($email);
while($result -> fetch()){
// This line below works
echo "Works";
echo $email;
}
To start, mysqli::prepare doesn't return a mysqli_result, it returns a mysqli_stmt. You then have to execute that mysqli_stmt and either fetch directly from it into a bound variable, or extract a mysqli_result and fetch from that. There are several ways to do what you want:
$qry = "SELECT email FROM Customer ";
$stmt = $dbcon->prepare($qry);
$stmt->execute();
$stmt->bind_result($email); // Bind variable to statement
while($stmt->fetch()) // Iterate through statement
{
// This line below works
echo "Works";
echo $email;
}
Or:
$qry = "SELECT email FROM Customer ";
$stmt = $dbcon->prepare($qry);
$stmt->execute();
$rslt = $stmt->get_result(); // Retrieve result set
while($row = $rslt->fetch_assoc()) // Iterate through result
{
// This line below works
echo "Works";
echo $row['email'];
}
Or:
$qry = "SELECT email FROM Customer ";
$stmt = $dbcon->prepare($qry);
$stmt->execute();
$rslt = $stmt->get_result(); // Retrieve result
$resArray = $rslt->fetch_all(MYSQLI_ASSOC)); // Convert to array
foreach ($resArray as $row) // Iterate through array
{
// This line below works
echo "Works";
echo $row['email'];
}
As is my custom, I leave error handling as an exercise for the reader.
Here is the code that I have problem with :
function getQuestions($mysqli, $subjectIdOrCode, $isStudent){
$idSubject = getSubjectId($mysqli, $subjectIdOrCode);
//writing the statement
$query = "select id,description from questions where id_subjects = ? and
is_for_student = ?";
//prepare statement
$stmt = $mysqli->prepare($query);
//binding the statement
$stmt->bind_param("si", $idSubject, $isStudent);
//execute the statement
$stmt->execute();
//get the result
$result = $stmt->get_result();
//store the result
$stmt->store_result();
//get the number of rows
$noOfRows = $stmt->num_rows();
$questions = null;
for ($i = 0; $i < $noOfRows; $i++) {
echo "test";
$row = $result->fetch_array(MYSQLI_ASSOC);
$questions[$i]['id'] = $row['id'];
$questions[$i]['sno'] = $i+1;
$questions[$i]['description'] = $row['description'];
}
return $questions;
}
When this function is called, nothing is printed (which implies that $noOfRows is 0). Now, when the line :
//get the result
$result = $stmt->get_result();
is removed, it prints test along with some error message that $result is undefined (which clearly shows that $noOfRows is > 0).
Where have I made a mistake in my code?
Thanks in advance!
OK ... I have got your point. I have just put code to fetch num_rows
//get the number of rows
$noOfRows = $stmt->num_rows;
I was wondering why I am receiving syntax errors on lines 9 and 16 from my PHP/PDO coding.
My code is as follows:
<?php
// Sandbox Functions
// PDO example
function get_page($dbc, $pg) {
$sql = "SELECT * FROM pages WHERE page = '$pg' AND status = 1 LIMIT 1";
$stmt = $dbc->prepare($sql);
$stmt->execute();
$row = $stmt->fetch()
echo '<h1>'.$page['title'].'</h1>';
echo '<div class="content">'.$page['body'].'</div>';}
function get_title($dbc, $pg) {
$sql = "SELECT title FROM pages WHERE page = '$pg' AND status = 1 LIMIT 1";
$stmt = $dbc->prepare($sql);
$stmt->execute();
$row = $stmt->fetch()
return $page['title'];}
?>
Put simicolon after that $stmt->fetch() like : $row = $stmt->fetch();
Missing semicolons...
<?php
// Sandbox Functions
// PDO example
function get_page($dbc, $pg) {
$sql = "SELECT * FROM pages WHERE page = '$pg' AND status = 1 LIMIT 1";
$stmt = $dbc->prepare($sql);
$stmt->execute();
$row = $stmt->fetch(); //missing ;
echo '<h1>'.$page['title'].'</h1>';
echo '<div class="content">'.$page['body'].'</div>';}
function get_title($dbc, $pg) {
$sql = "SELECT title FROM pages WHERE page = '$pg' AND status = 1 LIMIT 1";
$stmt = $dbc->prepare($sql);
$stmt->execute();
$row = $stmt->fetch(); //missing ;
return $page['title'];}
Your statement:
$row = $stmt->fetch()
Is not having a semicolon:
$row = $stmt->fetch();
Same on the line 16:
$row = $stmt->fetch()
To
$row = $stmt->fetch();
I would like to use the row that I have selected and echoed to insert to a table again.
So what I want is row to_user. how should I define $to_user to get to_user row?
$to_user = $row['to_user']; //this gives me undefined variable error
$stmt = $mydb->prepare("insert into `messages`(`to_user`) values(?)");
echo $mydb->error;
$stmt->bind_param('s', $to_user);
$stmt->execute();
$stmt = $mydb->prepare("SELECT * FROM messages where id = ? ");
$stmt->bind_param('s', $id);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
echo $row['to_user'];}
$row['to_user'];
$stmt = $mydb->prepare("insert into `messages`(`to_user`) values(?)");
echo $mydb->error;
$stmt->bind_param('s', $row['tu_user']);
$stmt->execute();
//get the last inserted id and store it in a var
$lastInserted = $mydb->lastInsertId();
$stmt = $mydb->prepare("SELECT * FROM messages where id = ? ");
$stmt->bind_param('s', $lastInserted);
$stmt->execute();
$result = $stmt->get_result();
//for testing
//print_r($result);
// echo $result;
while ($row = $result->fetch_assoc()) {
echo $row['to_user'];}