php num->行无效

时间:2015-07-28 08:51:41

标签: php mysql mysqli

我一直收到错误,即使我100%确定我遵循了 PHP manual中的示例。

代码的简化版本可以在下面找到。

注意:与数据库的连接没问题。

编辑:我一直得到一个" Catchable致命错误:类mysqli_stmt的对象无法转换为字符串"错误。

编辑:现在我一直在"任务失败"即使我确信行数应为1。

这里使用的是coode:

        @ $db = new mysqli('localhost', 'USER', 'PASSWORD', 'DATABSE');

        $email = $db->prepare("select * from members where email = ?");
        $email->bind_param('s', $email);
        $email->execute;
        $email->store_result;
        $email->num_rows;

        if ($email > 0) {
          echo "<p>This e-mail is already in use, please try again with another e-mail.</p>";
          exit;
        } else {
          echo "mission failed";
        }

        exit;

编辑:

        @ $db = new mysqli('localhost', 'USER', 'PASSWORD', 'DB');

        if ($db->connect_errno) {
          echo "<p id=\"signup_confirmed\">Error: could not connect to database. Please try again later.</p>";
          exit;
        }

        $checkRow = $db->prepare("select * from members where email = ?");
        $checkRow->bind_param('s', $email);
        $checkRow->execute;
        $checkRow->store_result;

        if ($checkRow->num_rows > 0) {
          echo "<p id=\"signup_confirmed\">This e-mail is already in use, please try again with another e-mail.</p>";
          exit;
        } else {
          echo "<p id=\"signup_confirmed\">Row checking has failed</p>";
        }

4 个答案:

答案 0 :(得分:4)

更改

  $email->num_rows();

 $email->num_rows;

代码

新编辑

$count = $email->num_rows;

        if ($count > 0) {
          echo "<p>This e-mail is already in use, please try again with another e-mail.</p>";
          exit;
        } else {
          echo "mission failed";
        }

更多编辑

将此更改为

$email = $db->prepare("select * from members where email = ?");
$email->bind_param('s', $email);

这个

 //  you are over riding your  $email value with the query thats the reason its not working 
$query= $db->prepare("select * from members where email = ?");
$query->bind_param('s', $email);

答案 1 :(得分:0)

$email是一个结果集,一个对象。

它将保存从SQL操作返回的数据。

您需要在变量中获取num_rows()

更正后的代码:

$cnt = $email->num_rows;
if ($cnt > 0) {

OR

if ($email->num_rows > 0) {

答案 2 :(得分:0)

    @ $db = new mysqli('localhost', 'USER', 'PASSWORD?', 'DATABSE');

    $query_email = "select * from members where email = ?";
    $email = $db->prepare($query_email);
    $email->bind_param('s', $email);
    $email->execute();
    $email->store_result();

    if ( $email->num_rows > 0) {
      echo "<p id=\"signup_confirmed\">This e-mail is already in use, please try again with another e-mail.</p>";
      exit;
    }

答案 3 :(得分:0)

而不是     if ($email > 0)使用if ($email->num_rows() > 0)