Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query Strings and variables

Status
Not open for further replies.

boab

Programmer
Joined
May 30, 2001
Messages
75
Location
GB
Guys a quick question,

I have two variables $Area and $value what I want to do is use these variables in a query on a mysql database roughly in the form below

SELECT * FROM db WHERE $Area = $value

and then use num-rows on the result

I have tried both of the ways below and I still keep getting warning messages

(i)$query = "SELECT * FROM db WHERE $area = $value";
(ii)$query = "SELECT * FROM db WHERE".$area."=".$value;

btw the warning messages relate to the result being an invalid argument for the num_rows function Any help in this is mucho appreciated.




THe Start of wisdom is to realise you know nothing. I must be a genius then!
 
are these string values?

If so the SQL needs to be off the form:

"SELECT * FROM MyTable WHERE Text = 'Some text in here'"

Skute

"There are 10 types of people in this World, those that understand binary, and those that don't!"
 
The variables are strings

so would it be right in phrasing it like this

$query = "SELECT * FROM table WHERE '$area'='$value'";

THe Start of wisdom is to realise you know nothing. I must be a genius then!
 
boab:
I've noticed that your use of capitalization in variable names wanders a bit. PHP's variable names are case-sensitive.

Also, how are the values getting into these variables, and have you verified the the values you expect are actually in the variables?

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
The Code I'm using is passing everything through but I am still getting 0 as an output

I'm really trying to make php search each of the areas find the total number of a response and then calculate the percentage that the answer is of the total. I Kno that there are values in the database as I've queried from the command line and returned values under the field names

to be truthful it's really starting to hack me off!:)


<?
include ('Include.inc');

mysql_connect($DBhost,$DBuser,$DBpass);
@mysql_select_db("$DBName");

function trawl(){

$qTrawl = "SELECT * FROM feedback";
$result = mysql_query($qTrawl);
$num = mysql_num_rows($result);

return $num;

}


function perC($crieria,$value,$total){

$qPerC = "SELECT * FROM feedback WHERE '".$criteria."' = '".$value."'";

$result = mysql_query($qPerC);
$chk_num = mysql_num_rows($result);

if($chk_num < 1)
{
$percent = 0;
}else{
$percent = ($chk_num/$total)*100;
}

return $percent;
}
?>
<tr>
<td>Computer Experience </td>
<td>Beginner</td>
<td><?php echo perC("2a","Beginner",trawl()); ?></td>
<td>Intermediate</td>
<td><?php echo perC("2a","Intermediate",trawl()); ?></td>
<td>Advanced</td>
<td><?php echo perC("2a","Advanced",trawl()); ?></td>
<td>Expert</td>
<td><?php echo perC("2a","Expert",trawl()); ?></td>
<td bgcolor="#333333">&nbsp;</td>
<td bgcolor="#333333">&nbsp;</td>
</tr>
<tr>


THe Start of wisdom is to realise you know nothing. I'll be a genius then!
 
hi

you have to use it like this:
$query = "SELECT * FROM table WHERE $area='$value'";
instead of
$query = "SELECT * FROM table WHERE '$area'='$value'";

andreas owen
aowen@swissonline.ch
 
I'm not getting an error / warning from the mysql_num_rows function but I just seem to be returning 0 as an out put but the only thin gthat I can see that would cause the function out put 0 would be mysql_num_rows

THe Start of wisdom is to realise you know nothing. I'll be a genius then!
 
I see multiple problems.

In your definition of the function perC(), you name the input parameter "$crieria", yet you refer to the variable "$cri[red]t[/red]eria" in the code.

In your query in perC():

$qPerC = "SELECT * FROM feedback WHERE '".$criteria."' = '".$value."'";

You're creating a query that will read:

SELECT * FROM feedback WHERE '2a' = 'Beginner'

The string '2a' will never be equal to the string 'Beginner'. If $criteria is the name of a column, you must either leave it bare or (in the case that you unwisely use a MySQL reserved word as a column name) put it inside backquotes.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top