TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-03-2009, 08:00 AM   #1 (permalink)
The Contributor
 
Join Date: Oct 2008
Posts: 75
Thanks: 4
9three is on a distinguished road
Default 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
9three is offline  
Reply With Quote
Old 01-03-2009, 10:44 AM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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.
Salathe is offline  
Reply With Quote
Old 01-03-2009, 04:35 PM   #3 (permalink)
The Contributor
 
Join Date: Oct 2008
Posts: 75
Thanks: 4
9three is on a distinguished road
Default

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.

Last edited by 9three : 01-03-2009 at 07:55 PM.
9three is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 08:10 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design