I am receiving a fatal error in my php/mysqli code which states that on line 46:
Fatal error: Call to undefined method mysqli_stmt::fetch_assoc() in ...
I just want to know how can I remove this fatal error?
The line of code it is pointing at is here:
$row = $stmt->fetch_assoc();
ORIGINAL CODE:
$query = "SELECT Username, Email FROM User WHERE User = ?";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s",$user);
// execute query
$stmt->execute();
// get result and assign variables (prefix with db)
$stmt->bind_result($dbUser, $dbEmail);
//get number of rows
$stmt->store_result();
$numrows = $stmt->num_rows();
if ($numrows == 1){
$row = $stmt->fetch_assoc();
$dbemail = $row['Email'];
}
UPDATED CODE:
$query = "SELECT Username, Email FROM User WHERE User = ?";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s",$user);
// execute query
$stmt->execute();
// get result and assign variables (prefix with db)
$stmt->bind_result($dbUser, $dbEmail);
//get number of rows
$stmt->store_result();
$numrows = $stmt->num_rows();
if ($numrows == 1){
$row = $stmt->fetch_assoc();
$dbemail = $row['Email'];
}
The variable $stmt is of type mysqli_stmt, not mysqli_result. The mysqli_stmt class doesn't have a method "fetch_assoc()" defined for it.
You can get a mysqli_result object from your mysqli_stmt object by calling its get_result() method. For this you need the mysqlInd driver installed!
$result = $stmt->get_result();
row = $result->fetch_assoc();
If you don't have the driver installed you can fetch your results like this:
$stmt->bind_result($dbUser, $dbEmail);
while ($stmt->fetch()) {
printf("%s %s\n", $dbUser, $dbEmail);
}
So your code should become:
$query = "SELECT Username, Email FROM User WHERE User = ?";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s",$user);
// execute query
$stmt->execute();
// bind variables to result
$stmt->bind_result($dbUser, $dbEmail);
//fetch the first result row, this pumps the result values in the bound variables
if($stmt->fetch()){
echo 'result is ' . dbEmail;
}
Change,
$stmt->store_result();
to
$result = $stmt->store_result();
And
Change,
$row = $stmt->fetch_assoc();
to
$row = $result->fetch_assoc();
You have missed this step
$stmt = $mysqli->prepare("SELECT id, label FROM test WHERE id = 1");
$stmt->execute();
$res = $stmt->get_result(); // you have missed this step
$row = $res->fetch_assoc();
I realized that this code was provided as an answer somewhere on stackoverflow:
//get number of rows
$stmt->store_result();
$numrows = $stmt->num_rows();
I tried it to get the number of rows but realized that i didnt need the line $stmt->store_result();, and it didn't get me my number. I used this:
$result = $stmt->get_result();
$num_of_rows = $result->num_rows;
......
$row = $result->fetch_assoc();
$sample = $row['sample'];
It's best to use mysqlnd as Asciiom pointed out. But if you're in a weird situation where you are not allowed to install mysqlnd, it is still possible to get your data into an associative array without it. Try using the code in this answer
Mysqli - Bind results to an Array
Related
I've been doing SQL for over a year now, and have became completely stuck. For some reason, i'm not able to return any values from this table as I get the error
mysqli_fetch_array(): Argument #1 ($result) must be of type mysqli_result, mysqli_stmt given
I'm completely floored as to why this is happening, as i've used these kind of queries in the past
The code i'm using is
$user = "testuser";
$q = $conn->prepare("SELECT * FROM users WHERE username = ?");
$q->bind_param("s", $user);
$q->execute();
while($row = mysqli_fetch_array($q))
var_dump($row);
If I do var_dump($q), then I get an object object(mysqli_stmt)#3 (10) with no errors and the correct amount of fields. I'm just not able to read anything from this for some reason.
Thanks!
You need to call a get_result() before you can fetch your data
$user = "testuser";
$stmt = $conn->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $user);
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()) {
var_dump($row);
}
PS. better to use fetch_assoc() instead of fetch_array()
This question already has answers here:
mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement
(2 answers)
Closed 1 year ago.
Iam getting this error
[07-Sep-2017 11:48:47 UTC] PHP Warning: mysqli_stmt::bind_result():
Number of bind variables doesn't match number of fields in prepared
statement
$stmt = $con->prepare("SELECT * FROM table where Id =?");
$stmt->bind_param("s", $_POST['Id']);
$stmt->execute();
$result = $stmt-> bind_result($Id);
$numRows = $result->num_rows;
if($numRows > 0) {
if($row = $result->fetch_assoc())
{
$Taxname=$row['TaxName'];
$Tid=$row['Id'];
}}
You need to alter your query. Instead of * you need to instead opt for picking out the data you actually want from the database.
For example, if the table table has columns Id,TaxName then you would execute like so:
<?php
$sqlData = array();
$stmt = $con->prepare("SELECT Id,TaxName FROM table where Id =?");
$stmt->bind_param("i", $_POST['Id']); //id is an integer? this should be i instead of s
$stmt->execute();
$stmt->store_result(); //store the result, you missed this
$stmt-> bind_result($Id,$TaxName); //bind_result grabs the results and stores in a variable
$numRows = $stmt->num_rows; //see the correction I made here
if($numRows >0){
while ($stmt->fetch()) { //propper way of looping thru the result set
$sqlData [] = array($Id,$TaxName);
//assoc array would look like:
//$sqlData [] = array("Id" =>$Id,"TaxName" => $TaxName);
}
}
$stmt->close(); //close the connection
?>
Then you would have an array of results that you can use after you've finished with mysqli queries.
Hope this helps you understand it a bit more.
$stmt = $con->prepare("SELECT Id,TaxName FROM table where Id =?");
$stmt->bind_param("s", $_POST['Id']);
$stmt->execute();
$result = $stmt-> bind_result($Id,$TaxName);
$stmt->store_result();
$numRows = $stmt->num_rows;
if($numRows > 0) {
while( $result->fetch_assoc())
{
$newid = $Id;
$newtaxname= $TaxName;
}
print_r($newid)."<br>";
print_r($newtaxname);
}
This code will give you the answer without any warnings.
Reference : http://php.net/manual/en/mysqli-stmt.bind-result.php
mysqli_stmt::bind_result — Binds variables to a prepared statement for
result storage
I keep getting the same error message...
Fatal error: Call to a member function fetch() on a non-object
I've tried:
- removing quotes from ':user' and ':pass'
- changing fetch() to fetchAll()
- using PDO::FETCH_ASSOC
I can't seem to find a question that solves this, they all are solid SQL statements, there's no variables inside them.
$q = $dbh->prepare("SELECT * FROM users WHERE username= ':user' AND password= ':pass' ");
$q -> bindParam(':user', $username);
$q -> bindParam(':pass', $password);
$result = $q -> execute();
$numrows = count($result);
echo $numrows;
if($numrows == 1){
while($row = $result->fetch(PDO::FETCH_OBJ)){
$row["id"] = $_SESSION["id"];
$row["username"] = $_SESSION["username"];
$row["password"] = $_SESSION["password"];
$row["email"] = $_SESSION["email"];
}
} else {
header("location: index.php?p=5");
}
Fetch should be used on the PDOstatement object.
According to the PDO manual:
PDOStatement::fetch — Fetches the next row from a result set
The fetch function is a member function of the PDOStatement object.
Example from the manual:
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute(); //no need in another variable, like: $r = $sth->
/* Exercise PDOStatement::fetch styles */
$result = $sth->fetch(PDO::FETCH_ASSOC); //$sth, not "$r"
Another note:
Regarding your usage of execute, according to the manual it returns a boolean value (true/false) and not an array of values.
I believe you've used mySQL so the "migration" to PDO is a bit strange for you, look at the manual and follow some tutorials.
Remove quote from user and pass
$q = $dbh->prepare(" SELECT * FROM users WHERE username= :user AND password= :pass");
The following code is giving me this error:
Call to undefined method mysqli_stmt::get_result() on line 21.
I don't understand how this object works and why I can do the first database call but not the second.
<?php
header('Content-Type: application/json');
include_once 'do_dbConnect.php';
include_once 'functions.php';
sec_session_start();
//identify who took the last call
$stmt = $mysqli->stmt_init();
if ($stmt->prepare("SELECT MAX(dateOfCall), id FROM call")) { //setup the query statement
$stmt->execute(); //execute the statement
$result = $stmt->get_result(); //get the results
$row = $result->fetch_assoc(); //get the first row
$user_id = $row['id']; //get the id column
}
//identify how many team members there are
if ($stmt->prepare("SELECT id FROM teamMembers")) { //setup the query statement
$stmt->execute(); //execute the statement
$result = $stmt->get_result(); //get the results
$memberCount = $result->num_rows;
}
//get next user
if ($stmt = $mysqli->prepare("SELECT * FROM teamMembers WHERE id = (? + 1) % ?")) { //setup the query statement
$stmt->bind_param('ii', $user_id, $memberCount);
$stmt->execute(); //execute the statement
$result = $stmt->get_result(); //get the results
$row = $result->fetch_assoc(); //get the first row
$next_user_id = $row['id']; //get the id column
$next_user_name = $row['username'];
}
$stmt->close();
//get the next call taker from the teamMember table
echo json_encode($row);
?>
Please read the user notes for this method:
http://php.net/manual/en/mysqli-stmt.get-result.php
It requires the mysqlnd driver... if it isn't installed on your webspace you will have to work with BIND_RESULT & FETCH!
http://www.php.net/manual/en/mysqli-stmt.bind-result.php
http://www.php.net/manual/en/mysqli-stmt.fetch.php
I tried to count return rows from a query using prepared statements.
Something like this :
$q = "SELECT name, address, contact FROM members";
$stmt = mysqli_prepare ($dbc, $q);
mysqli_stmt_store_result($stmt);
// Get the number of rows returned:
$rows = mysqli_stmt_num_rows($stmt);
echo $rows;
But always I am getting 0 rows when executing this query. I tested it using mysql client ant then I got 6 returned rows.
can anyone tell me what is the wrong with this?
Thank you.
You need to call mysqli_stmt_execute to actually get a result set, before trying to store the results.
$stmt = mysqli_prepare ($dbc, $q);
mysqli_stmt_execute($stmt); // <--------- currently missing!!!
mysqli_stmt_store_result($stmt);
$rows = mysqli_stmt_num_rows($stmt);
You have to execute the query
$q = "SELECT name, address, contact FROM members";
if ($stmt = mysqli_prepare ($dbc, $q)) {
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
// Get the number of rows returned:
$rows = mysqli_stmt_num_rows($stmt);
echo $rows;
mysqli_stmt_free_result($stmt);
mysqli_stmt_close($stmt);
}
You need to call execute() on your statement handle, right now you are only preparing the query.
$q = "SELECT name, address, contact FROM members";
$stmt = mysqli_prepare ($dbc, $q);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
// Get the number of rows returned:
$rows = mysqli_stmt_num_rows($stmt);
echo $rows;
i was concerned whether your table Members actually had records in it. Anyways try
$rows = mysqli_num_rows($stmt);
//or
$rows = mysql_num_rows($stmt);
This has worked out for me and has returned 5 as there were only 5 records in my table.