-1

I have many different merchant stores like (Nike, Reebok, Adidas, Loto etc) in my table.

I want to retrieve all the information related to one particular store like (ex: Nike) and I want to show all the information related to that Nike store.

My table is like:

+----+--------------+--------------+
| id | store_name   | description  |
+----+--------------+--------------+
|  1 | nike         | dasfdasdfas  |
|  2 | reebok       | dfasdfa      |
|  3 | addidas      | adasdf       |
|  4 | loto         | asdfasfdas   |
|  5 | nike         | sadlfjasldfj |
+----+--------------+--------------+

I wrote the code as:

<?php include ('config.php');
$q = mysql_query("SELECT * FROM `Merchant Store` WHERE store=$store_name");
        while($r=mysql_fetch_array($q)){
      ?>

echo <?php $r ?>

How can I do this? I want all the Nike store related content to be displayed.

halfer
  • 19,824
  • 17
  • 99
  • 186
Sudheer
  • 1
  • 1
  • As crypticous said. And I suggest preventing SQL injection like so : `$q = mysql_query("SELECT store FROM coupons WHERE store='" . mysql_real_escape_string($store_name) . "'");` – Rossco Mar 19 '14 at 09:59
  • Your column and table names should be lower case with underscores - if they contain spaces you're making things harder than they need to be. Rename the table to `merchant_store`. Other than that, I am not sure what you are having trouble with - aside from the possible SQL injection the code looks OK. Where does `$store_name` come from? Does it work? – halfer Mar 19 '14 at 10:33

2 Answers2

1
$q = mysql_query("SELECT * FROM `coupons` WHERE store='" . $store_name . "'");

Missing single quotes

Also in your table there is not column named store , so posted query retrieving all columns (using * instead of store)

PS. See this post, why not to use mysql extension

Community
  • 1
  • 1
nanobash
  • 5,419
  • 7
  • 38
  • 56
0

Here:

$q = mysql_query("SELECT * FROM coupons WHERE store='$store_name'");

Also your $store_name variable must have the value of that store whose value you want to fetch and display. Also you most probably have each store name only once in your table. So you can leave out while loop which is used when multiple results are expected. And after running mysql_fetch_array() part you need to get each field in a variable and then display it. So your query will look like

<?php include ('config.php');
$q = mysql_query("SELECT * FROM coupons WHERE store='$store_name'");
$r=mysql_fetch_array($q);
$store_name = $r['store_name'];
$description = $r['description'];

echo 'Store name '.$store_name;
echo 'Description '.$description;
?>
Rolen Koh
  • 719
  • 2
  • 11
  • 21