从PHP Mysql中的1个数据库中选择2个表

时间:2014-03-23 01:26:14

标签: php mysql

我可以使用哪些代码从1个数据库中选择2个表?

Warning: mysql_num_rows() expects exactly 1 parameter, 2 given in 
C:\xampp\htdocs\addeditdelete1\exec.php on line 19(this error is on,
$count=mysql_num_rows($result,$result1); )

PHP代码:

include("dbcon.php");

$username=$_POST['username'];

$password=$_POST['password'];


$username = stripslashes($username);

$password = stripslashes($password);

$username = mysql_real_escape_string($username);

$password = mysql_real_escape_string($password);



$sql="SELECT * FROM admininfo WHERE username='$username' and password='$password' and type='admin'";

$sql1="SELECT * FROM customerinfo WHERE username='$username' and password='$password' and type='customer'";

$result=mysql_query($sql);

$result1=mysql_query($sql1);

//error in this line of code

$count=mysql_num_rows($result,$result1);

if($count==1){

$_SESSION["username"];

$_SESSION["password"];

header("location:menu.php");
}

else
{

echo "Invalid username or password!";
}

2 个答案:

答案 0 :(得分:1)

你不能使用带有两个参数的mysql_num_rows,你必须调用两次函数。

$count = mysql_num_rows($result) + mysql_num_rows($result1);

我不知道您的数据库架构,但您很可能只使用一个SQL查询来完成此任务。

答案 1 :(得分:1)

您是否考虑过SELECT UNION,例如

$ sql =" SELECT * FROM(SELECT * FROM admininfo WHERE username =' $ username' and password =' $ password' and type =' admin& #39;)UNION(SELECT * FROM customerinfo WHERE username =' $ username' and password =' $ password' and type =' customer'))&#34 ;;

通过这种方式,您将只有一个查询结果来自两个表。如果表格列不匹配,您可以在SELECT中使用AS sintax修改它们。例如:

SELECT * FROM (
   SELECT username AS user, password AS pwd 
   FROM admininfo 
   WHERE username='$username' and password='$password' and type='admin')
   UNION 
   (SELECT username AS user, password AS pwd 
   FROM customerinfo 
   WHERE username='$username' and password='$password' and type='customer') 
)";