Jump to content


Photo

Using variable in a sql statement


  • Please log in to reply
3 replies to this topic

#1 lee_swire

lee_swire
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 01 September 2006 - 03:59 PM

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

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 01 September 2006 - 04:09 PM

the problem is that you're using mysql_result() with the wrong number of arguments. that's what the error means by Wrong parameter count. 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 starting with 0 and one for the column name you're wanting to pull).

hope this helps
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 01 September 2006 - 04:13 PM

You are using variables in single quotes. Variables will not be parsed if they are using in single quotes. So use double quotes:
$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);

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

#4 lee_swire

lee_swire
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 04 September 2006 - 09:54 AM

thanks for your help guys!!!!

::) I knew it would be something simple!! It works a treat now!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users