This question already has answers here:
MySQL returns only one row
(5 answers)
Closed 6 years ago.
I want to list all username from database if the id exists. That code works fine in database select * from users where id IN (1,2,3,4,5).
But in php it doesn't work. My php code is
$sql = "select * from users where id IN (1,2,3,4,5)";
$result = mysql_query($sql);
if ( mysql_num_rows($result) > 0 ) {
$row = mysql_fetch_assoc( $result );
echo $row["username"];
}
This php code only list one user name. I want to list all usernames. Sorry for my bad English. Thanks.
<?php
$sql = "select * from users where id IN (1,2,3,4,5)";
$result = mysql_query($sql);
if ( mysql_num_rows($result) > 0 ) {
while($row = mysql_fetch_assoc( $result )) {
echo $row["username"]."\n";
}
}
if you only fetch once, you will only get one row - obviously. so if you want all rows, you have to fetch as long as there are rows to fetch.
Related
This question already has answers here:
How to get count of rows in MySQL table using PHP?
(3 answers)
Closed 2 years ago.
How do I display this query result:
from MySQL onto a webpage, just like this:
?
I want to display the count only. I did run the same query through PHP but its returning '2'. The PHP code is below
<?php
//Connection for database
$conn = mysqli_connect("localhost", "root", "aaaaa", "db");
$query = "SELECT `gender`,COUNT(`gender`) FROM `reg` GROUP BY `gender`";
$result = mysqli_query($conn, $query);
if ($result)
{
// it return number of rows in the table.
$row = mysqli_num_rows($result);
if ($row)
{
printf("" . $row);
}
// close the result.
mysqli_free_result($result);
}
// Connection close
mysqli_close($conn);
?>
Please note that I have gone through other answers like this one but I can't really find my way through, I want the value count only not the total number of all rows in the table, .e.g. count for 'Male'.
You're using mysqli_num_rows(), which returns the number of rows in the result, not the actual data in the result. For that you need to use mysqli_fetch_assoc().
Your could would become:
$query = "SELECT `gender`,
COUNT(`gender`) AS `count`
FROM `reg`
GROUP BY `gender`";
$result = mysqli_query($conn, $query);
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$gender = $row['gender'];
$count = $row['count'];
echo "$gender = $count<br>";
}
mysqli_free_result($result);
}
Note that I slightly changed your query to make the count accessible.
This question already has answers here:
Create array in PHP from mysql
(2 answers)
php update to mysql with array not updating
(3 answers)
Closed 4 years ago.
I'm trying to use the results of a MySQL select in a php script and make some updates in the same table.
For the following query, I need to look at each record and say "if order_status = 'S', set is_placement to 1 (this is a bit) and set date_updated to curdate()"
I can do that with an update, but the trick is I need to store the order_id of those with the status of 'S' in an array to use in another query.
I'm not sure how I would loop and store those in an array for that.
Here's the select:
$orderCheck = "
SELECT
order_id,
order_status
FROM order_status
";
EDIT (based on answers below): So I can make an array of order IDs like so:
$result = mysqli_query($connection,$orderCheck);
while ($row = mysqli_fetch_array($result))
{
$array = $row['order_id'}
if($row['order_status'] == 'S'){
Store ORder IDs here
}
}
But with that array and stored order IDs, I need to update the records for each ID.
How can I preform that update and store the necessary IDs as well?
The following will store the data the way you needed:
$result = mysql_query($orderCheck);
$order_ids = array();
while ($row = mysql_fetch_array($result))
{
$order_id = $row['order_id'];
if ($row['order_status'] == "S")
{
array_push($order_ids, $order_id);
}
}
$orderCheck = "SELECT
order_id,order_status FROM jfi_sales.order_status ";
$result = mysql_query($orderCheck );
$ids= array();
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
if($row['order_status'] == 'S'){
$ids[]= $row['order_id'];
}
}
print_r($ids);
Now $ids is your array of order id whose order status is 'S'.Try this it will work.
This question already has answers here:
MySQL skipping first row
(2 answers)
Closed 5 years ago.
Even though it works correctly when I query it in phpMyAdmin SQL tab. What I am trying to accomplish is to display all images uploaded by the user ( which are currently 4 ) but I only get 3 ( the one missing is always the first one ). Here's my code:
<?php
$currentUser = $_SESSION['id'];
$sql = "SELECT username FROM user WHERE id='$currentUser'";
$result = mysqli_query($conn, $sql);
$getResult = mysqli_fetch_assoc($result);
$author = $getResult['username'];
$sql2 = "SELECT * FROM image WHERE author='$author' ORDER BY id DESC";
$result2 = mysqli_query($conn, $sql2);
$getResult2 = mysqli_fetch_assoc($result2);
while ($row = $result2->fetch_assoc()){
echo '<img class="profilePageImages" src="uploads/'.$row['path'].'" alt="Random image" />';
}
?>
This line, before the loop is consuming the first row. Just delete it.
$getResult2 = mysqli_fetch_assoc($result2);
This question already has answers here:
Single result from database using mysqli
(6 answers)
Closed 4 years ago.
I need to get only the id of member who's username is X from the mysql database.
Can this only be done with a while loop or is there any other way around this?
What I'm thinking of is something like:
$id = mysqli_query($con,'SELECT id FROM membrs WHERE username = '$username' LIMIT 1)
Thanks,
You can use:
mysqli_fetch_array();
// For Instance
$id_get = mysqli_query($con, "SELECT id FROM membrs WHERE username='$username' LIMIT 1");
$id = mysqli_fetch_array($id_get);
echo $id['id']; // This will echo the ID of that user
// What I use is the following for my site:
$user_get = mysqli_query($con, "SELECT * FROM members WHERE username='$username'");
$user = mysqli_fetch_array($user);
echo $user['username']; // This will echo the Username
echo $user['fname']; // This will echo their first name (I used this for a dashboard)
without while loop we can do it by the following code, if you are selecting more than 1 record you need to loop it
$row = mysqli_fetch_array($id);
echo $row['id'];
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
MYSQL COUNT GROUP BY question
$query = "SELECT * FROM product_id_1";
$result = mysql_query($query);
while( $record = mysql_fetch_array($result) ){
$array[] = $record['username'];
$unique_array = array_unique($array);
}
foreach( $unique_array as $list ){
echo $list.'<br/>';
}
Greeting,
I'm stuck trying to figure out how to apply array_count_unique(). Reason is I need to find out the count of each individual user in the table, i.e how many times adam appears and how many times abcd appears.
UPDATE
This is what I've done according to you guys's suggestion:
$query = 'SELECT username, COUNT(*) AS username_count FROM product_id_1 GROUP BY username';
$result = mysql_query($query);
while ($record = mysql_fetch_object($result)) {
echo $record->username;
echo $record->username_count;
}
This is exactly why we have RDBMS and SQL:
SELECT COUNT(1) AS username_count, username
FROM
product_id_1
GROUP BY
username