Page 1 of 1

Sprintf problem again

Posted: Fri Oct 17, 2008 6:29 am
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

Re: Sprintf problem again

Posted: Fri Oct 17, 2008 6:31 am
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.

Re: Sprintf problem again

Posted: Fri Oct 17, 2008 6:35 am
by papa
Ok thanks!!!