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 08-20-2009, 02:18 PM   #1 (permalink)
The Acquainted
 
Hightower's Avatar
 
Join Date: May 2009
Location: Durham, UK
Posts: 134
Thanks: 9
Hightower is on a distinguished road
Default CodeIgniter Query Bindings

Ok, so I should maybe post this on CI forums, but I like this place too much.

I have the following in my CI script:

PHP Code:
$sql "SELECT articles.body, articles.user_id, users.username FROM articles, users WHERE ? = 1 AND articles.user_id = users.user_id ORDER BY ?";

$query $this->db->query($sql, array('item1''item2'));
echo 
$sql
The output from the echo however is:

Code:
SELECT articles.body, articles.user_id, users.username FROM articles, users WHERE ? = 1 AND articles.user_id = users.user_id ORDER BY ?
It's not replacing the question marks. I've checked the code, and it looks ok.

Anybody got any ideas?
__________________
Hightower's Softpolio
Send a message via MSN to Hightower
Hightower is offline  
Reply With Quote
Old 08-20-2009, 02:47 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,381
Thanks: 5
Salathe is on a distinguished road
Default

Why would you expect the value in $sql to change? It has been many years since I even touched CI code but the replacements should be done internally. Does $query contain what you expect it to contain?
Salathe is offline  
Reply With Quote
Old 08-20-2009, 02:53 PM   #3 (permalink)
The Acquainted
 
Hightower's Avatar
 
Join Date: May 2009
Location: Durham, UK
Posts: 134
Thanks: 9
Hightower is on a distinguished road
Default

Query Bindings:

http://codeigniter.com/user_guide/database/queries.html

Bottom of this page
__________________
Hightower's Softpolio
Send a message via MSN to Hightower
Hightower is offline  
Reply With Quote
Old 08-20-2009, 03:14 PM   #4 (permalink)
The Contributor
 
ioan1k's Avatar
 
Join Date: Mar 2009
Location: US
Posts: 76
Thanks: 0
ioan1k is on a distinguished road
Default

The ? will be replaced once the query is executed automatically, it also may be that CodeIgnitor does not bind field names only values even though the same has been provided, as the documentation shows values being replaced.
__________________
My Portfolio - Work - Need freelance Work?
I've been developing 5 years now, and I learn something new everyday
ioan1k is offline  
Reply With Quote
Old 08-20-2009, 04:02 PM   #5 (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

does the $this->db->query method take in the sql variable by reference? I'm guessing not.
I don't know, never used
CI.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 08-21-2009, 07:45 AM   #6 (permalink)
The Acquainted
 
Hightower's Avatar
 
Join Date: May 2009
Location: Durham, UK
Posts: 134
Thanks: 9
Hightower is on a distinguished road
Default

Quote:
Originally Posted by sketchMedia View Post
does the $this->db->query method take in the sql variable by reference? I'm guessing not.
I don't know, never used
CI.
I'm not exactly sure - all I know is my code is the exact syntax as their example.
__________________
Hightower's Softpolio
Send a message via MSN to Hightower
Hightower is offline  
Reply With Quote
Old 08-21-2009, 10:37 AM   #7 (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

It doesn't pass byRef just looked through the source, do you understand what I'm getting at?

Take this example:
PHP Code:
$sql "SELECT username FROM users WHERE password = '?'"

function 
query($sql$repl)
{
    
$sql str_replace('?'$repl$sql);
}

function 
queryref(&$sql$repl)
{
    
$sql str_replace('?'$repl$sql);
}


query($sql'cheese');
echo 
$sql '<br />''<br />''<br />';


queryref($sql'cheese');
echo 
$sql
Results:
Code:
SELECT username FROM users WHERE password = '?'


SELECT username FROM users WHERE password = 'cheese'
Basically the $sql variable in your example is in a different namespace to $this->db->query's $sql variable (as method/function params are passed byVal by default) therefore when you echo $sql; out it will still equal the value you set.

To replicate the behaviour you want/need, you could do a call-time-pass-by-reference call:
PHP Code:
$sql "SELECT articles.body, articles.user_id, users.username FROM articles, users WHERE ? = 1 AND articles.user_id = users.user_id ORDER BY ?"

$query $this->db->query(&$sql, array('item1''item2')); 
echo 
$sql
This is apparently deprecated behavior however and may result in a warning.

Having not tested CI i can't be 100% thats why it isnt working, but im pretty sure (after a quick skim read of the code)
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 08-21-2009, 11:30 AM   #8 (permalink)
The Acquainted
 
Hightower's Avatar
 
Join Date: May 2009
Location: Durham, UK
Posts: 134
Thanks: 9
Hightower is on a distinguished road
Default

Posted on CI in the end, was a complete school boy error.

Was echoing $sql which doesn't change. Just needed to echo $this->db->last_query() which worked spot on.

Thanks for all the assistance
__________________
Hightower's Softpolio
Send a message via MSN to Hightower
Hightower 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem executing MSSQL query in PHP trmbne2000 MySQL & Databases 2 06-30-2009 02:52 PM
Full Text Searches with Query Expansion Orc MySQL & Databases 4 12-19-2008 07:39 PM
Query With a C# Variable?‏ StevenF The Lounge 5 12-08-2008 04:32 AM
query question Evulness Absolute Beginners 4 04-21-2008 07:46 PM
Query caching xenon Advanced PHP Programming 4 01-29-2008 07:20 PM


All times are GMT. The time now is 01:49 AM.

 
     

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