Sprintf problem again

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Sprintf problem again

Post by papa »

Hi,

Code: Select all

    function mySQLmatch($col, $data, $mySQLtable) {
        //DB connect
        $link = $this->dbConnect();
        //MySQL query
        $query = sprintf("SELECT %1$s FROM %s WHERE %1$s = '%s'",
        mysql_real_escape_string($col, $link),
        mysql_real_escape_string($mySQLtable, $link),
        mysql_real_escape_string($data, $link));
        if(!$result = @mysql_query($query)) {
            $this->errorMSG[] = "Query failed: " . mysql_error();
            return false;
            exit;   
        } else {
            return mysql_num_rows($result); 
        }
    }
I'm doing exactly what is stated on php.net regarding using the placeholders and assigning an "id" to be able to re-use them.

I need to use $col twice.

Error:
Warning: sprintf() [function.sprintf]: Too few arguments in C:\wamp\www\test\MySQL\mySQLConnect.php on line 181

Thanks
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Sprintf problem again

Post by onion2k »

SELECT %1$s FROM %s WHERE %1$s = '%s' has 4 replacements ... you're only supplying 3.

EDIT: Correction.. the reason it's not working is because your assignments eg %1$s are being converted into %1 ... because you've used double quotes $s is being replaced by it's value (eg nothing). Either use single quotes or backslash the $ signs.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Sprintf problem again

Post by papa »

Ok thanks!!!
Post Reply