【问题标题】:atchable fatal error: Object of class mysqli could not be converted to string ...可匹配的致命错误:无法将类 mysqli 的对象转换为字符串...
【发布时间】:2013-09-02 17:11:50
【问题描述】:

我已经设置了一个函数,它使用插入语句将用户注册到站点中,然后另一个语句检查电子邮件是否已经存在于数据库中。

问题在于尝试将用户数据放入数据库的插入语句...浏览器输出 可捕获的致命错误:无法将类 mysqli 的对象转换为字符串...

我真的不明白是什么问题。Result1 是出现错误的查询。 从语法的角度来看,我没有做错什么。 我已经尝试了很多,但我找不到问题。通常此类错误出现在 SELECT 语句中。 代码如下:

function register_enduser($post,$connection)

{ global $errorclass;

 $name = $connection->real_escape_string($post['name']);
 $lastname = $connection->real_escape_string($post['lastname']);
 $email = $connection->real_escape_string($post['e-mail']);
 $pass_hashed = password::hash($_POST['password']); 
 $passwd =  $pass_hashed;
 $usertype= $_POST['usertype'];

 $connection->set_charset("utf8");

 $result = $connection->query("select * from users where email='".$email."'");
 if (!$result) {
     throw new Exception('error');
     return false;
 }
 elseif ($result->num_rows > 0) {
     $errorclass['existentemail'] = ' e-mail allready exists.';
     return $errorclass['existentemail'];
 } else {
     $result1 = $connection->query("insert into users values

 (NULL,'".$name."','".$lastname."','"
 .$email."','".$passwd."','".$pass_hashed."','".$usertype."')")or die($connection);
 }
 if (!$result1) {
     throw new Exception('error.');
     return false;
 } 

 return true;
 }

【问题讨论】:

  • 我可以问你一个问题吗?
  • 问题是什么?

标签: php mysqli


【解决方案1】:

检查or die($connection);。然后查看exit 的文档(dieexit 的别名)。它接收一个字符串(或 int)参数,然后向它发送一个对象。

因此,它失败了。

【讨论】:

    【解决方案2】:

    您的查询:

    $result1 = $connection->query("insert into users values
        (NULL,'".$name."','".$lastname."','"
        .$email."','".$passwd."','".$pass_hashed."','".$usertype."')")
    

    显然失败,die($connection) 部分语句被执行,导致上述可捕获错误,以下是解决方法:

    $sql = "INSERT INTO users VALUE
        (NULL, '".$name."', '".$lastname."', '".$email."', '".$passwd."', '".$pass_hashed."', '".$usertype."')";
    $result1 = $connection->query($sql) or die($connection->error); // notice "->error"
    

    收到 SQL 错误消息后,您将能够继续调试查询。

    此外,如果您在与$connection->query(...) 相同的语句中使用or die(...),则此代码毫无意义:

    if (!$result1) {
        throw new Exception('error.');
        return false;
    } 
    

    这部分代码永远不会执行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-08
      • 2013-08-07
      • 2012-08-02
      • 2017-05-03
      • 1970-01-01
      相关资源
      最近更新 更多