Is it possible to use mysqli_fetch_object with a prepared statement
I don't believe the interface works like that.
Going by the documentation and examples (http://www.php.net/manual/en/mysqli.prepare.php) it seems that $stmt->execute() does not return a resultset, but a boolean indicating success / failure (http://www.php.net/manual/en/mysqli-stmt.execute.php). To actually get the result, you need to bind variables to the resultset (aftere the execute call) using $stmt->bind_result (http://www.php.net/manual/en/mysqli-stmt.bind-result.php).
After you did all that, you can do repeated calls to $stmt->fetch() () to fill the bound variables with the column values from the current row. I don't see any mention of $stmt->fetch_object() nor do I see how that interface could work with a variable binding scheme like described.
So this is the story for "normal" result fetching from mysqli prepared statments.
In your code, there is something that I suspect is an error, or at least I am not sure you intended to do this.
You line:
$result = $stmt->result_metadata();
assignes the resultset metadata, which is itself represented as a resultset, to the $result variable. According to the doc (http://www.php.net/manual/en/mysqli-stmt.result-metadata.php) you can only use a subset of the methods on these 'special' kinds of resultsets, and fetch_object() is not one of them (at least it is not explicitly listed).
Perhaps it is a bug that fetch_object() is not implemented for these metadata resultsets, perhaps you should file a bug at bugs.mysql.com about that.
Use method mysqli in php oop
You cannot use fetch_object
directly on the statement object. As it said here:
Is it possible to use mysqli_fetch_object with a prepared statement
and in the documentation
http://php.net/manual/en/mysqli.prepare.php
you should get the results and apply the fetch_object to that.
/* execute query */
$stmt->execute();
$result = $stmt->get_result();
/* now you can fetch the results into an object */
while ($myrow = $result->fetch_object()) {
PHP: Get mysql data from a function as an object
Afaik mysql_fetch_object
doesn't work with prepared statements. See Is it possible to use mysqli_fetch_object with a prepared statement.
Try to either use ->fetch
on your statement (but use ->bind-result
before).
Examples on php.net
Or use ->query
instead of your prepare / execute lines, which should return a result set to use with ->fetch_object
.
Examples on php.net
$mysqli-fetch_object($result) not working
Shouldn't that be $result->fetch_object()
?
http://php.net/manual/en/mysqli-result.fetch-object.php
From Example 1:
if ($result = $mysqli->query($query)) {
/* fetch object array */
while ($obj = $result->fetch_object()) {
printf ("%s (%s)\n", $obj->Name, $obj->CountryCode);
}
According to the answers on this stackoverflow page, there is not much of a difference between the two.
Error: mysqli_fetch_object() expects parameter 1 to be mysqli_result
As far as your exact error is concerned one of your query is failing, the following steps might help. Ofcourse you question looks duplicate but here are some of the things that addresses your question
Your first query should be like this, with no curly braces, ofcourse untill you have explicitly ids wrapped in curly braces in your table.
SELECT * FROM user WHERE user_id ='$userid'
Secondly you are executing multiple queries so you might wanna consider error checking if your query executes properly or not(because of syntax error columns mismatch table name mismatch many more possibilities): do error checking like this as for while($a...)
part
if ($result=mysqli_query($link, $sorgu);)
{
while($a=mysqli_fetch_object($result))
{
$ids .= $a->pub_id . ',';
}
$sorgu2 = "select count(id) as total , year from publication where id IN ($ids) GROUP BY YEAR(`year`) order by `year` ";
//... Your further code
}
else
{
echo "Something went wrong while executing query :: $sorgu";
}
Third i see your are getting pub_id
make a comma seperated list of it so that you can give it as a parameter in your last query which is a long shot, why not use sub query for you IN
clause like this:
SELECT
COUNT(id) as total, year
FROM publication
where id
IN (
SELECT pub_id FROM pub_author WHERE user_id='$userid'
)
GROUP BY `year`
order by `year`;
Related Topics
PHP Float Calculation 2 Decimal Point
How to Get the Current PHP Executable from Within a Script
Read the Longest String from an Array in PHP 5.3
Convert PHP Array to JSON Tree
Asynchronous Http Requests in PHP
Converting to Date in PHP from Yyyymmdd Format
Is $_Server['Http_Referer'] Safe
PHP Session Id Changing on Every Request
If I Use Captcha Will I Be Able to Stop the Spam Completely in My Blog
Mysqli Commands Out of Sync; You Can't Run This Command Now
A Method of Selecting Random Characters from Given String
PHP Variable Variables with Array Key
How to Get the Last Dir from a Path in a String
How to Verify the Requesting Server in PHP