$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
}
Related
I have been trying to compare value in a table with another value in another table but only the else part is executing.
<?php
$sql = "SELECT * FROM user WHERE user_id=$row[user_id]";
$result = $conn -> query ($sql);
if ($result -> num_rows > 0) {
while ($row = $result ->fetch_assoc()) {
$sql1 = "SELECT * FROM jobpost WHERE jobpost_id=$_GET[id] ";
$result1 = $conn -> query ($sql1);
$row_count = mysqli_num_rows($result);
$row1_count = mysqli_num_rows($result1);
$remaining_rows = min($row_count, $row1_count);
$row = mysqli_fetch_assoc($result);
$row1 = mysqli_fetch_assoc($result1);
if($row["experience"] > $row1["experience"])
{
//some code to display something
echo "1";
}
else
{
echo "2";
}
} }
?>
Welcome to SO, it is hard to read your source code. You can use a simple query like this:
SELECT IF(u.experience > j.experience,1,0) FROM
(
(SELECT experience FROM user WHERE user_id = 1) u
JOIN
(SELECT experience FROM jobpost WHERE jobpost_id = 8) j
)
Try this query here: http://sqlfiddle.com/#!9/1742c0
Please check our datasource. Maybe the else case is correct.
So, I am wondering how I can compare the result of one query to the result of another query in a if-statement. Like this:
$team = mysql_query("SELECT teamId FROM team WHERE teamName='$teamName'");
$tplayer = mysql_query("SELECT teamId FROM player WHERE playerName='$playerName'");
if($team==$lplayer){
//Do something
}
else{
//Do something else
}
This does not work... Why?
Now, why doesnt this work:
$tleague = mysql_query("SELECT teamId from team
WHERE leagueId=(SELECT leagueId FROM league WHERE leagueName='$leagueName')");
$tplayer = mysql_query("SELECT teamId FROM player WHERE playerName='$playerName'");
$row1 = mysql_fetch_array($tleague);
$row2 = mysql_fetch_array($tplayer);
if($row1['teamId']==$row2['teamId']){}
else{}
You need to use mysql_fetch_assoc() on the query, and compare the returned values. Something like the below. What you're comparing is the two returned resource objects:
$team = mysql_query("SELECT teamId FROM team WHERE teamName='$teamName'");
$tplayer = mysql_query("SELECT teamId FROM player WHERE playerName='$playerName'");
$t = mysql_fetch_assoc($team);
$p = mysql_fetch_assoc($tplayer);
if($t['teamId'] ==$p['teamId']){
//Do something
}
else{
//Do something else
}
However, you shouldn't be using mysql_* methods, instead look at using MySQLi // Tutorial.
You are not fetching any result from queries. Do something like
$team_result = mysql_fetch_array($team);
$tplayer_result = mysql_fetch_array($tplayer);
Then use fetched result to make your if condition
if($team_result['teamId'] == $tplayer_result['teamId'])
{
//do something
}
Also please stop using mysql as it is deprecated, switch to PDO or mysqli for new projects
Update
The new query have mistake. Why don't you use a join
$tleague = mysql_query("SELECT a.`teamId` from `team` a LEFT JOIN `league` b ON a.`leagueId` = b.`leagueId` WHERE b.`leagueName` = '$leagueName'");
$tplayer = mysql_query("SELECT `teamId` FROM `player` WHERE `playerName`='$playerName'");
$row1 = mysql_fetch_array($tleague);
$row2 = mysql_fetch_array($tplayer);
if($row1['teamId']==$row2['teamId'])
{
// do something
}
else
{
// do something else
}
UPDATED AGAIN
I merged all queries in one and i encapsuled data in the query '".$playerName."' and '".$leagueName."'
$query = mysql_query("SELECT a.`teamId` from `team` a LEFT JOIN `league` b ON a.`leagueId` = b.`leagueId` LEFT JOIN `player` c ON b.`teamId` = c.`teamId` WHERE b.`leagueName` = '".$leagueName."' and c.`playerName`= '".$playerName."'");
if($row = mysql_fetch_array($query))
{
echo 'Found: ' . $row['teamId'];
}
else
{
echo 'Not Found.';
}
It does not work, because it's not the result of the query. You need to pass to a variable the mysql_result i.e.:
$result1 = mysql_result($team, 0);
$result2 = mysql_result($tplayer, 0);
if ($result == $result2) { ...
Try like this
$teamQuery = mysql_query("SELECT teamId FROM team WHERE teamName='$teamName'");
$lplayerQuery = mysql_query("SELECT teamId FROM player WHERE playerName='$playerName'");
$team = mysql_fetch_assoc($teamQuery);
$lplayer = mysql_fetch_assoc($lplayerQuery);
if($team['teamId']==$lplayer['teamId']){
//Do something
}
else{
//Do something else
}
do it like that
$team = mysql_query("SELECT teamId FROM team WHERE teamName='$teamName'");
$tplayer = mysql_query("SELECT teamId FROM player WHERE playerName='$playerName'");
$row1 = mysql_fetch_array($team);
$row2 = mysql_fetch_array($tplayer);
if($row1['teamId']==$row2['teamId']){
//Do something
}
else{
//Do something else
}
EDIT:
on your second edit your problem is in the query itself .
try this one
$tleague = mysql_query("SELECT t.teamId from team inner join league l On t.leagueId = l.leagueId
WHERE t.leagueName='".$leagueName."' ");
I need to do a SELECT * FROM table_X , the problem is table_X is the result of another query, I don't know how to do it, perhaps with two loop, something like this :
<?php
$query1 = mysql_query("SELECT * FROM table_ref");
while ($row = mysql_fetch_array($query1))
{
$name = $row['table_name'];
$query2 = mysql_query(" SELECT * FROM '$name' ");
while ($row = mysql_fetch_array($query2))
{
$time = $data['itime'];
echo $time;
}
}
?>
The tables are all similar & I can't do joint there's no keys. So what I want is to show only the results of the second query from each results of the first query !
So, what's your structure? I don't understand. You have column table_name where are listed a lot of tables? If so, just use backquotes on your $name:
$query2 = mysql_query(" SELECT * FROM `$name` ");
Apart from the obvious that has been pointed out in the comments, you're overwriting $row in the second loop.
Also, you're trying to read an array ($data) that is not defined.
The following will work much better (but still isn't ideal):
$query1 = mysql_query("SELECT `table_name` FROM `table_ref`");
while ($row = mysql_fetch_array($query1))
{
$name = $row['table_name'];
$query2 = mysql_query("SELECT `itime` FROM `$name`");
while ($data = mysql_fetch_array($query2))
{
$time = $data['itime'];
echo $time;
}
}
just change your quotes to have the query ready to be started
change
$query2 = mysql_query(" SELECT * FROM '$name' ");
to
$query2 = mysql_query(" SELECT * FROM `".$name."` ");
i would also rather sugest to check this part
while ($row = mysql_fetch_array($query2))
{
$time = $data['itime'];
echo $time;
}
you already used variable $row to fetching previus query so better to change to something else, look like $data is matching your needs because you already used but you did not declare it
while ($data = mysql_fetch_array($query2))
{
$time = $data['itime'];
echo $time;
}
Try this query:
select x.* from ( SELECT table_name FROM table_ref) as x
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";
$query = "SELECT * FROM `table`";
$results = mysql_query($query, $connection);
If 'table' has no rows. whats the easiest way to check for this.?
Jeremy Ruten's answer above is good and executes quickly; on the other hand, it only gives you the number of rows and nothing else (if you want the result data, you have to query the database again). What I use:
// only ask for the columns that interest you (SELECT * can slow down the query)
$query = "SELECT some_column, some_other_column, yet_another_column FROM `table`";
$results = mysql_query($query, $connection);
$numResults = mysql_num_rows($results);
if ($numResults > 0) {
// there are some results, retrieve them normally (e.g. with mysql_fetch_assoc())
} else {
// no data from query, react accordingly
}
You could use mysql_num_rows($results) to check if 0 rows were returned, or use this faster alternative:
$query = "SELECT COUNT(*) AS total FROM table";
$results = mysql_query($query, $connection);
$values = mysql_fetch_assoc($results);
$num_rows = $values['total'];
Alternatively you can simply check if the result of mysql_fetch_assoc is false.
$query = "SELECT * FROM `table`";
$results = mysql_query($query, $connection);
$Row = mysql_fetch_assoc($results);
if ($Row == false)
{
$Msg = 'Table is empty';
}
One thing i noticed that was missed was the fact that the query might not succeed, so you do need to check if the $results variable is set. I'll use the answer given by yjerem as an example.
$query = "SELECT COUNT(*) AS total FROM table";
$results = mysql_query($query, $connection);
if ($results) { // or use isset($results)
$values = mysql_fetch_assoc($results);
$num_rows = $values['total'];
}
If you loop through the results, you can have a counter and check that.
$x = 1;
$query = mysql_query("SELECT * FROM table");
while($row = mysql_fetch_assoc($query))
{
$x++;
}
if($x == 1)
{
//No rows
}