Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to print all the results of a query, but for some reason I get the following error: Fatal error: Call to a member function fetch() on boolean in H:\Some-Location\ on line X
This is my code:
<?php
$query = "SELECT adID FROM given WHERE toUser = :userid";
$query_params = array( ':userid' => $_SESSION['user']['ID'] );
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
echo "Failed to run query: " . $ex->getMessage();
}
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
echo $row['adID'];
}
?>
What's wrong?
As the docs and the error show, execute returns a boolean: http://php.net/manual/en/pdostatement.execute.php
You need to call fetch() on the statement, not the return value of execute. : http://php.net/manual/en/pdostatement.fetch.php
so replace $result>fetch() with $stmt->fetch().
The execute() method on the PDOStatement does not return the results, it only signals if the query succeeded or not (only useful when not using exceptions).
PHP Documentation:
public bool PDOStatement::execute ([ array $input_parameters ] )
What you want to do is use the PDOStatement itself to get the data:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo $row['adID'];
}
Note: According to your current logic, the loop will execute even when an exception is thrown. Try either returning from the catch block or putting the loop inside the try block.
$result is only a boolean holding success/failure of execute statement.
You need to run fetch on $stmt:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo $row['adID'];
}
As per documentation here:
http://php.net/manual/en/pdostatement.fetch.php
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm following this tutorial, but my problem is that everything is written for PDO, but the my website is MySQLi.
I've been trying to just search for conversions on www.php.net (for example; $statement = $db->prepare($query); = $statement = $db->mysqli_prepare($query) (source))
This is my code that doesn't work:
<?php
$query = "
SELECT shape FROM inventory
";
$statement = $db->mysqli_prepare($query)
$statement->mysqli_execute();
$result = $statement->mysqli_fetch()
foreach($result as $row) {
?>
<!-- HTML code here -->
<?php } ?>
It's supposed to query the shape column from the database but I keep getting this error Fatal error:
Uncaught Error: Call to a member function stmt_init() on null in /homepages/7/d410968336/htdocs/Inventory/vendors/php/Filters/Filters.php:68 Stack trace: #0 {main} thrown in /homepages/7/d410968336/htdocs/Inventory/vendors/php/Filters/Filters.php on line 68
(in this case line 7 $statement = $db->mysqli_prepare($query))
Well, first in a simple select query you really don't need the prepared statement. but if you wanna do I think this would help you.
$query = "SELECT shape FROM inventory";
$statement = mysqli_prepare($db, $query);
mysqli_stmt_execute($statement);
while(mysqli_stmt_fetch($statement)){
....
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am getting an error while running this code:
Fatal error: Call to a member function bindParam() on boolean in D:\xampp\htdocs\ipack\insertstatus.php on line 9
<?php
header('Access-Control-Allow-Origin: *');
include 'dbconnection.php';
$jobno = "AFE/0001/2015";
$jobseq = 0;
//to get INTJOBNO
$intjobno = "";
$data = $dbh->query("select INTJOBNO from PRTJOBHD where JOBNO = :jobno and JOBSEQ = :jobseq");
$data->bindParam(':jobno',$jobno,PDO::PARAM_STR);
$data->bindParam(':jobseq',$jobseq,PDO::PARAM_STR);
$data->execute();
foreach($data as $row) {
$intjobno = $row['INTJOBNO'];
echo $intjobno;
}
>
Have a look at this answer: PDO's query vs execute. You cannot bind parameters to PDO query, you need to use prepare instead.
header('Access-Control-Allow-Origin: *');
include 'dbconnection.php';
$jobno = "AFE/0001/2015";
$jobseq = 0;
//to get INTJOBNO
$intjobno = "";
$data = $dbh->prepare("select INTJOBNO from PRTJOBHD where JOBNO = :jobno and JOBSEQ = :jobseq");
$data->bindParam(':jobno',$jobno,PDO::PARAM_STR);
$data->bindParam(':jobseq',$jobseq,PDO::PARAM_STR);
$data->execute();
foreach($data as $row) {
$intjobno = $row['INTJOBNO'];
echo $intjobno;
}
PDO::query() returns a PDOStatement object, or FALSE on failure.
Source
It means your query has failed for some reason.
In this case you are using the wrong function to do what you want to do.
You need to prepare your statement since you want to bind two parameters in your query.
Use $dbh->prepare() instead of $dbh->query().
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I've set up a Database class, a User class and a UserTools class to interact with a MySQL database. It seems that my UPDATE, DELETE and INSERT commands are working find but I can't figure out what's going on with my SELECT statements. I'm only using mysql right not until I get everything working as I'd like, then I'll switch over to mysqli or PDO. But anyway, here's what I'm looking at....
DB class....
public function select($table, $where) {
$sql = "SELECT * FROM $table WHERE $where";
$result = mysql_query($sql);
if (mysql_num_rows($result) == 1)
return $this->processRowSet($result, true);
return $this->processRowSet($result);
}
public function processsRowSet($rowSet, $singleRow=false) {
$resultArray = array();
while ($row = mysql_fetch_assoc($rowSet)) {
array_push($resultArray, $row);
}
if ($single_row === true)
return $resultArray[0];
return $resultArray;
}
UserTools class
public function getUser($id) {
$db = new DB();
$result = $db->select('users', 'id = $id');
return new User($result);
}
There seems to be an issue with how I'm processing the rows or something. I've followed similar setups with UPDATE,DELETE,INSERT and they seem to work fine but I don't know whats going on here.
If I call an instance of new UserTools(), then try to run the getUser() function, it's not returning anything the way it's set up. If I keep the result from being pushed through the User class constructor, it just returns a Reference Value, meaning that I'm not processing the rows properly.
I have a feeling I'm overlooking something stupid here so any help would be greatly appreciated. Thanks!
For a start,
$result = $db->select('users', 'id = $id');
Should be
$result = $db->select('users', 'id = '.$id);
As Casimir mentioned, there's a typo in public function processsRowSet
I'd echo $sql; die; to check if the query is complete.
In UserTools class: 'id = $id' wouldn't parse in $id. Instead do "id = {$id}" or similar so that it can parse $id.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have a condition, if you find the exact url in the table, will return true, but if you do not think will return false.
But when I run the sql and it returns empty, the condition is as if I had found this condition, I will give an example.
$sql = "SELECT url FROM table WHERE url = '$link'";
$prepare = $conn->prepare($sql);
if ( $prepare->execute() ) {
return true;
} else {
return false; }
I want even when it returns empty, he gives the result of else.
If execute returns empty it feels true, but is false
I want even when it returns empty, he gives the result of else.
Well, that's not what execute does. It only returns false if the query failed. A query which is able to execute but returns 0 rows is not a failure.
http://us3.php.net/manual/en/pdostatement.execute.php
You will need to count the rows your statement would select first: http://us3.php.net/manual/en/pdostatement.rowcount.php
Go to Example #2 Counting rows returned by a SELECT statement
The return value of execute() indicates whether the query succeeded or failed. A query only fails if there's an error of some kind (network problems, permissions errors). If the query simply doesn't find any matching data, that's not an error, it's normal behavior, and execute returns true.
If you want to know if any data was returned, call fetch or fetchAll on the result.
You should use bind in PDO.
It will return false if no data found or query not executed.You can try this:
$sql = "SELECT url FROM table WHERE url = :link";
$prepare = $conn->prepare($sql);
prepare->bindParam(':link', $link, PDO::PARAM_STR, 12);
if ( $prepare->execute() ) {
if($prepare->rowCount()){
echo "got data";
return true;
}
else{
echo "no data";
return false;
}
} else {
return false;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
function getById($id) {
global $mysqli;
$result = $mysqli->query("SELECT * FROM `ids` WHERE `id` = '".$mysqli->real_escape_string($id)."'") or die($mysqli->error);
$rows = array();
while($row = $result->fetch_array(MYSQLI_ASSOC)) {
$rows[] = $row;
}
return $rows;
}
How do I get the value of id field?
$data = getById(1);
echo $data[0] or echo $data['id'] doesn't work.
Your function returns an array of arrays, so to get the first ID:
$data = getById(1);
echo $data[0]['id'];
Side notes:
Globals are undesirable. Consider passing the database connection object to the function as an argument instead.
Escaping like that is a primitive way of preventing SQL Injection. The use of a Prepared Statement would be more secure and easier.
or die(....) is bad practice because it exposes the detailed MySQL error to users, and it's hard to remove if you have written that hundreds of times. An alternative is trigger_error() which silently writes to your error log.
You no need to use while and array. Get single row and you can get without using array:
function getById($id) {
global $mysqli;
$result = $mysqli->query("SELECT * FROM `ids` WHERE `id` = '".$mysqli->real_escape_string($id)."'") or die($mysqli->error);
return $result->fetch_array(MYSQLI_ASSOC);
}
$data = getById(1);
echo $data['id'];