This question already has answers here:
SELECT COUNT(*) AS count - How to use this count
(5 answers)
Closed 8 years ago.
Here is the full error.
Catchable fatal error: Object of class mysqli_result could not be converted to string in
.../test/submit.php on line 11.
Here are the lines of code it is referring to.
$classId = $mysqli->query("SELECT COUNT(id) FROM class");
$classId += 1;
echo $classId;
I'm not sure if the statement isn't a returning an int, because when I'm in phpMyAdmin and do the same SQL statement, I get an int returned.
Thanks.
You need to fetch the object first. Something like the following should suffice:
$result = $mysqli->query("SELECT COUNT(id) as `count` FROM class");
$row = $result->fetch_object();
$classId = $row->count;
$classId += 1;
echo $classId;
Related
This question already has answers here:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 1 year ago.
I'm having some issues with my SQL Query. I am trying to run an sql query that saves the output into a variable and then print the variable in a different section of code:
Query:
$sql = "select Count(distinct `Customer Name`) as columnNameCount from allservers";
$result = mysqli_query($DBcon, $sql);
Display variable:
<h3 align="center"><?php echo $resultarr;?></h3>
Error message:
Catchable fatal error: Object of class mysqli_result could not be converted to string
<?php
$sql = "select Count(distinct `Customer Name`) as columnNameCount from allservers";
$result = mysqli_query($DBcon, $sql);
$row = mysqli_fetch_row($result);
?>
<h3 align="center"><?php echo $row[0];?></h3>
Remember: Mysqli_query() return an Object Resourse to your variable $result! Not a String!
You can't directly use it as $result variable!
If you have multiple results, you can loop:
while ($row = $result->fetch_assoc())
echo $row['some_row'];
else you have to fetch as row and display according to index:
$row = $result->fetch_row();
echo $row[0]; // $row[index]
Read The Documentaion
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:
SELECT COUNT(*) AS count - How to use this count
(5 answers)
Closed 1 year ago.
I want to compare the result for count(*) with INT. This is my code;
$sql_check = "SELECT COUNT(*) FROM hd";
$result_check = mysqli_query($link, $sql_check) or die ("ERR");
$row = mysql_fetch_assoc($result_check);
if($row == 1)
{...}
It seem like there is no error. I tried to run it but it skip my if statement. If I tried to
echo $row
It show error
NOTICE: Array to string conversion
If I used code
$sql_check = "SELECT COUNT(*) FROM hd";
$result_check = mysqli_query($link, $sql_check) or die ("ERR");
$row = int($result_check);
if($row == 1)
{...}
It show error
NOTICE: Object of class msqli_result could not converted to int
I have tried to used the answers from previous questions that have been asked before this (same question) but it doesn't work. Can you help me? Please I really need to know where my false.
Thankyou for helping.
Some other questions (that have been asked) and I have tried it but it seem does not work for me:
1. Object of class mysqli_result could not be converted to int and all entries return true
2. mysqli_result could not be converted to int in
3. Object of class mysqli_result could not be converted to int - Can't find my Error
4. Convert SQL query object into integer
you can try this, not sure if it solves the problem!
$sql_check = "SELECT COUNT(*) FROM hd as 'res'";
$result_check = mysqli_query($link, $sql_check) or die ("ERR");
$row = mysql_fetch_assoc($result_check);
if($row['res'] == 1)
{...}
OR YOU CAN TRY THIS
$sql_check = "SELECT COUNT(*) FROM hd as 'res'";
$result_check = mysqli_query($link, $sql_check) or die ("ERR");
$row = mysql_fetch_assoc($result_check);
if((int)$row['res'] == 1)
{...}
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