This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How do i “echo” a “Resource id #6” from a MySql response in PHP?
Hey guys, I got an error when I try to run my code in PHP.
It displays resource id #53 in my screen. All I want is to count only the total of one of my field but I'm stuck with this error. Here's my code below:
$last_points = mysql_insert_id();
//echo $last_points , display like 12... no error
$fkid = $last_points; // no error....
$sql = "SELECT COUNT(*) FROM downline WHERE fkmember = {$fkid}";
$execute = mysql_query($sql) or die (mysql_error());
echo $execute; //display error why?
Help me guys please. I think it's my query.
First off, resource id #53 is not an error. You are displaying a resource, not the output of the query.
To show the output, use:
$last_points = mysql_insert_id();
//echo $last_points , display like 12... no error
$fkid = $last_points; // no error....
$sql = "SELECT COUNT(*) FROM downline WHERE fkmember = {$fkid}";
$execute = mysql_query($sql) or die (mysql_error());
print_r(mysql_fetch_array($execute)); //display error why?
Secondly, the mysql_* functions are deprecated. You should look into learning and utilising the mysqli or PDO libraries accordingly.
Instead of trying to echo a resultset(as received because of mysql_query) do this:
print_r( mysql_fetch_array($execute) );
$execute is an array so you need to print it among echoing it
print_r($execute);
By codeigniter way
In Model:
function getCount($fkid)
{
$Qry = "SELECT * FROM downline WHERE fkmember = $fkid};
$query = $this->db->query($Qry);
return $query->num_rows();
}
In controller:
echo $Count = $this->modelname->getCount($id);
Related
This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
Ok, hi. So i was trying to create an Android App wich uses a MySQL database to generate a RecylerView. To get the Data i use a PhP file store on the same Server. But i wanted to extend the App a bit. And now if i call this Line: $query = 'SELECT * FROM items WHERE catid = "$catid"'; the result is empty.
The weird Thing is: If i enter the Query in PhPMyAdmin it shows me the correct Results.
Here is the Complete PHP-File (Database Login removed) (Annnd the mandatory disclaimer: I'm not fluent in English, so please excuse some typos c:)
EDIT: I think some People missunderstood: The Problem is not in Android.. I'm sure. And the "generate".. just ignore that i didnt know what other Word i could use.. So the Problem is only in the PHP File
<?php
$connection = mysqli_connect("","","","");
$type = $_GET["t"];
if($type == "categorys"){
$query = "SELECT * FROM categorys";
}else{
$catid = $_GET["id"];
$query = 'SELECT * FROM items WHERE catid = "$catid"';
}
$result = mysqli_query($connection,$query);
while ($row = mysqli_fetch_assoc($result)) {
$array[] = $row;
}
header('Content-Type:Application/json');
echo json_encode($array);
?>
Write it like this, with double-quotes:
$query = "SELECT * FROM items WHERE catid = $catid";
If catid is a string, then:
$query = "SELECT * FROM items WHERE catid = '$catid'";
Anyway, you should use prepared statement.
Try this:
$query = "SELECT * FROM items WHERE catid = '$catid'";
or:
$query = "SELECT * FROM items WHERE catid = '".$catid."'";
This question already has answers here:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 1 year ago.
i have to find max value from database. for this purpose i have used max() with where clause but when i echo the result then i get this error.
Catchable fatal error: Object of class mysqli_result could not be converted to string in
i have searched alot and tried this,, this and this and some of others but found nothing helpfull...
my code is :
include('connection.php');
$qry = "SELECT MAX(week) FROM reservation WHERE status= 1" ;
$result = mysqli_query($connection,$qry2);
echo $result ;
on the same page other query is working fine but this one is not..
what i want :
basically i want to get the maximum week number where status is = 1
Hope this helps you
$result = mysqli_query("SELECT MAX(week) AS max_week reservation WHERE status= 1");
$row = mysqli_fetch_array($result);
echo $row["max_week"];
Here is corrected code:
include('connection.php');
$qry = "SELECT MAX(week) as max_week FROM reservation WHERE status= 1" ;
$result = mysqli_query($connection,$qry);
while($row = mysqli_fetch_array($result)) {
echo $row['max_week'];
}
This question already has answers here:
SELECT COUNT(*) AS count - How to use this count
(5 answers)
Closed 1 year ago.
I'm currently receiving this error message:
"Catchable fatal error: Object of class mysqli_result could not be
converted to string in ".
I'm trying to count the number of rows in a table. I know you can also use mysqli_num_rows but I thought I could just use SELECT COUNT ( * ) FROM table but it doesn't seem to be working. How can I get the Count(*) to work?
My code:
$query = "SELECT COUNT(*) FROM category";
$select_all_categories = mysqli_query($connection, $query);
echo "<div class='huge'> $select_all_categories </div>"
This won't work because mysqli_query returns an object filled with data, data that needs to be fetched by either mysqli_fetch_array or mysqli_fetch_assoc.
If you desire to get the count, you would need to do this instead:
$query = "SELECT COUNT(*) as count FROM category";
$select_all_categories = mysqli_query($connection, $query);
$data = mysqli_fetch_assoc($select_all_categories);
echo "<div class='huge'> " . $data['count']. " </div>"
$select_all_categories is an object, it is not a simple string. Therefor, you cannot use echo with it.
You will need to get the actual results before you can even get to the point of being a viable string.
In your case, for a simple count:
$query = "SELECT COUNT(*) AS 'count' FROM category";
$select_all_categories = mysqli_query($connection, $query);
$rows = $select_all_categories->fetch_assoc();
$string = $rows[0]['count'];
echo "<div class='huge'> $string </div>"
Notice that I gave the column the alias of count in the MySQL Query.
Your query (including COUNT(*)) is fine, it is not generating the error here. The error you're getting is a PHP Error - the script itself has issues. PHP is telling you that you can't echo an object. You can only echo strings, though PHP will convert most data types to strings for you (like integers for example) if you try to echo them.
$select_all_categories variable it's a object, you can read about it in documentation. You can use with that methods fetch_all, fetch_assoc, fetch_row, etc.
In your case I recommend use fetch_row, example:
<?php
$connection = new mysqli('127.0.0.1', 'root', 'www', 'ccc');
$select_all_categories = $connection->query('SELECT COUNT(*) FROM `category`');
var_dump($select_all_categories->fetch_row()); // fetch_row() will return "1" for my database
This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 8 months ago.
I don't know what the problem is with this line or how to fix it, before was okay and now I'm getting this error:
mysqli_fetch_object() expects parameter 1 to be mysqli_result
Here is my PHP code:
<?php
}
if($_GET['action']=="user_info")
{
$userid = $_GET['user_id'];
$query = "SELECT * FROM user WHERE user_id ='{$userid}'";
$result = mysqli_query($link, $query);
$user = mysqli_fetch_object($result);
$queryt = "SELECT * FROM user_title WHERE id='".$user->title."'";
$resultt = mysqli_query($link, $queryt);
$rowt = mysqli_fetch_object($resultt);
$title = $rowt->name;
$sorgu = "select * from pub_author where user_id='$userid'";
$publications = mysqli_query($link, $sorgu);
while($a = mysqli_fetch_object($publications))
{
$ids .= $a->pub_id . ',';
}
$ids = rtrim($ids,",");
$sorgu2 = "select count(id) as total , year from publication where id IN ($ids)
GROUP BY YEAR(`year`) order by `year` ";
$publications2 = mysqli_query($link, $sorgu2);
while($a2 = mysqli_fetch_object($publications2))
{
$mount = explode('-', $a2->year);
$accyaz[$mount[0]] = $a2->total;
}
}
?>
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`;
The error you are stating translates to this: The query fails somehow, instead of running the mysqli_query($link, $sorgu); line echo $sorgu, go to phpmyadmin and test your query, if it is bad, fix it in phpmyadmin until it works and set it up in the code correctly
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How do i “echo” a “Resource id #6” from a MySql response in PHP?
I have a statement:
$query1 = mysql_query("SELECT COUNT(author) FROM userpages WHERE `author` = '" . $userid . "'") or die();
echo $query1;
Ignore the amazingly awful naming convention their, its just for testing it before i beef it up. Because im not actually getting data from the table i should just be able to echo the statement shouldnt i? However its giving me an error.
The error is: Resource id #4
Any advice?
Thanks.
mysql_query returns a resultset resource, you have to loop through the resultset using
mysql_fetch_array($query1)
(or similar) to retrieve each record. It doesn't matter whether the query returns 1 row or 100,000 rows, the principle is the same
please read the relevant sections of the manual that explain this in great detail before feeling the need to ask for help.... reading the manual is your friend!
EDIT
while ($row = mysql_fetch_array($res,MYSQL_ASSOC)) {
echo $row['COUNT(author)'];
}
or change your SQL query to
$query1 = mysql_query("SELECT COUNT(author) AS authorcount FROM userpages WHERE `author` = '" . $userid . "'") or die();
and then
$row = mysql_fetch_array($res,MYSQL_ASSOC))
will return an array with an index of 'authorcount'
echo $row['authorcount'];
http://www.php.net/manual/en/function.mysql-query.php
Returned values:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning
resultset, mysql_query() returns a resource on success, or FALSE on
error.
And:
The returned result resource should be passed to mysql_fetch_array(),
and other functions for dealing with result tables, to access the
returned data.
I think, this will be helpfull
$query = "SELECT COUNT(author) as cnt FROM userpages WHERE `author` = '" . $userid . "'";
echo $query;
$res = mysql_query($query);
while ($row = mysql_fetch_array($res,MYSQL_ASSOC)) {
print_r($row);
}