MySQL Query不适用于PHP

时间:2014-05-07 22:25:08

标签: php mysql sql

$result = mysql_query("
    select CashID,something,something1,something2 
    from MsUser mu, MsCash mc 
    where mu.UserID = mc.UserID and (something2 = 1 or something2 = 2)");

while(($row = mysql_fetch_array($result) !== false)){
    This doesn't works
}

CashID位于MsCash中,UserID位于MsUser和MsCash中。为什么它不适用于PHP?它只在尝试只占用一个表时有效,但当它加入时,mysql_fetch_array会给我一个错误。

$resultLog = mysql_query("
    select UserID from MsUser where username=$username and password=$password 
    ");

while(($row1 = mysql_fetch_array($resultLog) !== false)){
    //This works
}

警告:mysql_fetch_array()期望参数1为资源,给定为空...

我在空白页面试过这个

<?
include "connect.php";

$result = mysql_query("
    select CashID,something,something1,something2 
    from MsUser mu, MsCash mc 
    where mu.UserID = mc.UserID and (something2 = 1 or something2 = 2)");
if($result === false){
    echo 'Query error: '.mysql_error();
}else{
    echo "works";
}
?>

但它没有给我任何东西,回声也没有显示出来。

感谢。

1 个答案:

答案 0 :(得分:0)

SELECT CashID,something,something1,something2
FROM MsUser mu
LEFT JOIN MsCash mc ON mu.UserID = mc.UserID
WHERE something2 = 1 or something2 = 2