简体   繁体   English

检查选定行中列的所有值

[英]Checking all the Values of the column from selected row

I would like to find out if all the column of the row selected has the equal value. 我想找出所选行的所有列是否具有相等的值。 And if all have the same values I would like to perform some action. 如果所有值都相同,我想执行一些操作。

id (tableA)   id2 (reference tableB) status
-------------------------------------------
     1               2               On 
     2               2               Off
     3               3               On
     4               3               On

if id2=3 have the same value of status { // perform action here }. 如果id2 = 3具有相同的状态值{//在此处执行操作}。 How can I check the values of id=3 then perform some action here? 如何检查id = 3的值,然后在此处执行一些操作? I'm using mysql and php. 我正在使用mysql和php。

I don't know if I'm doing it right: 我不知道我是否做对了:

$change = mysql_query("SELECT status from  tableA where status =   'On' and id=3 ");
if ($change == 'On')// if all the columns

{ mysql_query("  UPDATE tableB 
         SET status2 = 'On Going' where id2 =3")or die(mysql_error());
}

Result should be updated in tableB: 结果应在表B中更新:

id    status2
---------------
3     On Going
$change = mysql_query("SELECT `stat` from  `order_details` 
          where `stat` =  'Cancelled'");
$res=mysql_fetch_array($change);

remember it will fetch only last value from database if you want all the records you must use while loop 请记住,如果您需要必须在while循环中使用的所有记录,它将仅从数据库中获取最后一个值

    if ($res == "Cancelled")
{


         mysql_query("UPDATE `order1` 
                 SET `status` = 'Cancelled' where `order_id` ='".$order_id."'")or die(mysql_error());
    }

try this... 尝试这个...

  • You might want to use mysql_num_rows() function of PHP to achieve your goal. 您可能要使用PHP的mysql_num_rows()函数来实现您的目标。
  • Are you sure that status column name is different from the stat column name? 您确定status列名称与stat列名称不同吗? And they are not the same column? 他们不是同一列吗?
  • Use a tick ( ' ) to parametize the variable in your query. 使用对号( ' )来参数化查询中的变量。
  • At least use mysql_real_escape_string() function. 至少使用mysql_real_escape_string()函数。
  • You should practice mysqli_* prepared statement rather than the deprecated mysql_* to prevent SQL injections . 您应该练习mysqli_* prepared statement而不是过时的mysql_*以防止SQL注入

Your revised code: 您修改后的代码:

$change = mysql_query("SELECT stat FROM order_details WHERE stat = 'Cancelled'");
if (mysql_num_rows($change) == 1){ /* IF THE QUERY FOUND 1 RESULT */
  mysql_query("UPDATE order1 SET status = 'Cancelled' WHERE order_id = '$order_id'") or die(mysql_error());
}

If you'll be doing it in prepared statement: 如果要在准备好的语句中执行此操作:

if($stmt = $YourConnectionDB->prepare("SELECT stat FROM order_details WHERE stat = 'Cancelled'")){

  $stmt->execute();
  $stmt->store_result();
  $numberofrows = $stmt->store_result();

  if($numberofrows == 1){
    if($stmt2 = $YourConnectionDB->prepare("UPDATE order1 SET status = 'Cancelled' WHERE order_id = ?")){
      $stmt2->bind_param("i",$order_id);
      $stmt2->execute();
      $stmt2->close();
    } /* END OF SECOND PREPARED STATEMENT */
  } /* END OF CHECKING NUMBER OF ROWS */

  $stmt->close();
} /* END OF PREPARED STATEMENT */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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