How do i use $sql "select from order where id = $id";
cos i used this but am not getting my results display. the idea is to display any id i want from a range.
thanks
Mysql Select Where
Moderator: General Moderators
Re: Mysql Select Where
You have to specify the fields you want from your order table. Or use * if you want everything. For example:
If you want all columns, use "select * from order where id=$id"
If you want data from a range of ids instead of just one id, use "select * from order where id>=$first_id and id<=$last_id", and iterate through the resulting data like this:
Code: Select all
$r = mysql_query("select customer_name, total_amount from order where id=$id") or die("SQL error: ".mysql_error());
$data = mysql_fetch_assoc($r);
// now $data['customer_name'], $data['total_amount'] contain values for this idIf you want data from a range of ids instead of just one id, use "select * from order where id>=$first_id and id<=$last_id", and iterate through the resulting data like this:
Code: Select all
$r = mysql_query("select id, customer_name from order where id>=$first_id and id<=$last_id") or die("SQL error: ".mysql_error());
$n = mysql_num_rows($r);
for ($i=0; $i<$n; $i++)
{
$data = mysql_fetch_assoc($r);
// $data['id'], $data['customer_name'] now contain values from this row (one particular order id)
}Re: Mysql Select Where
1. Maybe MySQL has something against the column named 'order', as it is a reserved word. Always use backticks (at the tilde ~ key) around SQL names: `order`
2. Your query might be vulnerable to SQL injection, always quote end escape the values:
Read my paper for more examples
2. Your query might be vulnerable to SQL injection, always quote end escape the values:
Code: Select all
$id = mysql_real_escape_string($id);
$sql = "select * from `order` where `id` = '$id'";