I want to show the count of users which have the status 1 (see code) within PHP MySQL.
<?php
// something like this
$result = mysqli_query($mysqli, "SELECT COUNT(*) FROM users WHERE status = '1'");
echo "$result";
?>
Try this:
$query = "SELECT COUNT(*) as countvar FROM users where status = '1'";
$result = mysqli_query($con,$query);
$row = mysqli_fetch_array($result);
$count= $row['countvar '];
Related
i'm trying to sum a column name "total". and i want to display the total sorting by id. if user A login he can see total booking in his account.
I keep get the error:
"Notice: Array to string conversion in Array."
can someone help me? I want to echo the total in input form.
this is my php code:
<?php
include ('connect.php');
$sql = "SELECT * FROM penjaga WHERE p_username = '".$_SESSION['username']."'";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_assoc($result);
$id = $row['p_id'];
$sql2 = "SELECT SUM(total) as total FROM sitter_kucing WHERE sitter_fk = '$id'";
$row2 = mysql_fetch_array($sql);
$sum = $row['total'];
?>
Try this,
$sql2 = "SELECT SUM(total) as total FROM sitter_kucing WHERE sitter_fk = '$id'";
$result2 = mysql_query($sql2) or die(mysql_error());
$row2 = mysql_fetch_array($result2) or die(mysql_error());
$sum = $row['total'];
i got it! thanks this is my code
this is the code:
<?php
include ('connect.php');
$sql8 = "SELECT * FROM penjaga WHERE p_username = '".$_SESSION['username']."'";
$result8 = mysqli_query($conn,$sql8);
$row8 = mysqli_fetch_assoc($result8);
$id = $row8['p_id'];
$sql9 = "SELECT SUM(total) as total FROM sitter_kucing WHERE sitter_fk = '$id'";
$result9 = mysqli_query($conn,$sql9);
$row9 = mysqli_fetch_array($result9);
$sum = $row9['total'];
?>
I am trying to display my first 10 users and their information by uid. When I use the code below, it only returns a single uid:
include "db_conx.php";
$sql = ('SELECT uid,username,country FROM users ORDER BY uid DESC LIMIT 10');
$result = mysqli_query($db_conx, $sql);
echo $result->fetch_object()->uid;
What function am I supposed to use to display rows correctly?
include "db_conx.php";
$sql = ('SELECT uid,username,country FROM users ORDER BY uid DESC LIMIT 10');
$result = mysqli_query($db_conx, $sql);
while($var = $result->fetch_object()->uid){
echo $var;
}
Should do the trick
If you get a list of object you should use a loop for show them
include "db_conx.php";
$sql = ('SELECT uid,username,country FROM users ORDER BY uid DESC LIMIT 10');
$result = mysqli_query($db_conx, $sql);
while($row = mysqli_fetch_object($result)) {
$row->uid;
}
I have a MySql table "MyTable" with the column "order"
order
-----
3
4
2
1
I want to get the highest number. The sql statement works well inside MySql:
SELECT MAX(order) FROM MyTable"
But I do not know how to use it with php and echo it? Something like:
$result = mysqli_query($con, "SELECT MAX(order) FROM MyTable");
$result = mysqli_query($con, "SELECT MAX(order) FROM MyTable");
$row = mysqli_fetch_array($result);
echo $row[0];
If you don't provide an alias to a MySQL function it will be shown as you've written it:
$row = mysqli_fetch_array($result);
echo $row['MAX(order)'];
What you can do is write something like:
$result = mysqli_query($con, "SELECT MAX(order) as 'max' FROM MyTable");
$row = mysqli_fetch_array($result);
echo $row['max'];
Which is using an alias.
Hope this helps!
Alternatively you can use ORDER BY DESC and Limit result to 1 this way you can easily get the maximum of an column .
$result=$con->query("SELECT order FROM MyTable ORDER BY order DESC LIMIT 1");
$row=mysqli_fetch_array($result);
echo $row['order'];
Or using MAX
$result = mysqli_query($con, "SELECT MAX(order) FROM MyTable");
$row=mysqli_fetch_array($result);
echo $row[0];
This tutorial is directly from tizag, using the MySql Max function
MySQL Aggregate Functions - MAX()
// Make a MySQL Connection
$query = "SELECT type, MAX(price) FROM products GROUP BY type";
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)){
echo "The most expensive ". $row['type']. " is $" .$row['MAX(price)'];
echo "<br />";
}
Here's my answer:
$host = "localhost";
$username = "your_username";
$password = "your_password";
$db_name = "your_db_name";
$connection = mysql_connect($host, $username, $password) or die ("Error:: [1]");
mysql_select_db($db_name, $connection) or die ("Error:: [2]");
$query = "SELECT `order` FROM `MyTable` order by `order` desc";
$res = mysql_query($query, $connection);
$row = mysql_fetch_array($res);
print $row[0];
With this query , you always have the highest value of the give column.
I have a database of order status' and I'd like to return the amount of rows for which the value of that row is anything but Complete.
How would I go about this? I have the following which shows how much rows has 'New' as the status for the order. But I'd like to show all rows except ones with the od_status of Complete.
<?php
$query = "SELECT od_status FROM tbl_order WHERE od_status = 'New'";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
echo "$num_rows";
?>
<?php
$query = "SELECT od_status FROM tbl_order WHERE od_status != 'Complete'";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
echo "$num_rows";
?>
$query = "SELECT od_status FROM tbl_order WHERE od_status <> 'Complete'";
$query = "SELECT COUNT(*) FROM tbl_order WHERE od_status != 'Complete'";
$result = mysql_query($query);
var_dump($result);
COUNT to count rows and != to select where is not equal to Complete.
I am trying to store a mysql value into a php variable. I have the following query which I know works. However, I the value for $count is always 0. Can someone explain what I need to do to get the count value? The count should be the count of x's w here name_x=.$id.
$query = "SELECT COUNT(name_x) FROM Status where name_x=.$id.";
$result = mysql_query($query);
$count = $result;
Is first letter in table name is really capital. Please check it first.
or Try :
$query = "SELECT COUNT(*) as totalno FROM Status where name_x=".$id;
$result = mysql_query($query);
while($data=mysql_fetch_array($result)){
$count = $data['totalno'];
}
echo $count;
$query = "SELECT COUNT(*) FROM `Status` where `name_x`= $id";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
$count = $row[0];
please try it
$query = "SELECT COUNT(*) FROM Status where name_x=$id";
$result = mysql_query($query);
$count = mysql_result($result, 0);
You are missing single quotes around $id. Should be
name_x = '" . $id . "'";