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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器