Search query using php and mysql [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Need help on how to filter search results in the search form using PHP and Mysql.
The form contains six fields Product is textbox, Category is drop down select item, Business in text box user enters and state, city are drop down select item and Landmark is textbox the user enters.
Product or Category or Business is mandatory. So the user may enter any one filed or may fill all the fields.
Now I need to get exact search result based on the input. Please Help me to solve this

Try this:
$query = "select * from table_name where 1 ";
if(!empty($_POST['field1']) ) {
$query .= " AND field1 like '".trim($_POST['field1'])."'";
}
if(!empty($_POST['field2'])) {
$query .= " AND field2 like '".trim($_POST['field2'])."'";
}
// and so on
$result = mysql_query($query);

Related

How to get only a text corresponding to the id from MySQL and store it as a variable in PHP [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm a begginer to PHP and I want to know how can I fetch some text from the corresponding ID and store it as a variable in PHP.
The table is like
ID----NAME----ACCOUNT----PASSWORD
1----name1----accont1----password2
2----name2----accont2----password2
3----name3----accont3----password3
Now if I want to get the account2 as text and save it in an variable (say acc2) then what should I do. Assuming that I have connection information in connect.php.
Edit: I want to select the account2 using the ID like from ID 2 select account.
Thanks In Advance!!!
Assuming you use MySQL, the table is named users and you are using PDO, this would get what you need:
$stmt = $conn->query("SELECT * FROM users WHERE ID = 2");
$row = $stmt->fetch()
$account = $row['ACCOUNT']

MySQL - Use Variable column name [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have multiple tables all with different columns.
For example: Table1 has the columns (Username, Group, Car, pet). Table2 has the columns (Username, Extra_groups, Vehicle, animals)
I want to Return the username that has 'Red' anywhere in anywhere in the 2 tables minus the username field of course. Now becuase each table has different columns im describing the table to find what they are then doing the following query however it doesn't want to work.
SELECT Username FROM $Table WHERE $column LIKE '%$search%'
What is the best way to query the columns without knowing what they are going to be and omit one of them from the search.
"SELECT Username FROM " . $Table ." WHERE ".$column . " LIKE '% " . $search. "%'"

How to print data from SQL database to HTML table based on query [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
so I have records in my database that I would like to print to a HTML table IF they belong to the user.
$sql="SELECT * FROM ".$tbl_name." WHERE Username='".$username."'";
The table would read :
Date, Room, Period , Cancel
With cancel being a red cross designed to delete that specific entry from the database :
Booking ID, Date, Period,Type,RoomID, Username are the fields in the database.
How would I go about doing this? I was thinking echoing a table using a for loop but I wouldn't know where to begin?
Try:
$query_rsSearch = "SELECT * FROM ".$tbl_name." WHERE Username='".$username."'";
$rsSearch = mysql_query($query_rsSearch) or die(mysql_error());
$row_rsSearch = mysql_fetch_assoc($rsSearch);
$totalRows_rsSearch = mysql_num_rows($rsSearch);
do {
echo $row_rsSearch['Date']." | ".$row_rsSearch['Room']." | ".$row_rsSearch['Period'];
} while($row_rsSearch = mysql_fetch_assoc($rsSearch));

tag system using PHP MySql [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to create tag system for my article. now I have this input with comma separated :
trance, house, electronica, dubstep, club
now I need to INSERT this tags To Tags Table Like This:
ID NAME ArticleId
1 trance 10
2 house 10
3 electronica 10
4 dubstep 10
5 garage 10
And How to Fetch, INSERT, DELETE , UPDATE This tags?!
For FETCH using Group Contact
SELECT GROUP_CONCAT(NAME) As tags FROM table_name WHERE ArticleId= 10;
will return like
trance, house, electronica, dubstep, club
for DELETE use simple delete query
DELETE FROM table_name WHERE name = 'trance'
Also for UPDATE query you can use like below
UPDATE table_name SET name = 'trance' WHERE ID = 1
EDIT
Use INSERT query
foreach($tag as $each_tag) {
INSERT INTO table_name (ID, NAME, ArticleId)
VALUES (1, '.$each_tag.', 10);
}
above is just skeleton map with your original code.
hope this will sure help you.

Issue with mysql fetch array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am using below code to loop records and display the Country value. If there are duplicate values they are all listed like Australia, Australia, United States, United States, United States. How can I remove any duplicates and just show Australia, United States? I cant use DISTINCT in mysql_query as I need to use an existing mysql_query. Thanks
<?php
while($row = mysql_fetch_array($result)) {
echo $row['Country'];
}
?>
I cant use DISTINCT in mysql_query as I need to use an existing mysql_query.
I am using
$result = mysql_query("SELECT * FROM specials WHERE expiry > CURDATE() ORDER BY Country;") or die(mysql_error());
The best way to do this would be to use DISTINCT clause in your MySQL query:
SELECT DISTINCT Country FROM table_name
But however, since you added this:
I cant use DISTINCT in mysql_query as I need to use an existing mysql_query.
You can simply store all the countries in an array and then use PHP's built-in function array_unique() to remove the duplicate values, like so:
while($row = mysql_fetch_array($result)) {
$countries[] = trim( $row['Country'] );
}
$countries = array_unique($countries);

Categories