0

使用这段代码,如果我从查询中删除内部联接,一切都会正常工作。另外,我尝试了来自 phpMyAdmin 的查询,它工作得很好。

这是我的代码:

           $db = mysql_connect("localhost", USERNAME,PASSWORD);
     if (!$db)   {
        echo("<p>Error creating Database Connection</p>");
        exit;
      }

        $db_selected = mysql_select_db(DATABASE_NAMEPH,$db);

$sql = "SELECT * FROM user " +
"INNER JOIN gender ON user.id = gender.user_id "+
"INNER JOIN user_address ON user.id = user_address.user_id "+
"INNER JOIN address ON address.id = user_address.address_id;";


$result = mysql_query($sql,$db);
if(!$result){
 echo( "<p>Unable to query database at this time.</p>" );

}
$num_results = mysql_num_rows($result);
mysql_close($db);

while( $row = mysql_fetch_assoc($result)){

echo"   <form name='register' class='register'> ";
echo"   <fieldset class='row1'>";
echo"   <legend>".$row["fName"].", ".$row["lName"]."'s Personal Details</legend>          </fieldset>";
echo"   <p><label>Email:</label><data> ".$row["email"]."</data> </p>";
echo"   <p><label>First Name:</label><data> ".$row["fName"]."</data></p>";
echo"   <p><label>Last Name:</label><data> ".$row["lName"]."</data></p>";    
echo"   <p><label>Phone:</label><data> ".$row["phone"]."</data></p>";
echo"   <p><label>Street:</label><data> ".$row["street"]."</data></p>";
echo"   <p><label>City:</label><data> ".$row["city"]."</data></p>";
echo"   <p><label>State: </label><data>".$row["state"]."</data></p>";
echo"   <p><label>Zip Code:</label><data> ".$row["zip"]."</data></p>";
echo"   <p><label>Gender:</label><data> ".$row["name"]."</data></p>";
echo"   <p><label>Birthdate:</label><data> ".$row["bDate"]."</data></p>";
echo"   <p><label>Additional Info:</label><data> ".$row["info"]."</data></p>";
echo"   <p><label>Admin:</label><data> ".$row["flag"]."</data></p></fieldset></form>";


   }

?>
<h3 id="path">Number of users registered: <?php echo $num_results; ?></h3>

这是我收到的错误消息:

Unable to query database at this time.

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in
/Users/mjramahi/Sites/web/admin/users.php on line 129

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in 
/Users/mjramahi/Sites/web/admin/users.php on line 132
4

1 回答 1

1

使用点来连接字符串,而不是加号。PHP 将您的变量视为整数并且 $sql 变为等于 0(检查它!)。试试这个:

$sql = "SELECT * FROM user " .
"INNER JOIN gender ON user.id = gender.user_id ".
"INNER JOIN user_address ON user.id = user_address.user_id ".
"INNER JOIN address ON address.id = user_address.address_id;";
于 2013-07-12T17:29:45.630 回答