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

类的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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?