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
Related
This question already has answers here:
SELECT COUNT(*) AS count - How to use this count
(5 answers)
Closed 1 year ago.
I'm trying to get the sum of a column from my database. When I try this code I get the error Undefined index: sum(col_1). How can I do this? I'm a real beginner so keep it a bit simple.
$sql = "SELECT * FROM table_1";
$run = mysqli_query($conn, $sql);
while($rows = mysqli_fetch_assoc($run)){
$col_ans[1] = $rows['sum(col_1)'];
echo $col_ans[1];
}
You can easily find sum of a column this way.
$sql = "SELECT sum(col_1) as col_1_sum FROM table_1";
$run = mysqli_query($conn, $sql);
while($rows = mysqli_fetch_assoc($run)){
echo $rows['col_1_sum'];
}
Your problem is that you didn't use the SUM() function inside your query, so you won't get any sum from the query
You can also try the following:
$query = "SELECT productName, SUM(productQty) AS 'Products Ordered'
FROM ctrlaproducttab
GROUP BY productName
";
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_assoc($result))
{
$productName = $row['productName'];
$itemsOrdered = $row['Products Ordered'];
}
Notice that we're giving an alias name Products Ordered to the result of expression SUM(productQty), and now we can use this alias name as an index in the associative array returned by mysqli_fetch_assoc()
This question already has answers here:
Get sum of MySQL column in PHP
(9 answers)
Closed 1 year ago.
I'm trying to get the sum off all the prize money from a column in a MySql table, but I'm not getting a result.
$result = mysqli_query("SELECT SUM(prize_money) FROM cards");
while ($rows = mysqli_fetch_array($result)) {
echo $rows['SUM(prize_money)'];
}
I just want to add all of the numbers in the prize_money column then echo the results.
Thank You
You should apply an alias to the SUM so it is easier to access in the PHP.
You then need to pass the connection string to the mysqli_query function as the first parameter.
So for example if your database connection were:
$con=mysqli_connect("localhost","my_user","my_password","my_db");
then you'd use this code to execute the query and assign the alias:
$result = mysqli_query($con, 'SELECT SUM(prize_money) AS sum_prize_money FROM cards');
$row = mysqli_fetch_assoc($result);
$sum = $row['sum_prize_money'];
echo $sum;
Do you get a result if you do this:
$result = mysqli_query("SELECT SUM(prize_money) FROM cards");
$rows = mysqli_fetch_array($result);
echo $rows
As good practice, you should learn to aliase your sql variables e.g. SUM(prize_money) AS total etc
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:
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'];
}
I ran the following code in PHPAdmin's terminal:
SELECT COUNT( * )
FROM User
WHERE email = 'ijp'
and got result 0.
Then part of my PHP program goes as follows:
$email = clean_input($_POST['email']);
$query = "SELECT COUNT(*) FROM user WHERE '.$email.'=email";
echo $query;
$result = mysqli_query($link, $query);
echo $result;
mysqli_close($link);
I have checked that I logging into the database went fine. Still the output is
SELECT COUNT(*) FROM User WHERE email='ijp'
Catchable fatal error: Object of class mysqli_result could not be converted to string in .../html/register.php on line 49
How can I run MySQL-commands in PHP?
You are passing the $result resource to echo which expects a string. You need to use one of the mysqli_fetch_* functions to read the value from the $result and then print it.
mysqli_fetch() is deprecated, use mysqli_fetch_array or mysql_fetch_assoc or mysql_fetch_all, for example:
$row = mysqli_fetch_array($result);
if($row) {
echo $row[0];
}
Try using
$query = "SELECT COUNT(*) FROM user WHERE email=\"".$email."\"";
If the value or $email is 'ijp', the value of $query after
$query = "SELECT COUNT(*) FROM user WHERE '.$email.'=email";
Would be...
SELECT COUNT(*) FROM user WHERE ijp=email
Use '$email' instead of '.$email.'
all the above answers are all correct but one thing was ommited... you need to differentiate between a table called "User" and one called "user" - they are case-sensitive on *NIX systems (Linux and the like)