【问题标题】:PHP MySQL database class not working with insertionPHP MySQL数据库类不适用于插入
【发布时间】:2011-05-23 08:36:22
【问题描述】:

我一直在使用这个类:

<?
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);

它工作正常。谁能告诉我我的课有什么问题?我真的很感激。

【问题讨论】:

  • 从那个错误中我想说 $db 不是你所期望的。把它扔出去,你看到了什么?
  • 你的 $db 变量没有在你得到那个错误的地方被实例化。

标签: php mysql database insert


【解决方案1】:

你应该这样做

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);

【讨论】:

  • 所以$db = DbConnector::getInstance(); 基本上只是检查是否已创建实例?感谢您的快速帮助!
  • 如果尚未创建新实例,则将创建新实例,否则将返回旧实例。在这种情况下,更多的事情 __constructor 不应该公开。
【解决方案2】:

你需要在使用之前实例化对象

$db = new DbConnetor();

现在你可以插入了

虽然我建议先添加一个__construct方法来连接数据库

class DbConnector {

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

  }

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2013-07-29
      • 1970-01-01
      • 1970-01-01
      • 2017-04-29
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-04
      相关资源
      最近更新 更多