Page 1 of 1
SQL row results based on var
Posted: Thu Sep 28, 2006 7:53 pm
by plankguy
So i'm trying to query my sql table based on the variable that i've past from another page. ie: the user selects an option from a menu, then is sent to a template page with a variable '$page = $_GET' and based on the variable I need to get the info relating to the variable from my database.
So if the variable was '4', I need to get all the values of columns from row 4. I jsut have no idea where to start.
Code: Select all
<?php
$page = $_GET['page']; // var is 4
$query = "SELECT `index`, `name`, `description`, `genre` FROM `table`";
$result = mysql_query ($query);
$row = mysql_fetch_row($result);
echo $row["name"];
echo $row["description"];
echo $row["genre"];
?>
But of course I get the first row, where I want row 4.
Help.
Posted: Thu Sep 28, 2006 8:02 pm
by volka
Code: Select all
<?php
$page = (int)@$_GET['page'];
$query = "SELECT
`index`, `name`, `description`, `genre`
FROM
`table`
LIMIT
$page,1
";
$result = mysql_query ($query) or die(mysql_error());
if ( $row = mysql_fetch_row($result) ) {
echo $row["name"], ' ',
$row["description"], ' ',
$row["genre"];
}
else {
echo '...';
}
?>
Posted: Thu Sep 28, 2006 8:09 pm
by printf
You need to add the
WHERE to the query so you can limit the result based on the question you will ask in the WHERE part of your statement!
Code: Select all
<?
if ( isset ( $_GET['page'] ) )
{
$r = mysql_query ( "SELECT index, name, description, genre FROM table WHERE some_column = " . intval ( $_GET['page'] ) ) or die ( 'Query Error: ' . mysql_error () );
if ( mysql_num_rows ( $r ) > 0 )
{
$data = mysql_fetch_assoc ( $r );
echo $data['name'] . "<br />";
echo $data['description'] . "<br />";
echo $data['genre'] . "<br />";
}
else
{
echo 'query returned no results';
}
}
else
{
echo 'data expected is missing, action canceled';
}
?>
me!
Posted: Thu Sep 28, 2006 10:23 pm
by plankguy
THanks for the help, but i'm getting an error:
Query Error: You have an error in your SQL syntax near 'index, name, description, genre FROM franchise_listing WHERE index = 0' at line 1
The $page var should be equal (=) to 'index' column, and it should be displaying that info. So I have:
Code: Select all
<?php
$r = mysql_query ( "SELECT index, name, description, genre FROM table_name WHERE index = " . intval ( $_GET['page'] ) ) or die ( 'Query Error: ' . mysql_error () );
?>
Doesn't work....any ideas? (BTW the 'index' column just contains numbers [0,1,2,3,4,5,etc...])
thnx
Posted: Fri Sep 29, 2006 1:16 am
by Christopher
I think you might try putting index in backticks (`index`) because it is a keyword. The index column is often named `id`.
Posted: Fri Sep 29, 2006 5:16 am
by Rovas
Check with mysql that the query is good (it' s a matter of the fact of putting the name of the table).
Put
Code: Select all
WHERE table.index=" .S_GET["index"] ." ORDER BY name"
.
You don' t need
because the value will be numeric but you should check the index as security measure if it is numeric before quering the table
Posted: Fri Sep 29, 2006 5:21 am
by miro_igov
Rovas wrote:Check with mysql that the query is good (it' s a matter of the fact of putting the name of the table).
Put
Code: Select all
WHERE table.index=" .S_GET["index"] ." ORDER BY name"
.
You don' t need
because the value will be numeric but you should check the index as security measure if it is numeric before quering the table
This would be bad practice if you don't check the $_GET['index'] content, because someone could inject another sql query or just make the query not working
