Displaying specific information from 1 SQL SELECT query - php

I have a database table that contains a bunch of different options and their values. The table has 3 columns which are ID, menu_option, and value. So one row may have the following information: 1, "menu_color", "#AB324B".
I want to display the value of each option on the page so the user can edit the option. Right now, i'm creating a query to get the info for each specific option. Like so
SELECT * FROM menu_theme WHERE ID='1'
SELECT * FROM menu_theme WHERE ID='2'
SELECT * FROM menu_theme WHERE ID='3'
...
Instead of making a new query to get the info per row, how can I make 1 query and distinguish what row I want to get the data from and display the data using php?
I'm aware of how to use php while loops with an SQL query, but I can't see how that would work with selecting specific rows.

Maybe something like this
SELECT * FROM menu_theme WHERE ID IN ('1','2','3')

maybe something like this:
<?php
//select all the rows
$sqlSelect="SELECT ID,menu_option,value FROM menu_theme";
$result=mysqli_query($con,$sqlSelect);
while($row=mysqli_fetch_array($result))
{
$id=$row['ID'];
$opt=$row['menu_option'];
$val=$row['value'];
$menuId="input-".$id;
//create the label for the input
echo "<label for='".$menuId."'>".$opt."</label>";
//pre-populate the input with the name,value,id
echo "<input type='text' name='".$menuId."' id='".$menuId."' value='".$val."'/>";
}
?>

Related

How to get Max date with selected row id in Mysql

I have a scenario in which i want to get Maximum Date but not from Whole Table Just from Selected id but i failed.
Here is my table
I run This Query For get my selected id
SELECT * FROM `tbl_methodology` WHERE FIND_IN_SET(6, `col_select_corporate`)
and i got that result
From that result I want to get that record which have maximum Date
I try that query but it not working for me
SELECT * FROM `tbl_methodology` WHERE id IN (1,5,7) AND `col_date` = (
SELECT MAX(`col_date`)
FROM `tbl_methodology`)
Can anyone help me??
Add the where clause twice and you should get the result you expected:
SELECT * FROM `tbl_methodology` WHERE id IN (1,5,7) AND `col_date` = (
SELECT MAX(`col_date`)
FROM `tbl_methodology` WHERE id IN (1,5,7))
And as i commented: it is very bad db design, to save values as CSV

SQL "LIKE" If empty returns all rows

Hello I have 2 textboxes and i want to give to the user the option to choose one in order to find results. The user can search through the id or the name. My problem is because i use LIKE%field% when the user chooses to search through the id the name field stays empty and returns all the table rows. I want to have results only if the user enters some value in the textbox. This is my sql query. I'm using mysql
"SELECT * FROM properties WHERE ID='$id' OR Name LIKE '%$name%'"
Thank you all
If the user has to select which field to search, you can do:
if ($_POST['search'] == 'id') {
$sql = "SELECT * FROM properties WHERE ID='$id'"
} else {
$sql = "SELECT * FROM properties WHERE Name LIKE '%$name%'"
}
You can do this in a single query (values are checked from the query itself):
"SELECT * FROM properties WHERE ('$id'='' OR ID='$id') AND ('$name' ='' OR Name LIKE '%$name%')"
Explanation:
First condition:
The query will select records with ID='$id' only when $id is not empty.
If $id is empty, query will not go for the second part ID='$id'
Second condition:
The query filters records with Name LIKE '%$name%' only when $name is not empty.
If $name is empty, query will not go for Name LIKE '%$name%'.
NB: This technique is extremely useful when you have numerous parameters to check, rather than using a bunch of if...elses at php side.

WHERE IN clause display result by them index in IN

i have a table, just id and name, have a mysql like this
`SELECT * FROM table WHERE id IN(154,12,148,50);`
and i use while loop PHP for display result as normal way :
while($rows= mysql_fetch_array($result)){ echo $rows['id'], echo $rows['name'] }
but result is ordered by id field in IN clause 12 ,50,148,154
i want to result still them index in IN() : 154,12,148,50
is there any way?
thank alot
read FIELD
SELECT *
FROM table
WHERE id IN(154,12,148,50)
ORDER BY FIELD(id,154,12,148,50)

Add link to mysql row result which points to the same result

Hello my data base have various columns , in a mysql fetch I am listing only the title column, but I would like that list to make a link on the already listd result which link will open a new page with the same result but this time listing all the columns from the table on this row.
mt SQL fethc is:
(!$result = mysqli_query($con,"SELECT * FROM tablename WHERE type = 'Panes'"))
and my sql result is: <?php echo $row['name']; ?> -> which result I would like to be link.
I wold like to know if somebody have any suggestion for a possible resolution. Thanks!
You cannot do that if you want to have a single query in your code(base from your query).
You must do another query for the click item or link..
If you do not need all the columns in your table to not use * instead select only what you need to make your code more readable and to be faster to execute.
Revised your string query,
"SELECT id, name FROM tablename WHERE type = 'Panes'"
and in your code to display the result to make a link,
<?php echo $row['name']; ?>
And in your page displayAllColumns.php you must get the passed data through get method
$id = $_POST['id'];
//then make a query for that to select all the data on that id
//just have to type only the string so
"SELECT * FROM tablename WHERE id = '$id'"
//you must use `*` because you need to display all the columns then write your table code to display the result.
Hope it helps
Try echoing an anchor tag. $row['name']
Use something like this:
<?php echo $row['name']; ?>
The display_item.php script then displays the row with the ID in $_GET['id'].

How to call one cell of a database?

I am very new to using PHP so I am asking for some help. I have this code which works fine (I got it online from somewhere).
$data = mysql_query("SELECT * FROM products")
or die(mysql_error());
print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
print "<tr>";
print "<th>title:</th> <td>".$info['title'] . "</td> ";
print "<th>info:</th> <td>".$info['information'] . " </td></tr>";
}
print "</table>";
But what PHP would I use to pull just one cell? For example, I have 9 items in my db with productIDs how could I call just the name or price or description for that particular product?
Thanks in advance.
It sounds like you want a particular row, in which, case you would just changes your query to filter for some specific value that identifies the row. THis might look something like this:
SELECT * FROM products WHERE product_id = ?
Where ? is the known product_id value that you want to receive the full record for.
Also, since you are just learning, I should point out that you should not learn to use mysql_* functions. They are deprecated. Learn mysqli or PDO for interacting with MySQL.
To obtain just one cell try the following:
SELECT CellName FROM TableName Where productId = x
So to get the price you would do:
SELECT price FROM products WHERE productid=x
Where productid equals the specific product you talked about.
if you want to understand a bit more mysql I advise you to go to PhpMyAdmin and try those and think about the result:
SELECT * FROM products;
SELECT title, information AS lol FROM products;
SELECT title, count(*) AS count FROM products;
SELECT * FROM products WHERE title = 'XXXX';
SELECT *, count(*), CHAR_LENGTH(title) FROM products;
SELECT title AS lol FROM products;
you will understand better the query language i thinks.
please read this chapter http://dev.mysql.com/doc/refman/5.0/en/examples.html. it'll really help you.
I just want to say, you can retrieve any columns(s) in any condition column
SELECT column_name(s) FROM products WHERE colmnm_name = ?

Categories