Group By Not working - only displaying 1st entry - php

I have over 40'000 entries and each is assigned to a "list_name"
I am basically trying to get just the list_name value echo'd out
$groupq = mysqli_query($dbc, "SELECT * FROM `products-full` GROUP BY `list_name`");
$groupr = mysqli_fetch_assoc($groupq);
do {
echo $groupr['list_name'];
} while($groupr = mysqli_fetch_assoc($groupq));
however its only displaying 1 entry then no more ..
https://imgur.com/a/3rnXGet

Try this.
$groupq = mysqli_query($dbc, "SELECT * FROM `products-full` GROUP BY `list_name`");
while($groupr = mysqli_fetch_assoc($groupq)){
echo $groupr['list_name'];
}

$database = "sample" //replace your database name here
$conn=new mysqli("localhost","root","",$database); // here username is root and password is null , change it according to yours
if($conn->connect_error)
{
echo $conn->connect_error;
die("sorry database connection failed");
}
$sql = "SELECT * FROM products-full GROUP BY list_name";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row['list_name'];
}
}
thats it

try this
$groupq = mysqli_query($dbc, "SELECT list_name, count(*) FROM `products-full` GROUP BY
`list_name`");
while($groupr = mysqli_fetch_assoc($groupq)) {
echo $groupr['list_name'];
}

Related

How would i display a message if mysql doesn't return a result or empty

how would i get this to display a message in place of the Query if no result is found i updated the code but its just showing "N"
<?php
$hostname = "...";
$username = "";
$password = "";
$db = "";
$dbconnect=mysqli_connect($hostname,$username,$password,$db);
if ($dbconnect->connect_error) {
die("Database connection failed: " . $dbconnect->connect_error);
}
$query=mysqli_query($dbconnect,"SELECT DISTINCT companyname,client_id,feedback,status from review WHERE status=1 ORDER BY RAND() LIMIT 4");
$rows_get = mysqli_num_rows($query);
if ($rows_get >0)
{
$query2=mysqli_query($dbconnect,"SELECT DISTINCT companyname,client_id,feedback,status from review WHERE status=1 ORDER BY RAND() LIMIT 4");
$row1 = mysqli_fetch_assoc($query2);
$row2 = mysqli_fetch_assoc($query2);
$row3 = mysqli_fetch_assoc($query2);
$row4 = mysqli_fetch_assoc($query2);
$row5 = mysqli_fetch_assoc($query2);
}else {
$row1 = "N0 Data";
$row2 = "N0 Data";
$row3 = "N0 Data";
$row4 = "N0 Data";
$row5 = "N0 Data";
}
?>
Do as follows:
After $query insert this:
$rows_get = mysqli_num_rows($query);
if ($rows_get >0)
{
//do all database operation
}else {
echo " No data found";
}
Hope this helps.
Amend your code for example..
if ($row_get>0){
//i assume you are getting multiple rows
while ($data =mysqli_fetch_assoc ($query))
{
//run this loop and you will get all you rows.
}
}

Comparison for in SQL row selection not working?

This is the code that is not working:
$query = "SELECT * FROM $table WHERE text_id > '$last_id'"; //SELECT NEW MESSAGES
$result = mysqli_query($connection,$query);
if ($result && mysqli_num_rows($result) > 0)
{
//THIS SHOULD NOT BE RUNNING
}
I've verified over and over in phpMyAdmin and the text_id in the table and $last_id are both the integer value '1'. That being said, the condition equates to true every time the code runs.
Am I messing this code up, or is my thinking improper?
Here is entire script:
<?php
session_start();
$alias = $_SESSION['username'];
$host = 'localhost';
$user = '*';
$pass = '*';
$database = 'vethergen_db_accounts';
$table = 'table_messages';
$last_id_table = 'table_chat_sync';
$connection = mysqli_connect($host, $user, $pass) or die ("Unable to connect!");
mysqli_select_db($connection,$database) or die ("Unable to select database!");
$last_id_query = "SELECT alias FROM $last_id_table WHERE alias = '$alias'";
$last_id_result = mysqli_query($connection,$last_id_query);
$last_id_rows = mysqli_fetch_array($last_id_result);
if ($last_id_rows['alias'] === $alias)
{
$last_id = $last_id_rows['last_id'];
$query = "SELECT * FROM $table WHERE text_id > '$last_id'"; //SELECT NEW MESSAGES
$result = mysqli_query($connection,$query);
if ($result && mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
if ($row['alias'] === "Vether")
{
echo '<p id = "chat_text">'.'<b>'.$row['alias'].'</b>'.': '.$row['text']."</p>";
echo '<p id = "time_stamp">'.$row['time'].'</p>';
echo '<p id = "chat_number">'.$row['text_id'].'</p>';
}
else
{
echo '<p id = "chat_text">'.'<b class = "bold_green">'.$row['alias'].'</b>'.': '.$row['text']."</p>";
echo '<p id = "time_stamp">'.$row['time'].'</p>';
echo '<p id = "chat_number">'.$row['text_id'].'</p>';
}
echo '<hr class = "chat_line"></hr>';
$last_row_id = $row['text_id'];
}
}
//UPDATE LAST SYNC ID
$update_query = "UPDATE $last_id_table SET last_id = '$last_row_id' WHERE alias = '$alias'";
mysqli_query($connection,$update_query);
}
else
{
$update_query = "INSERT INTO $last_id_table (alias, last_id) VALUES('$alias','-1')";
mysqli_query($connection,$update_query);
}
?>
You should change ;
WHERE text_id > '$last_id'
to
WHERE text_id > $last_id
text_id column is integer and can't be compared like string.

PHP select one data from mysql NOT DISPLAYING

I am trying to display or return the Date value from a selected user
function mysql_select_one_data($database, $table, $data, $country, $phone){
$conn = mysql_connect("localhost", "root", "test123");
mysql_select_db($database, $conn) or die (mysql_error());
$query = "SELECT DATE FROM Temp_Users WHERE Country = '+94' AND Phone = '1234539543';";
$result = mysql_query($query) or die (mysql_error());
if (mysql_num_rows($result) > 0) {
// output data of each row
while($row = mysql_fetch_assoc($result)) {
echo "Date: " . $row["Date"]. "<br>";
}
} else {
echo "0 results";
}
return $result;
}
result:
Date:
Resource id #5
but when I do the query in phpmyadmin it returns
I am new in Php and I Don't know what I am doing wrong, Thanks for your help.
change this
$query = "SELECT DATE FROM Temp_Users WHERE Country = \"+94\" AND Phone = \"1234539543\";";
to this
$query = "SELECT DATE FROM Temp_Users WHERE Country = '+94' AND Phone = '1234539543';";
Change the echo command to echo "Date: $row['DATE']<br>"

Getting the COUNT(*) value in PHP

I'm using to following to count the number of rows in a table:
// Count rows
$sql = "SELECT COUNT(*) FROM articles";
$result = mysqli_query($con,$sql);
$max = mysqli_fetch_row($result);
echo $max;
This echoes array. I understand why but I can't find how to get the value in this case. I've tried $max[0]. I don't understand how to reference the column in the array in this case.
try this:
$sql = "SELECT COUNT(*) as counts FROM articles";
$result = mysqli_query($con,$sql);
$max = mysqli_fetch_assoc($result);
echo $max['counts'];
some docs here
EDIT:
$sql = "SELECT COUNT(*) as counts FROM articles";
$result = mysqli_query($con,$sql);
while($max = mysqli_fetch_assoc($result))
{
echo $max['counts'];
}
You should use MySQL PDO.
Try this:
try{
$conn = new PDO("mysql:host=localhost;dbname=dbname", username, password);
$conn -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
$errors = "There is no connection to the Server: localhost";
}
$qry = $conn -> prepare("SELECT COUNT(*) AS counts FROM articles");
$qry -> execute();
while($row = $qry->fetch(PDO::FETCH_ASSOC)) {
$Total = $row['counts'];
}
echo $Total;

PHP Mysql select

I'm trying to select some rows from my database, and generate specific HTML where my selection finds something and other HTML when it does not.
This is my code. The problem is that it always finds no matches:
$pos = 0;
$con = mysqli_connect('localhost', 'user', 'pass', 'database');
if (mysqli_connect_error()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM table1 where user = 'user'")
or die("Error: ".mysqli_error($con));
while ($row = mysqli_fetch_array($result)) {
$result2 = mysqli_query($con,"SELECT * FROM table2 where id = '"
.$row['id']."'") or die("Error: ".mysqli_error($con));
$dbpos = $row['pos'];
while ($row2 = mysqli_fetch_array($result2)) {
if ($dbpos == $pos) {
echo 'found<br/>';
} else {
echo 'empty<br/>';
}
$pos++;
}
}
mysqli_close($con);
I always get empty. What am I doing wrong?
Try making your query something like
Select * from table1
inner join table2
on table1.id=table2.id
Where table1.user='user'
This will do what you want in 1 query
If you get 1 result print found. If you get 0 then print empty
If you just want to determine if your query has an empty result or how many result was found, you can use mysqli_num_rows and remove your second while loop ($result2).
$noofrows=mysqli_num_rows($result2);
if($noofrows==0){
echo "Empty<br>";
}
else {
echo "There are ".$noofrows." record/s found.";
}
Try this. SQL joins can be very efficient depending on how your tables are structured.
//Query the DB for all of the rows in table2 where the table2 id column value
// is equal to the value of the id column value in table1
$query = "SELECT table2.*
FROM table1
INNER JOIN table2
ON table1.id = table2.id
WHERE table1.user = 'user'";
$result = mysqli_query($con, $query)or die(mysqli_error());
If you're expecting to return more than one row, the following might come in handy
$count = mysql_num_rows($result);
$i = 0;
while($row = mysqli_fetch_array($result)) {
$dbpos[$i] = $row['pos'];
$i++;
}
for($i=0;$i<$count;$i++) {
if($dbpos[$i] == $pos) {
echo 'found<br/>';
} else {
echo 'empty<br/>';
}
}
If not, then simply do this
while($row = mysqli_fetch_array($result)) {
$dbpos = $row['pos'];
}
if($dbpos == $pos) {
echo 'found<br/>';
} else {
echo 'empty<br/>';
}
More info about SQL joins here.

Categories