SELECT COUNT和SELECT错误

时间:2013-10-28 14:07:41

标签: php mysql mysqli

在我的代码中,我想从不同的表中创建多个SELECT。我有一个SELECT COUNT和一个普通的SELECT。我想在一个声明中写这些。 当我这样做时,我得到以下错误:

  

mysqli_error()期望参数1为mysqli,布尔值为

这是我所作的陈述:

$check = "SELECT 
            (SELECT COUNT(bloopp) FROM bloopp WHERE email='$email') AS bloopp, 
            (SELECT first_name AS first_name, last_name AS last_name 
             FROM user WHERE email ='$email')";

3 个答案:

答案 0 :(得分:6)

问题是你的第二个select语句,你试图在第二个select中返回多个列。您应该使用类似于以下内容的内容:

SELECT 
    (SELECT COUNT(bloopp) FROM bloopp WHERE email='$email') AS bloopp, 
    first_name AS first_name, 
    last_name AS last_name 
FROM `user` 
WHERE email ='$email';

当您以编写的方式使用select语句时,它一次只能返回一列。

答案 1 :(得分:-2)

没有理由在一个声明中写这些。所以,把它们写成两个。

$sql = "SELECT COUNT(bloopp) FROM bloopp WHERE email=?"
//process it usual way
$sql = "SELECT first_name, last_name FROM user WHERE email = ?";
// process it usual way

此外,无论bloopp必须通过数字ID连接到用户,而不是电子邮件。

答案 2 :(得分:-2)

试试这个:

$check = "SELECT (SELECT COUNT(bloopp) FROM bloopp WHERE email='$email') AS bloopp ,
         first_name AS first_name, last_name AS last_name FROM user WHERE email ='$email'";