get count of the ids in mysql - php

I want to count all the entries against id
i have used this query
select count(*) from table1 group by `id`
the problem is my table has 3 records
id A has 2 records
id B has 1 record
when i run this query using php it gives me 2 enteries
$mysqli = new mysqli("localhost","root", "", "db");
$query = $mysqli->prepare("SELECT COUNT(*) FROM `tble1` GROUP by `ID`");
$query->execute();
$query->store_result();
$rowsaff = $query->num_rows;
echo $rowsaff;

EDIT: misunderstand the question.
You have to SELECT the count of the distinct id:
$query = $mysqli->prepare("SELECT COUNT(distinct `ID`) FROM `tble1`");

Related

How to count messages for a user with php and mysql

I'm trying to count number of messages each user got. I have a users with user's detail id, name etc. I have created another table users_msgs where I have id, msg_id, user_id. I want to display count of messages each user got. what will be the best way to do it?
The web application have more than 2000 users. so the script have to select all of them and count their messages. I think this is not the best solution.
I thinking of count rows for 1 user from users_msgs table as count and then querying the users table for user's name with his id from users_msgs table.
I have tried selecting all users without any limit:
SELECT * FROM users
then iterating over the results like so:
<?php
while ($user = mysqli_fetch_assoc($users)) {
$count = count_user_msgs($user['id']);
echo "{$user['name']} messages: $count";
}
?>
The count_user_msgs function looks like this:
<?php
$sql = "SELECT COUNT(id) as msgs_count FROM users_msgs WHERE user_id = ?";
$stmt = mysqli_stmt_init($db);
if (mysqli_stmt_prepare($stmt, $sql)) {
mysqli_stmt_bind_param($stmt, 's', $user_id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$count = mysqli_fetch_assoc($result)['msgs_count'];
return $count;
}
return false;
?>
You need to group by each user and get the count:
$sql = "SELECT user_id, name, count(user_id) as msgs_count from table group by (user_id, name)";
$result = $mysqli->query($query)
while ($user = mysqli_fetch_assoc($users)) {
$user_id = $users['user_id'];
$msgs_count= $users['msgs_count'];
$count[$user_id] = $msgs_count;
echo "{$user['name']} messages: $msgs_count";
}
FWIW, You can get it in a single query...
SELECT u.name, count(m.user_id) message_count
FROM users u
LEFT JOIN users_messages m ON u.user_id=m.user_id
GROUP BY m.user_id, u.name
You want to count the number of msg_ids per user_id, so you'll want to GROUP BY user_id and count msg_id, like this:
$sql = "SELECT COUNT(msg_id) as msgs_count FROM user_msgs WHERE user_id = ? GROUP BY user_id";
The GROUP BY might not be necessary.

How can I summarize total amount

How can I summarize total amount of users earning from a column "price" values?
$statement = "SELECT user_id, SUM(price) FROM tbl_post WHERE user_id= ".'$_SESSION['user']';
$statement = mysql_query($statement);
$user_earning= mysql_fetch_row($statement);
echo $user_earning;
you should add GROUP BY clause at select statment
$statement = "SELECT user_id, SUM(price)
FROM tbl_post
WHERE user_id= ".'$_SESSION['user']". "
GROUP BY user_id";
write above query instead of this query
$statement = "SELECT user_id, SUM(price) FROM tbl_post WHERE user_id= ".'$_SESSION['user']';
i dont know about php but by see your query you want to sum of all price of a perticular user.
Try this, you have error in your query :
$statement = "SELECT user_id, SUM(price) FROM tbl_post WHERE user_id= '".$_SESSION['user']."' ";
$statement = mysql_query($statement);
$user_earning= mysql_fetch_row($statement);
echo $user_earning[1];// echo $user_earning['price'];
Your SQL query should look like that:
select user_id,sum(price) from tbl_post where user_id=? group by user_id;

Get record by id number mysql

This is my database structure.
id userid
1 x
2 xx
3 xxx
4 xx
I want to display row no. 3 in php. Only userid by row number.
Result should be :
xxx
Try this
select userid from table where id=3
Simply use this query
SELECT userid FROM table WHERE id=3
This should work
<?php
$connection = new mysqli("localhost", "db_username", "db_password", "db_name");
$result = mysqli_query($connection, "SELECT * FROM `table_name` WHERE `id` = '3' LIMIT 1");
$record = mysqli_fetch_object($result);
echo $record->userid;
?>
look here this answer and sqlfiddle:http://sqlfiddle.com/#!2/f001f/2
mysql:
SELECT id, userid
FROM t
WHERE id=3;
php:
$result = mysqli_query($connection, "SELECT id,userid FROM t WHERE id = 3");

MySQL Update Query - By Row Number

I'd like to update a row in a MySQL table when a specific row number is reached
This is a little confusing since , the real row number of the record isn't a column in the table.
And there's no question of iterating over rows , since we're not iterating over an array as in mysql_fetch_array()
So , if I'd like to update - say the 3rd row of the table , what would the query be like?
I'm a noob at MySQL
Thanks a ton for your help ! :D
$link = mysqli_connect("localhost", "my_user", "my_password", "my_db");
$query = "SELECT MyColoumn FROM Mytable";
$result = mysqli_query($link, $query);
$row_needed = 3; //Your needed row e.g: 3rd row
for ($i=1,$i=$row_needed,$i++) {
$row = mysqli_fetch_array($result);
}
// now we are in 3rd row
$query = "UPDATE MyColumn FROM MyTable SET MyColumn = '".$MyColumnUpdate."' WHERE MyColumn = '".$row['MyColumn']."' ";
$result = mysqli_query($link, $query);
...
Try this query
UPDATE
tbl a,
(Select
#rn:=#rn+1 as rowId,
tbl.*
from
tbl
join
(select #rn:=0) tmp) b
SET
a.columnName = <VALUE>
WHERE
b.rowId = <rowNumber> AND
a.id = b.id;
NOTE The id column must be a unique one, you can use the primary key of that table...
SQLFIDDLE

How do I SELECT and COUNT rows in SQL?

How do I get the results and count the results in one query? (Im using PDO statements)
SELECT * FROM table; // gets results
SELECT COUNT(*) FROM table; // counts results
$result = mysql_query( "SELECT * FROM table");
$count = mysql_num_rows( $result);
Using PDO:
$statement = $dbh->prepare('SELECT * FROM table');
$statement->execute();
$count = $statement->rowCount();
This will put the record count at the end of each row.
SELECT *
, COUNT(1) OVER () AS RecordCount
FROM table;
SELECT *, (select count(*) FROM table) ct FROM table
you should execute only one query...
$sql = SELECT * FROM table;
$res = mysql_query($sql);
you can have total count by mysql_num_rows(); function.. like this ..
$count = mysql_num_rows($res);

Categories