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

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";

Related

get required result in sql

i have a sql database having two columns
1. id and 2. parent_id
i want to select all ids where parent_id=1 [let the results saved in X array] then,
select all the ids where parent_id in X[] [let the results saved in Y array] then... do the same upto 7 levels at last i want to sum all levels and get that sum in a variable name "number"
what i tried:
$sql = "SELECT count(id) as leadersearch FROM `$database`.`$mem` where parent_id = ".$parent[id];
$result = mysqli_query($con, $sql);
$lv1 = mysqli_fetch_array($result);
then again
$sql = "SELECT count(id) as leadersearch FROM `$database`.`$mem` where parent_id in ($lv1)";
$result = mysqli_query($con, $sql);
$lv2 = mysqli_fetch_array($result);
and so on.... it may give me required result but it is too much code...
i want to the same by using a loop or something else... to shorten the code.
Your first query gets count(id) and then second query uses it to get results. It seems you have many flaws in your requirement to begin with.
However, for your question you can use sub-query like following
$sql = "SELECT count(id) as leadersearch
FROM `$database`.`$mem`
WHERE parent_id in (
SELECT id FROM `$database`.`$mem` where parent_id = ".$parent['id']."
)";
$result = mysqli_query($con, $sql);
$lv2 = mysqli_fetch_array($result);
If you have many level nesting and you want to search records like this then you can consider functions:
function getLeader($parent_id){
$results = [];
$sql = "SELECT id as leadersearch FROM `$database`.`$mem` where parent_id in (".$parent_id.")";
$result = mysqli_query($con, $sql);
foreach($row = mysqli_fetch_array($result)){
$results[] = $row['id'];
}
return $results;
}
$leaders = [];
$ids = getLeader($parent['id']);
foreach($ids as $id){
$leaders[$id] = getLeader($id);
}

Check ALL empty comlumn in PHP & MySQL

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;

mysql query select multiple rows where column value is same

i have this table
Column_1 Column_2
1 value1
2 value1
3 value2
My php query is
$query = "SELECT * FROM `table` WHERE `Column_1` = 'value1' ";
print_r($query);
This returns only the 1st row. I am looking to display row 1 and 2. When I run the SQL in phpmyadmin it returns row 1 and 2. However, the php script only returns row 1... I also did an
echo count($query);
But it returns only 1. What am i doing wrong?
$query = "SELECT * FROM `table` WHERE `Column_2` = 'value1' ";
$res = mysql_query($query);
if(mysql_num_rows($res)!=0) {
while($rowData = mysql_fetch_array($res)) {
var_dump($rowData);
}
}
Use mysql_num_rows to count number of results.
Use mysql_fetch_array or mysql_fetch_assoc to fetch data.
$query = "SELECT * FROM `table` WHERE `Column_1` = 'value1' ";
$res = mysql_query($query);
while($row = mysql_fetch_assoc())
print_r($row);
You need add fetch in cycle.
Use mysql_fetch_array() function
$query = "SELECT * FROM `table` WHERE `Column_1` = 'value1' ";
$res = mysql_query($query);
while($row = mysql_fetch_array($res))
{
echo $row['Column_1'];
echo $row['value_1'];
}

echo all and entire MySQL Query rows in php without specifically naming each column

I have
$result = "";
if(someCondition)
$result = mysql_query("SELECT * FROM table1 WHERE column = '$value' ");
else
$result = mysql_query("SELECT * FROM table2 WHERE column = '$value' ");
$result could have 0 -> infinity rows returned
Table 1 and Table 2 have different amounts of columns with different names
I want to write 1 generic loop after the above else that will just print out all of the rows. Preferably 1 per line or deliminated.
To clarify, one of the two query calls will fill the $results variable with rows.
I wont know which one fills it at run time so I want to just do a print all contents to screen. Is there a method that does this? is there a fast loop that iterates through all of the rows without explicitly saying the column names?
bored enough to answer:
$result = "";
if(someCondition){
$result = mysql_query("SELECT * FROM table1 WHERE column = '$value' ");
}else{
$result = mysql_query("SELECT * FROM table2 WHERE column = '$value' ");
}
while ($row = mysql_fetch_array($result)) ) {
foreach($row as $key => $var)
{
echo $key . ' = ' . $var . '<br />';
}
}

comparing Query results

$query1= mysql_query("select r.nid from ....");
$query2= mysql_query("select t.nid from....");
Both these queries return a nid. How to compare the 2 returned nid are equal.. I'm just a beginner.
$row1 = mysql_fetch_row($query1);
$row2 = mysql_fetch_row($query2);
if($row1[0] == $row2[0])
{
//something
}
You could do it in pure sql. Like this:
select
r.nid
from
....
WHERE EXISTS
(
select
NULL
from
....
WHERE
t.nid = r.nid
)
If you are certainly sure that the query really returns one id, you can speed up checking it by:
$query1 = mysql_query("select r.nid from ....");
$query2 = mysql_query("select t.nid from ....");
if(mysql_fetch_field($query1, 0) === mysql_fetch_field($query2, 0))
{
//do something
}

Categories