doutuo7609 2011-02-22 01:02
浏览 58
已采纳

在PHP中使用MySQL类的简单范围问题

This should be a no brainer, its late and i cant see what i am doing wrong:

MySQL class:

class MySQL
{
public $db;
private $result;

public function __construct ($host, $user, $password, $database)
{
  $this -> connectDB ($host, $user, $password, $database);
}

public function __destruct ()
{
  $this -> breakDB();
}

private function connectDB ($host, $user, $password, $database)
{
  $this -> db = mysql_connect ($host, $user, $password) or die (mysql_error());
  if (is_resource ($this -> db))
  {
    mysql_select_db ($database) or die (mysql_error());
    mysql_set_charset('utf8',$this -> db); 
  }
}

private function breakDB()
{
  if (is_resource ($this -> db))
  {
    mysql_close ($this -> db);
    }
}
}

class using MySQL:

class Scraper {
private $urls_array = array();
private $mysql;

public function __construct ()
{
        $this -> mysql = new MySQL ('xxx', 'xxx', 'xxx', 'xxx');
}
private function getURLs($city=NULL,$provider=NULL) {

    /* Get all URLs to scrape */
    $result = $this -> mysql -> db = mysql_query("SELECT `xxx`,`xxx`,`xxx`, `xxx` FROM `URL` WHERE `xxx` = '1'");
   }
   }

The line i am having probs with is:

$result = $this -> mysql -> db = mysql_query("SELECT `xxx`,`xxx`,`xxx`, `xxx` FROM `URL` WHERE `xxx` = '1'");

I dont know how to use $mysql correctly so it sends the mysql_query to the connection i opened in the constructor? Any help apreciated THANKS!

  • 写回答

3条回答 默认 最新

  • duanniu3385 2011-02-22 01:07
    关注

    You just need to rearrange the line a bit:

    $result = mysql_query("SELECT `xxx`,`xxx`,`xxx`, `xxx` FROM `URL` WHERE `xxx` = '1'",
                          $this -> mysql -> db);
    

    I would suggest creating a query method in your MySQL class though and making the db private.

    At its most basic:

    class MySQL {
       private $db;
    
       // ... as before ...
    
       public function query($queryText) {
           return mysql_query($queryText, $this->db);
       }
    }
    

    Of course, using mysqli might be a better idea all round.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大