2

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!

1
  • Get rid of the (') around the 1, it's bad practice to call numeric data with a char symbol. Commented Feb 22, 2011 at 1:08

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I agree. Using mysqli or PDO is probably a better solution.
i use later on: mysql_real_escape_string(), can i use that too with mysqli, since its not listed as function? and mysqli seems really to be worth it :)
@kritop: mysqli has the same function, mysqli::real_escape_string(). You just need to call it from the object that you create: $mysqi->real_escape_string($text)
using: $mysqli = new mysqli("xxx", "xxx", "xxx", "xxx"); in the constructor and later on in a function $result = $this -> mysqli->query("SELECT xxx` FROM URL WHERE scrape = 1");` leads to PHP Fatal error: Call to a member function query() on a non-object error what am i missing??
@kritop: Whenever you get that error "Call to a member function...", it means whatever variable is before one of the -> is not an object, probably because you did not initialise it.
2

You could make things a little easier on yourself by including a query function on the MySQL class you've created. The basic jist would look like this:

public function query($sqlQuery)
{
     return mysql_query($sqlQuery, $this->db);
}

You would use it like so:

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

Comments

1

You would have to do this:

$result = mysql_query("SELECT ...", $this->mysql->db);

The $db property only holds the MySQL resource which you can optionally supply to the mysql functions. It would seem to make more sense if you included a query method in your MySQL class so this happens transparently and you can use $this->mysql->query('SELECT ...').

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.