Jump to content

Archived

This topic is now archived and is closed to further replies.

lee_swire

Using variable in a sql statement

Recommended Posts

Hello,

I am new here so please forgive me if this has been posted before, I did use search but couldn't find the answer. 

I have processed a form using php and written some of the information to a file on my server.  This is because the users that use this system do not accept cookies, and I have disable session variables.

I can read the file no problem, but when defining the variables and trying to use them in my SQL statement I get the following error:

Warning: Wrong parameter count for mysql_result() in /www/survey/reports/xml/6.php on line 35

Warning: Wrong parameter count for mysql_result() in /www/survey/reports/xml/6.php on line 40

The PHP code looks like this:


Now read from file recently written too*/

$lifecycle = file('../xml_lifecycle.txt');
$from = file('../xml_date_from.txt');
$to = file('../xml_date_to.txt');

echo "$lifecycle[0] | $from[0] | $to[0] ";

$from_date = $from[0];
$to_date =  $to[0] ;
$life = $lifecycle[0];

// QUESTION 6 START
// First get results from DB
$sql = 'SELECT count(*) FROM `survey` WHERE `6` = CONVERT( _utf8 \'yes\' USING latin1 ) COLLATE latin1_swedish_ci
AND stage = \'$life\' AND date_submitted BETWEEN \'$from_date\' AND \'$to_date\'';
$query1 = mysql_query($sql);
$yes =  mysql_result($query1);

$sql2 = 'SELECT count(*) FROM `survey` WHERE `6` = CONVERT( _utf8 \'no\' USING latin1 ) COLLATE latin1_swedish_ci
AND stage = \'$life\' AND date_submitted BETWEEN \'$from_date\' AND \'$to_date\'';
$query2 = mysql_query($sql2);
$no =  mysql_result($query2);

Does anyone have any suggestions on what is going wrong?  I have spent all day trying different things and am really stuck!!

Thanks,

Lee

Share this post


Link to post
Share on other sites
the problem is that you're using mysql_result() with the wrong number of arguments. that's what the error means by [i]Wrong parameter count[/i]. mysql_result() is used to pull a specific item from a specific record in the results of a query. if you're trying to get the entire row, you need to use mysql_fetch_array() or similar. if you're trying to pull an individual piece of data, mysql_result() takes either one or two additional parameters (one for the row number to pull from [i]starting with 0[/i] and one for the column name you're wanting to pull).

hope this helps

Share this post


Link to post
Share on other sites
You are using variables in single quotes. Variables will not be parsed if they are using in single quotes. So use double quotes:
[code=php:0]$sql = "SELECT COUNT(*) FROM `survey`
        WHERE `6` = CONVERT( _utf8 'yes' USING latin1 ) COLLATE latin1_swedish_ci
        AND stage = '$life' AND date_submitted BETWEEN '$from_date' AND '$to_date'";

$query1 = mysql_query($sql);
$yes = mysql_result($query1, ROW_NUMBER_HERE);[/code]

Also the mysql_result function requires two parameters,
one for the query result ($query1)
and the other the row number

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.