TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Messing around with OOP (http://www.talkphp.com/absolute-beginners/3830-messing-around-oop.html)

9three 01-03-2009 08:00 AM

Messing around with OOP
 
I'm just messing around with OOP since I've never used it before. I'm able to make a connection, select my database, and query it. I'm having a problem when I want to create an array:

mysql.class.php
PHP Code:

  public function query($query)
  {
    
$this->query mysql_query($query$this->link);
    if (!
$this->query)
    {
      Throw New 
Exception('Unable to query');
    }
  }

  public function 
fetcharray()
  {
    
$this->fetch $this->query();
    
$return = array();
    while (
$field mysql_fetch_array($this->fetch))
    {
      
$return[] = $field;
    }
    return 
$return;
  } 

When I put the code to work this is what it looks like:

index.php
PHP Code:

$connection = new mysql();
$connection->connect('localhost''root''');
$connection->select('9three');
$query "SELECT * FROM clients";
$connection->query($query);
$connection->fetcharray(); 

I'm just trying to pull whatever information is in the table. But its giving me an error:

Warning: Missing argument 1 for mysql::query(), called in C:\Users\9three\Desktop\Server\htdocs\9three\libra ry\9three\mysql.class.php on line 36 and defined in C:\Users\9three\Desktop\Server\htdocs\9three\libra ry\9three\mysql.class.php on line 25

Fatal error: Uncaught exception 'Exception' with message 'Unable to query' in C:\Users\9three\Desktop\Server\htdocs\9three\libra ry\9three\mysql.class.php:30 Stack trace: #0 C:\Users\9three\Desktop\Server\htdocs\9three\libra ry\9three\mysql.class.php(36): mysql->query() #1 C:\Users\9three\Desktop\Server\htdocs\9three\index 2.php(15): mysql->fetcharray() #2 {main} thrown in C:\Users\9three\Desktop\Server\htdocs\9three\libra ry\9three\mysql.class.php on line 30

I can do it through the procedural but it defeats the purpose of learning :-/

Salathe 01-03-2009 10:44 AM

Code:

  public function fetcharray()
  {
    $this->fetch = $this->query(); // Parentheses shouldn't be here
    $return = array();
    while ($field = mysql_fetch_array($this->fetch))
    {
      $return[] = $field;
    }
    return $return;
  }

The error messages itself point very clearly to the problem if you know how to decipher them. The first error says that mysql::query() was called on line 36 but you forgot to include the first argument (line 36 is: $this->fetch = $this->query();). It also tells us that mysql::query() is defined on line 25 (public function query($query)) which clearly accepts one argument.

You then get the exception because eventhough you haven't provided the SQL string to mysql::query() it'll still run. Since there's no valid SQL command passed through, mysql_query will do it's job properly allowing you to catch the problem and throw the "Unable to query" exception.

So, just remove those parentheses.

9three 01-03-2009 04:35 PM

hmm, I'll have to read up on how to properly read errors like those. I did have another problem though, the loop I created doesn't seem to return anything although I already have information in my table.

The loop looks good to me, but obviously I'm doing something wrong.

Edit: forgot to mention that yes, that was the problem; The parenthesis shouldn't have been there, thanks.


All times are GMT. The time now is 09:18 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0