Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-11-2012, 08:35 PM   PM User | #1
Brian.Wynes
New to the CF scene

 
Join Date: Dec 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Brian.Wynes is an unknown quantity at this point
Question Wrong parameter count for mysql_query() in...

Warning: Wrong parameter count for mysql_query() in /home/harold/public_html/news/index.php on line 147

PHP Code:
$sql mysql_query("SELECT `title`,`content`,`date` FROM `news` WHERE `id` = ?", array($_GET['id']), true); 
Any ideas?

Last edited by Brian.Wynes; 12-11-2012 at 08:51 PM.. Reason: put in php tags
Brian.Wynes is offline   Reply With Quote
Old 12-11-2012, 09:07 PM   PM User | #2
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,503
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
It's unrelated to the issue at hand, but you're attempting to utilize parameter sanitizing, which mysql_query does not natively support.

http://www.php.net/mysql_query

Here's a quick and dirty way to achieve what you're looking to do:
PHP Code:
function mysql_prepare$query, Array $params = array(), $link_identifier NULL )
{

    if ( 
FALSE === strpos$query'?' ) || empty( $params ) )
    {
        return 
$query;
    }

    if ( 
count$params ) !== substr_count$query'?' ) )
    {
        throw new 
InvalidArgumentException(
            
'Placeholder count does not match parameter count'
        
);
    }

    
$parts explode'?'$query );

    
// append the first query part
    
$query = array( array_shift$parts ) );

    foreach ( 
$parts as $part )
    {

        
// grab the next parameter[s]
        
$_params = ( array ) array_shift$params );

        
// sanitize the parameter[s]
        
foreach ( $_params as & $_param )
        {

            if ( isset( 
$link_identifier ) )
            {
                
$_param mysql_real_escape_string$_param$link_identifier );
            }

            else
            {
                
$_param mysql_real_escape_string$_param );
            }

            
$_param '\'' $_param '\'';

        }

        
// append the parameter[s]
        
$query[] = implode', '$_params );

        
// append the next query part
        
$query[] = $part;

    }

    return 
implode''$query );


Usage:
PHP Code:
$query mysql_prepare(
    
'SELECT `title`,`content`,`date` FROM `news` WHERE `id` = ?',
    array( 
$_GET['id'] )
);
$result mysql_query$query ); 
...or, "upgrade" to PDO or MySQLI
__________________
ZCE

Last edited by kbluhm; 12-12-2012 at 04:45 PM..
kbluhm is offline   Reply With Quote
Old 12-12-2012, 01:11 PM   PM User | #3
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
Quote:
Originally Posted by kbluhm View Post
...or, "upgrade" to PDO or MySQLI
I emphasize that!
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 12-12-2012, 05:20 PM   PM User | #4
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,667
Thanks: 45
Thanked 456 Times in 444 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by Brian.Wynes View Post
Warning: Wrong parameter count for mysql_query() in /home/harold/public_html/news/index.php on line 147

PHP Code:
$sql mysql_query("SELECT `title`,`content`,`date` FROM `news` WHERE `id` = ?", array($_GET['id']), true); 
You can't do that! Look at the php function manual for mysql_query:

Quote:
resource mysql_query ( string $query [, resource $link_identifier = NULL ] )
You're using 3 parameters yet the function can only take two - the second being a link identifier (the resource returned from mysql_connect) which is optional. You're passing it the $_GET array instead. The function returns a resource.

The function manual on php.net is very important and will save you a lot of hassle if you learn to use it and understand what it is telling you. You can't just make up your own parameters to a function or assume its the same as another with the same name from a different language or database - you need to target the manual and actually find out how to use it accurately.

You can lookup any function by visiting this link: http://www.php.net/<function_name>
__________________
Please don't be rude: Put your php code in [php][/php] tags. It is a sticky topic at the top of the forum and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is online now   Reply With Quote
Old 12-12-2012, 06:25 PM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,530
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
The mysql_ interface is scheduled for removal from PHP as it has long ago been replaced by the newer mysqli_ interface and by PDO - either of which will allow you to do what you are trying to do using two separate calls - prepare and bind.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:28 PM.


Advertisement
Log in to turn off these ads.