Reputation: 41
A self taught newbie here looking for advice...
I have very simple PHP query against a MySQL database that is suppose to generate a drop down menu populated with information from the MySQL database. I was wondering if based on the code included in this posting someone could steer me in the right direction or lend a suggestion as to what I should.
P.s. the MySQL database is products the table I am selecting from within is products and the column I am trying to select is description.
<?php
$adConn = mysqli_connect("localhost", "user", "password", "products");
$result = "SELECT * FROM products where Description order by descending";
$result = mysqli_query($adConn, $result);
echo "<select name='product'>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='" . $row['Description'] . "'>" . $row['Description'] . "</option>";
}
echo "</select>";
?>
Upvotes: 4
Views: 57
Reputation: 41
I want to thank everyone for your comments and suggestions. This web community turned out to be pretty good. I am glad that there are still people on the web willing to brainstorm and help a newbie!
Have a great day!
Upvotes: 0
Reputation: 9024
You need to learn the basic concepts of how to make a SQL statement .
Here are some basic queries you can take a look at for example:
Simple straight forward select all:
SELECT * FROM products;
Select where column value equals to something:
SELECT * FROM products WHERE product_category=foo;
Select and order by column ascending(ASC
) or descending(DESC
) order :
SELECT * FROM products ORDER BY product_id DESC;
now lets combine them:
SELECT * FROM products
WHERE product_category = foo
ORDER BY product_id DESC;
It is good practice to keep all SQL functions in uppercase and column names and values in lowercase making it easy to read. Also, don't use mysql_*
since it's deprecated and removed from PHP 7. Instead, use mysqli_*
.
Upvotes: 0
Reputation: 538
If you're only looking for the descriptions from your products table, you can change your select statement to something like this:
SELECT description
FROM products
ORDER BY <column name> desc;
This will give you all the descriptions on your products table, in descending order based on a column name. Not sure what you are trying to order by on the products table, but you need to add a column name to your ORDER BY
clause.
Note - You had description in your WHERE
clause, so if you are looking for a specific description, you could do something like this instead:
SELECT description
FROM products
WHERE description = 'some specific description';
Returns one specific description only, thus no need for an ORDER BY
clause.
Upvotes: 0
Reputation: 3515
Your where
condition is not correct. Also look for order by
.
Try this :
$result = "SELECT * FROM products where Description = 'SOME VALUE' order by SOME FIELD desc;
Refer this : http://dev.mysql.com/doc/refman/5.7/en/select.html
Also you are mixing mysqli_*
and mysql_*
.
Please don't use mysql_*
it is deprecated and removed from PHP 7.
For mysqli : http://php.net/manual/en/book.mysqli.php
Upvotes: 2