Check ALL empty comlumn in PHP & MySQL - php

Ex : I have 1 table below
ID USER1 USER2 USER3
1 X X
2 X X
3 X X
4 X X
5 X X
How i can check ALL value USER2 comlumn is empty in PHP & MySQL? I code below but it's not working
$res = mysqli_query($conn, "SELECT USER2 FROM TABLE")
if(count($res)== "0") echo "OK";

select sum(user2 <> '') as count
from your_table

This would help,
$res = mysqli_query($conn, "SELECT COUNT(*) AS count FROM TABLE where USER2 IS NULL OR USER2=''");
if($res['count']==0)
{
echo "It is Empty";
}
else
{
echo $res['count']." rows have User2 empty";
}
count(*) returns the no of rows coming as the output of the query, which will be in integer form. $res == "0" means that you are comparing with a string "0", not an integer.

$res = mysqli_query($conn, "SELECT * FROM TABLE where USER2=''")
$n_rows_void_user2 = mysql_num_rows($res);
if($n_rows_void_user2) echo "OK";
else
echo "There are $n_rows_void_user2";

First of all you have to write the correct query:
"SELECT * FROM mytable WHERE --user2 condition--"
Where user2 condition may be different depend on your data:
WHERE ISNULL(user2) OR user2=''
It depends on your data.
Then to execute this query in php:
$res = mysqli_query($conn, $query);
$n_rows = mysql_num_rows($res);
if ($n_rows > 0) {
// echo your rows here
}
Also you should connect to database before, i hope you did that.

you can do something like this:
$res = mysqli_query($conn, "SELECT * FROM TABLE where USER2 IS NULL OR USER2 = ''");
$num = $res->num_rows;
if($num != 0){
echo "number of empty row for USER2".$num;
}else{
echo "on empty column for USER2";
}

An extension to the answer by #juergen :
select sum(user2 IS NOT NULL OR some_col <> '') as count
from your_tabl;

Related

Remove Duplicate Comma-Separated Values From MySQL Column with PHP

I have a db with columns like the following:
id options
1 Website,Website,Newspaper,Newspaper,TV,TV,Radio,Radio
2 Website,Website,Newspaper,Newspaper
3 Website,Website,TV,TV
The goal is to remove the duplicate entries and normalize the options column to:
id options
1 Website,Newspaper,TV,Radio
2 Website,Newspaper
3 Website,TV
I have developed the following PHP code:
$sql = "SELECT id, options FROM table";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$id = $row['id'];
$values_array = explode( ',' , $row['options'] );
if(count($values_array) != count(array_unique($values_array)))
{
$likes = array_unique($values_array);
$new = implode(',', $likes);
$sql = "UPDATE table SET options=".$new." WHERE id = '$id'";
}
}
} else {
echo "0 results";
}
$conn->close();
This doesn't get the job done. Everything seems to work but the attempt to update the options columns with the new array data.
This doesn't seem too difficult, just looking for a little guidance on how to make it work.
Thanks in advance!
You can do it directly in mysql
UPDATE T
JOIN
(SELECT id,GROUP_CONCAT(DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(t.options, ',', sub0.aNum), ',', -1)) AS ids
FROM t
INNER JOIN
(
SELECT 1 + units.i + tens.i * 10 AS aNum, units.i + tens.i * 10 AS aSubscript
FROM (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) units
CROSS JOIN (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) tens
) sub0
ON (1 + LENGTH(t.options) - LENGTH(REPLACE(t.options, ',', ''))) >= sub0.aNum
GROUP BY id)x
ON x.id=t.id
SET t.options=x.ids
FIDDLE
Inspired by this answer
Try This:
$sql = "SELECT id, options FROM table";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
$id = $row['id'];
$values_array = explode(',', $row['options']);
if (count($values_array) != count(array_unique($values_array))) {
$likes = array_unique($values_array);
$new = implode(',', $likes);
$sql = "UPDATE table SET options=" . $new . " WHERE id = '$id'";
/* seems you missed this */
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
/* you declared sql query but not executed it */
}
}
} else {
echo "0 results";
}
$conn->close();
hope it was helpful :)
As stated in other answers, your quotes are wrong and you didn't execute the UPDATE query, there's another thing you need to know.
table is a reserved keyword in MySQL, so you can't use it like that in your query. Use backticks to escape it.
So your code should be like this:
$sql = "SELECT id, options FROM `table`";
$result = $conn->query($sql);
if ($result->num_rows > 0){
// output data of each row
while($row = $result->fetch_assoc()) {
$id = $row['id'];
$values_array = explode( ',' , $row['options'] );
if(count($values_array) != count(array_unique($values_array))){
$likes = array_unique($values_array);
$new = implode(',', $likes);
$sql = "UPDATE `table` SET options='".$new."' WHERE id = '$id'";
$conn->query($sql);
if($conn->affected_rows){
echo "success<br />";
}else{
echo "error<br />";
}
}
}
}else{
echo "0 results";
}
$conn->close();
Here's the reference:
Keywords and Reserved Words
I added the following code to my PHP as showcased by Ajjay Aroraa --
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
The final code for my entire application:
$sql = "SELECT id, options FROM tdata";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$id = $row['id'];
$values_array = explode( ',' , $row['options'] );
if(count($values_array) != count(array_unique($values_array)))
{
// find duplicate values in the array
$likes = array_unique($values_array);
$new = implode(',', $likes);
$sql = "UPDATE tdata SET options='".$new."' WHERE id = '$id'";
// execute update query
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
}
}
} else {
echo "0 results";
}
$conn->close();
Thanks to all who replied -- this is a quick fix as I work to normalize the tables.

how get and compare two mysql data?

table 1
two column >> paid - order_num1
table 2
two column >> order_num2
I want get order_num2 value from table 2 and
update paid(insert paid = 1) in table one with same order_num value
If order_num1=order_num2 then paid = 1 in table 1
$q = mysql_query("select order_num2 from table2 where samevalue = samevalue ");
$x = mysql_fetch_row($q);
mysql_query("update table1 set paid=1 where order_num1='$x['order_num2']'");
But it does not work!
First get from one table and update paid from another table if order_num have same value
Try
$table2 = mysqli_query("SELECT * FROM table2");
$row = mysqli_fetch_array($table2);
$num2 = $row['order_num2'];
$table1 = mysqli_query("SELECT * from table1");
$roww = mysqli_fetch_array($table1);
$num1 = $row['order_num1'];
if ($num2 == $num1) {
$updateDB = mysqli_query("UPDATE table1 SET paid = 1 WHERE order_num1 = '$num2'");
} else {
//Not equal so the paid column wont get updated
}

How can I check column sql Belong to the same row

My database is called pica
It has the following table called gallery
Example:
gallery==>
id pic1
__ ___
1 picture1.jpg
2 picture2.jpg
3 picture3.jpg
How can I check column sql Belong to the same row in php
Example:
I want to check that picture1.jpg Belong to id 1 and
picture2.jpg Belong to id 2
After that I get a message Verified ,
It's that possible ?
You can achieve this with a query like this:
$row = $db->getOne("SELECT count(*) as count
FROM pica WHERE id = 1
AND pic1 = 'picture1.jpg'");
if ($row['count'] > 0)
{
print 'message Verified';
}
You can use a condition for this:
IF EXISTS (SELECT * FROM pica WHERE id=1 AND pic1='picture1')
SELECT 'verified'
ELSE
SELECT 'failed'
Assuming you want to check $php_pic and $id are in same row or not
Do it this way :
Method 1
/*Fetch records*/
$sql = 'SELECT id,pic1 from gallery where id = $id';
$rs= $mysqli->query($sql); /* fetch details */
$id = $rs['id'];
$pic1 = $rs['pic1'];
/* match using php */
if ($id == $php_id and $pic1 == $php_pic)
{
/* they are in same row*/
}
Method 2
/*Fetch records*/
$sql = "SELECT id,pic1 from gallery where (id = $id and pic1=`$php_pic`)";
$rs= $mysqli->query($sql); /* fetch details */
$row_cnt = $rs->num_rows;
/* match using php */
if ($row_cnt >= 1)
{
/* they are in same row*/
}

php, check if a result is in second column of mysql result

i have a query,
$theresult = mysql_query("SELECT * FROM table WHERE column_b = ".$variable);
so this will produce a list of results, 3 columns.
what i want to do is check if any of the rows of the result have "27" in column C.
i dont need the results, just a true/false..
thank you!
Hi i think you want to check that column c of any row has value 27 or not in fetching result PHP..then
1.If you can add condition column_c = 27 then use mysql_num_rows for count number of rows in result
$result = mysql_query("select * from table where column_b = ".$variable." AND column_c = 27") or die(mysql_error());
if($result){
if(mysql_num_rows($result) > 0){
echo "true";
}else{
echo "false";
}
}
2.If you not want to change in mysql query then
$result = mysql_query("select * from table where column_b = ".$variable."") or die(mysql_error());
$exist = "false";
if($result){
while($row = mysql_fetch_array($result)){
if($row['column_c'] == 27){
$exist = "true";
}
}
}
echo $exit;
Change the query :
"SELECT * FROM table WHERE column_b = ".$variable." AND colc = 27"
SELECT COUNT(*) FROM table WHERE column_b = 'var' AND column_c = 27
That returns the number of matching rows. If there are no matching rows, you'll get 0.
Try this:
$theresult = mysql_query("SELECT * FROM table WHERE column_b = ".$variable . " AND column_c=27");
if (mysql_num_rows($theresult) == 0 ) echo "False/True";

How to pull columns with '1' in MySQL

I was wondering if there was an easy way to do this:
I have a user_id column and then 33 other columns with either a '0' or a '1' as the type. How can I easily select all of the columns where the user_id = 320 and the column's data equals 1?
The column titles are labeled 1-33. In the end I want to join it to another table where 1-33 is the id to a label column.
Does this make sense?
Thanks for any help!
Try this I get all the fields and test if it BIT and then inject it to the query
And please tell me the result
<?php
$result = mysql_query("SHOW COLUMNS FROM sometable");
if (!$result){
echo 'Could not run query: ' . mysql_error();
exit;
}
if (mysql_num_rows($result) > 0){
$str ="";
$nums = mysql_num_rows($result);
$i = 0 ;
while ($row = mysql_fetch_assoc($result)){
$i++;
if($row['Type'] = 'BIT' && $i != $nums){
$str .= " WHERE `".$row['Field']."` = 1 AND";
}
elseif($row['Type'] = 'BIT' && $i == $nums){
$str .= " WHERE `".$row['Field']."` = 1 ";
}
}
}
$query = "SELECT * FROM mytable user_id = 320 AND ".$str;
$q = mysql_query($query);
This is for the first part of you question
the second part the same operation
and this working without knowing the fields name

Categories