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 02-27-2009, 03:23 PM   #1 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default Some help

Hi!

I'm wondering what the error with the following code is:
php Code:
public function loadQuery($query)
        {
           
            $this->query_sql = $query;
            
            return $this;
           
        }

        public function exeQuery($sql = NULL)
        {
           
            if($sql == NULL)
            {
               
                if(isset($this->query_sql))
                {
               
                    $this->query_result = mysql_query($this->query_sql, $this->conn);
                   
                    if($this->query_result)
                    {
                       
                        return $this;
                       
                    }
                   
                    return 'Query did not work';
                   
                }
               
                return 'Query sql not provided';
               
            }
            
            else
            {
               
                $this->loadQuery($sql)->exeQuery();
           
            }
           
           
        }

I've just done like this:
PHP Code:
$sql "SELECT * FROM `test` WHERE `id` = '4'";
$fetch $db->exeQuery($sql);
    
echo 
$fetch
This should actually give me an error: "Fatal blabla.. cannot convert object to string... blabla".. because it returns $this.. but it doesn't. It just displays blank.

Oh, and btw. It works if I do like:
PHP Code:
$sql "SELECT * FROM `test` WHERE `id` = '4'";
$fetch $db->loadQuery($sql)->exeQuery();
    
echo 
$fetch
Then it gives me:
Code:
Catchable fatal error: Object of class DBmysql could not be converted to string in C:\wamp\www\Eldobado\index.php on line 9
which is correct.
But it doesn't seem like I can link them like I do, and skip the loadQuery.

Anyone see the problem?
__________________
Tanax is offline  
Reply With Quote
Old 02-27-2009, 04:04 PM   #2 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

I don't see anywhere in your code where it should say "cannot convert object to string..."

Is that part somewhere else?
allworknoplay is offline  
Reply With Quote
Old 02-27-2009, 04:10 PM   #3 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Also can we see the rest of your class or is it too big? Like do you have any other properties declared etc...
allworknoplay is offline  
Reply With Quote
Old 02-27-2009, 05:50 PM   #4 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Try putting a return on the line:
PHP Code:
$this->loadQuery($sql)->exeQuery(); 
in your class.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
Tanax (02-27-2009)
Old 02-27-2009, 07:10 PM   #5 (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

Quote:
Originally Posted by Tanax View Post
This should actually give me an error: "Fatal blabla.. cannot convert object to string... blabla".. because it returns $this.. but it doesn't. It just displays blank.

Anyone see the problem?
That's because the return value (which is assigned to $fetch) isn't an object instance, it's NULL.

If some SQL is passed through to exeQuery, the else block is followed. This block just calls $this->loadQuery(SQL)->exeQuery(); then exits the if/else block. Following that route, nothing is returned from exeQuery so $fetch will be NULL which will print nothing when echoed.

As sketchMedia said, just put return before that loadQuery/exeQuery line.
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Tanax (02-27-2009)
Old 02-27-2009, 09:53 PM   #6 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

That's wicked. It worked though! Thanks!

The thing is that:
PHP Code:
$this->loadQuery($sql)->exeQuery(); 
this line, should execute this part of the code in that function:

PHP Code:
if($sql == NULL)
            {
               
                if(isset(
$this->query_sql))
                {
               
                    
$this->query_result mysql_query($this->query_sql$this->conn);
                   
                    if(
$this->query_result
                    {
                        
                        return 
$this;
                        
                    }
                    
                    return 
'Query did not work';
                   
                }
               
                return 
'Query sql not provided';
               
            } 
because no SQL value is passed.
And in THAT code, I return a value. I didn't think I'd need to return the function when I'm returning values IN that function :O

Tricky..
But it's working, thanks!
__________________
Tanax is offline  
Reply With Quote
Old 02-27-2009, 11:19 PM   #7 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Btw!

Quote:
Originally Posted by allworknoplay View Post
I don't see anywhere in your code where it should say "cannot convert object to string..."

Is that part somewhere else?
No, it's a PHP-generated error, fatal errors are caused by PHP, and not really "outputted" anywhere in the code.
It's like.. when you try to connect to a db with the wrong settings, it gives an errormsg that it can't connect to the database, even if you haven't said anywhere that it should output that specific message.

Quote:
Originally Posted by allworknoplay View Post
Also can we see the rest of your class or is it too big? Like do you have any other properties declared etc...
It's quite big, but since it's solved now it doesn't really matter
__________________
Tanax 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 12:14 AM.

 
     

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