为什么我可以在phpmyadmin中运行查询,但不能在PHP中运行查询

时间:2015-09-16 14:00:25

标签: php mysql mysqli phpmyadmin

我写了一个这样的脚本:

<?php
$admin = new stdClass();
$admin->name = "admin";
$admin->passwd = hash("sha256", "*********");
$admin->email = "you@yourdomain.com";

$script = "
    CREATE TABLE IF NOT EXISTS `users` 
    ( 
        `id` INT(11) PRIMARY KEY AUTO_INCREMENT NOT NULL, 
        `username` VARCHAR(255) UNIQUE NOT NULL, 
        `passwd` VARCHAR(255) NOT NULL, 
        `email` VARCHAR(255) UNIQUE NOT NULL, 
        `user_level` VARCHAR(255) NOT NULL 
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1; 

    INSERT IGNORE INTO `users` (`username`, `passwd`, `email`, `user_level`) 
    VALUES ('{$admin->name}', '{$admin->passwd}', '{$admin->email}', 'admin');
    ";
print_r( $script );
print ("<br /><br /><br />");
$connect->multi_query($script) ? print "tr" : print "fa";
?>

当我在phpmyadmin中运行它时,执行完美,但是当从我的浏览器运行时,它根本不起作用,这里可能有什么问题?

查询

    CREATE TABLE IF NOT EXISTS `users` 
    ( 
        `id` INT(11) PRIMARY KEY AUTO_INCREMENT NOT NULL, 
        `username` VARCHAR(255) UNIQUE NOT NULL, 
        `passwd` VARCHAR(255) NOT NULL, 
        `email` VARCHAR(255) UNIQUE NOT NULL, 
        `user_level` VARCHAR(255) NOT NULL 
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1; 

    INSERT IGNORE INTO `users` (`username`, `passwd`, `email`, `user_level`) 
    VALUES ('admin', 'password from hash', 'you@yourdomain.com', 'admin');

$connect声明

$connect = new connect("localhost", "********", "********", "********");

connect类声明

<?php

class connect extends mysqli
{
    public function __construct($host = null, $username = null, $passwd = null, $dbname = null)
    {
        $host       = $host     == null ? ini_get("mysqli.default_host")    : $host;
        $username   = $username == null ? ini_get("mysqli.default_user")    : $username;
        $passwd     = $passwd   == null ? ini_get("mysqli.default_pw")      : $passwd;
        $dbname     = $dbname   == null ? ""                                : $dbname;
        return $this->real_connect($host, $username, $passwd, $dbname);
    }
}

?>

1 个答案:

答案 0 :(得分:0)

您需要在调用mysqli::real_connect

之前初始化MySQLi对象

因此,将parent::init();添加到connect类构造函数中应该修复它。