Jump to content

Using variable in a sql statement


lee_swire

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
Link to comment
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
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.