在phpmyadmin中编辑的mysql数据库不会在php脚本中返回查询

问题描述:

This is a weird problem. I have a field called status in a database table called Parents. Via a php script, I entered a bunch of parents with status of 'active'. Later, I used phpmyadmin to change two of them to a status of 'dormant'. When I run a query asking for rows with status of active, all is well in both phpmyadmin and in my php script. When I run a query asking for rows with status of dormant, phpmyadmin returns the two rows whose status is dormant, but my php script does not return any rows. Here are the relevant pieces of the code:

// get value of radio button for $active
$active = stripslashes(strip_tags($_POST['active']));
if (!$active) {
  $active = 'active';
}

// Similar process for value of $myOrderby, 
// which can have value of 'name' or 'email' and works fine

$query = "SELECT 
    Parents.parentID, 
    Parents.parentName, 
    Parents.parentEmail, 
    Students.nickName,
    Students.Lname
  FROM Parents, Students
  WHERE Parents.parentID=Students.parentID
    AND Parents.status=:active
  ORDER BY $myOrderby ASC" ;
$stmt = $db->prepare($query);
$stmt->bindValue(':active', $active, PDO::PARAM_STR);

try {
   $stmt->execute();
   $affected_rows = $stmt->rowCount();
   if ($affected_rows > 0) {
      // various actions...
   }
} catch (PDOException $ex) {
   $message = $ex->getMessage() ;
   $filename = 'admin-parents' ;
   notifyMe($message, $filename) ;
}

Curiously, however, if I again use phpmyadmin to change the two rows whose status is 'dormant' back to a status of 'active' (not using any punctuation in the input box) those two rows do NOT show up in the php results for all active parents. That is the problem that started me on this entire quest.

CLARIFICATION: myphpadmin query ALWAYS returns correct result for these two rows, both when status is 'active' and when status is 'dormant'. (I change them via myphpadmin.) However, php script NEVER returns these two rows, regardless of their status.

It turns out: The two rows in Parent table with status of 'dormant' did NOT have any rows in the Student table. The query returns only rows where a Parent also has a Student. Which is not exactly what I intended. Back to the drawing board! But I am SO relieved to understand the source of the problem! Thanks everyone! :-)

You need to set which button was selected by the user, use result as parameter in query Assuming radio buttons set up as

<Input type = 'radio' Name ='active' value= 'active'>Active
<Input type = 'radio' Name ='active' value= 'dormant'>Dormant


// get value of radio button for $active
$active = $_POST['active'];

}
ETC..............

Assign parameters

$stmt->bindValue(':active', $active, PDO::PARAM_STR);