dtwye28880 2014-12-07 19:20
浏览 49
已采纳

类的PHP错误对象无法转换为字符串

I am trying to create a PHP file that connects to a mysql database and inserts data into the database. I am getting these errors:

( ! ) Catchable fatal error: Object of class foo_mysqli could not be converted to string in ( ! ) Notice: Undefined variable: host in C:\wamp\www\final_kk.php on line 21

( ! ) Catchable fatal error: Object of class foo_mysqli could not be converted to string in C:\wamp\www\final_kk.php on line 21

Line 21 is the first line inside of the try. Any help would be appreciated. Thanks!

<?php

class foo_mysqli extends mysqli {
    public function __construct($host, $user, $pass, $db) {
        parent::__construct($host, $user, $pass, $db);

        if (mysqli_connect_error()) {
            die('Connect Error (' . mysqli_connect_errno() . ') '
                    . mysqli_connect_error());
        }
    }
}

$db = new foo_mysqli('localhost', 'root', '', 'users');

echo 'Success... ' . $db->host_info . "
";

try {
            $conn = new PDO("mysql:host=$host;dbname=$db;username=$user;password=$pass", $user, $pass);


            // set the PDO error mode to exception
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);



            $sql = "INSERT INTO users (fname, lname,email,username,password,SSN) VALUES ('$fname', '$lname', '$email', '$uname', '$password', '$ssn')";


            // use exec() because no results are returned
            $conn->exec($sql);
            echo "New record created successfully";
            }

        catch(PDOException $e)
            {

            echo $sql . "<br>" . $e->getMessage();
            }

    $db->close();
    ?>

Okay....I made some changes based on the comments and my code now looks like this:

<?php

class foo_mysqli extends mysqli {
    public function __construct($host, $user, $pass, $db) {
        parent::__construct($host, $user, $pass, $db);

        if (mysqli_connect_error()) {
            die('Connect Error (' . mysqli_connect_errno() . ') '
                    . mysqli_connect_error());
        }
    }
}

$db = new foo_mysqli('localhost', 'root', '', 'users');

echo 'Success... ' . $db->host_info . "
";
settype($host, "string"); 
settype($user, "string");   
settype($pass, "string");    
try {
            $conn = new PDO("mysql:host=$host;dbname=$db, $user, $pass");


            // set the PDO error mode to exception
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);



            $sql = "INSERT INTO users (fname, lname,email,username,password,SSN) VALUES ('$fname', '$lname', '$email', '$uname', '$password', '$ssn')";


            // use exec() because no results are returned
            $conn->exec($sql);
            echo "New record created successfully";
            }

        catch(PDOException $e)
            {

            echo $sql . "<br>" . $e->getMessage();
            }

$db->close();
?>

This got rid of one of my errors however I still get

( ! ) Catchable fatal error: Object of class foo_mysqli could not be converted to string in C:\wamp\www\final_kk.php on line 33

please help...what am I doing wrong?

  • 写回答

1条回答 默认 最新

  • douzi9211 2014-12-07 19:58
    关注

    You're setting $db as an instance of a foo_mysqli() object in this line:

    $db = new foo_mysqli('localhost', 'root', '', 'users');
    

    Later in

    $conn = new PDO("mysql:host=$host;dbname=$db;username=$user;password=$pass", $user, $pass);
    

    you're using $db as a database name. It's enclosed in a double-quoted string, so PHP will attempt to interpolate it and substitute a string in the PDO connection string. Since $db is now an object, it fails.

    It's not clear why you're setting up an object that extends MySQLi when you're later using a PDO object anyway.

    Additionally, you haven't defined $host in the scope where you're setting up your PDO connection, so PHP reports that as undefined.

    Since you know what the host and database names are you could just do this:

        $conn = new PDO("mysql:host=localhost;dbname=users;", $user, $pass);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?