PHP MySQL 数据库类不支持插入

发布于 2024-11-09 07:06:00 字数 2493 浏览 0 评论 0原文

我一直在使用这个类:

<?
class DbConnector {

var $theQuery;
var $link;

function DbConnector() {

    $host = 'localhost';
    $db = 'my_db';
    $user = 'root';
    $pass = 'password';

    // connect to the db
    $this->link = mysql_connect($host, $user, $pass);
    mysql_select_db($db);
    register_shutdown_function(array(&$this, 'close'));
}

function find($query) {
    $ret = mysql_query($query, $this->link);
    if (mysql_num_rows($ret) == 0)
        return array();
    $retArray = array();

    while ($row = mysql_fetch_array($ret))
        $retArray[] = $row;

    return $retArray;
}

function insert($query) {
    $ret = mysql_query($query, $this->link);

    if (mysql_affected_rows() < 1)
        return false;
    return true;
}

function query($query) {
    $this->theQuery = $query;
    return mysql_query($query, $this->link);
}

// get some results
function fetchArray($result) {

    return mysql_fetch_array($result);
}

function close() {
    mysql_close($this->link);
}

function exists($query) {
    $ret = mysql_query($query, $this->link);
    if (mysql_num_rows($ret) == 0)
        return false;
}

function last_id($query) {
    return mysql_insert_id($query);
}

}

?>

我正在使用它进行很多查询并且它工作正常,尽管我试图使用这个查询插入:

    global $db;
        $query = $db->insert("INSERT INTO  `submissions` (
         `id` ,
        `quote` ,
        `filename` ,
        `date_added` ,
        `uploaded_ip`
        )
    VALUES (
    NULL ,  '{$quote}',  '{$filename}',  NOW(),  '{$_SERVER['REMOTE_ADDR']}')
    ");

并且它给了我这个错误:

Not Found

 The requested URL /horh_new/id/<br /><b>Fatal error</b>: Call to a member function        insert() on a non-object in <b>/Applications/MAMP/htdocs/horh_new/addit.php</b> on line <b>52</b><br /> was not found on this server.

虽然当我不使用该类时上面,并使用这个代替:

$con = mysql_connect("localhost","root","password");
 if (!$con) {
   die('Could not connect: ' . mysql_error());
   }

 mysql_select_db("my_db", $con);

 mysql_query("INSERT INTO  `submissions` (
         `id` ,
        `quote` ,
        `filename` ,
        `date_added` ,
        `uploaded_ip`
        )
    VALUES (
    NULL ,  '{$quote}',  '{$filename}',  NOW(),  '{$_SERVER['REMOTE_ADDR']}')
    ");

 echo mysql_insert_id();

 mysql_close($con);

它工作正常。谁能告诉我我的班级出了什么问题吗?我真的很感激。

I've been using this class:

<?
class DbConnector {

var $theQuery;
var $link;

function DbConnector() {

    $host = 'localhost';
    $db = 'my_db';
    $user = 'root';
    $pass = 'password';

    // connect to the db
    $this->link = mysql_connect($host, $user, $pass);
    mysql_select_db($db);
    register_shutdown_function(array(&$this, 'close'));
}

function find($query) {
    $ret = mysql_query($query, $this->link);
    if (mysql_num_rows($ret) == 0)
        return array();
    $retArray = array();

    while ($row = mysql_fetch_array($ret))
        $retArray[] = $row;

    return $retArray;
}

function insert($query) {
    $ret = mysql_query($query, $this->link);

    if (mysql_affected_rows() < 1)
        return false;
    return true;
}

function query($query) {
    $this->theQuery = $query;
    return mysql_query($query, $this->link);
}

// get some results
function fetchArray($result) {

    return mysql_fetch_array($result);
}

function close() {
    mysql_close($this->link);
}

function exists($query) {
    $ret = mysql_query($query, $this->link);
    if (mysql_num_rows($ret) == 0)
        return false;
}

function last_id($query) {
    return mysql_insert_id($query);
}

}

?>

Which I'm using for a lot of queries and it's working fine, although I'm trying to insert using this query:

    global $db;
        $query = $db->insert("INSERT INTO  `submissions` (
         `id` ,
        `quote` ,
        `filename` ,
        `date_added` ,
        `uploaded_ip`
        )
    VALUES (
    NULL ,  '{$quote}',  '{$filename}',  NOW(),  '{$_SERVER['REMOTE_ADDR']}')
    ");

And it's giving me this error:

Not Found

 The requested URL /horh_new/id/<br /><b>Fatal error</b>: Call to a member function        insert() on a non-object in <b>/Applications/MAMP/htdocs/horh_new/addit.php</b> on line <b>52</b><br /> was not found on this server.

Although when I don't use that class above, and use this instead:

$con = mysql_connect("localhost","root","password");
 if (!$con) {
   die('Could not connect: ' . mysql_error());
   }

 mysql_select_db("my_db", $con);

 mysql_query("INSERT INTO  `submissions` (
         `id` ,
        `quote` ,
        `filename` ,
        `date_added` ,
        `uploaded_ip`
        )
    VALUES (
    NULL ,  '{$quote}',  '{$filename}',  NOW(),  '{$_SERVER['REMOTE_ADDR']}')
    ");

 echo mysql_insert_id();

 mysql_close($con);

It works fine. Can anyone tell me what is wrong with my class? I'd really appreciate it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦在深巷 2024-11-16 07:06:00

你应该这样做

global $db;
$db = new DbConnector();

或者更好的使用方法是

添加 DbConnector 类的静态函数,它将在整个请求中仅创建单个实例

public static function getInstance(){
   static $instance = null;
   if($instance === null){
      $instance = new DbConnector();
   }

   return $instance;    
}

并将此类用作

$db = DbConnector::getInstance();
$db->insert($query);

You should do like

global $db;
$db = new DbConnector();

Or the better way to use this is

add a static function of class DbConnector, which will create only single instance throughout a request

public static function getInstance(){
   static $instance = null;
   if($instance === null){
      $instance = new DbConnector();
   }

   return $instance;    
}

And use this class as

$db = DbConnector::getInstance();
$db->insert($query);
路弥 2024-11-16 07:06:00

您需要在使用之前实例化该对象,

$db = new DbConnetor();

现在您可以执行插入操作,

尽管我建议首先添加一个 __construct 方法来连接到数据库

class DbConnector {

  public function __construct() {
     // connect to db here

  }

you need to instantiate the object before using it

$db = new DbConnetor();

now you can do an insert

although i suggest adding a __construct method to connect to the database first

class DbConnector {

  public function __construct() {
     // connect to db here

  }
悲念泪 2024-11-16 07:06:00

您确定 $db 包含您的类的实例吗?也许只需在调用 $db->insert() 之前执行 $db 的 var_dump() 并查看它是否已设置。

Are you sure $db contains an instance of your class? Maybe just do a var_dump() of $db just before your call to $db->insert() and see if it's set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文