简体   繁体   中英

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!

更改查询:

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM