PHP search using database records - php

I'm trying to make a PHP search form that lets you search for something, then compares it to the record with the Name field that most closely matches it. Then, it would open the URL specified in the src field of the same record in the database. I've been looking around many websites but I can't find a tutorial for this, does anyone know a good one or know how to do this? Thanks!

Use the MySql like clause
"SELECT * FROM table where field like '%{$searchTerm}%'"
Now fetch the src field from the selected record.

Related

Search query containing a variable

HI I am working on a php mysql project and need some help.
On one of my fields I use a check box to enter a value. The possible options are 9001 14001 and 18001.
If I tick 14001 and 18001 the result that gets stored is 14001,18001.
When I set up a search I have had to set up an if equals for each possible combination. ..not too bad in this case as only 7.
Bit what would I use in an sql query if I wanted to say if (field) contains?
The following query would give you all line where field contains 14001.
SELECT * FROM table WHERE field LIKE '%14001%'
But you might want to reconsider the way you store your data to make it faster.
One way to do it is nicely described here:
http://www.phpknowhow.com/mysql/many-to-many-relationships/

Selecting information from specific row and column in MySQL database

I am trying to retrieve the information in a specific cell of a MySQL database. I have Googled around to try and find something regarding this but it only comes up with selecting from just a single row or single column. However I need the information in a specific cell.
This is the information I am after in the red box...
http://i.imgur.com/5LO1zqV.png
then you can write like this.select only specific column like:
SELECT valist FROM threadfields WHERE valist LIKE "% your identifying value from string %";
these are the basic thing you should look in google or read from w3school:
http://www.w3schools.com/sql/ it is very good site for beginners.

Dynamic content without multiple pages

Let me start by saying i am new to mysql and php, and I'm sure this is a noob question but I've been searching google and can't find any solution.
Basically I want to create 1 template file that will read all of a table from a database with multiple rows and columns but only display one row at a time.
An example would be http://www.w3schools.com/php/php_mysql_select.asp scroll towards the bottom where it shows peter griffen and glen quagmire i would want it to only display 'glen quagmire' or only display 'peter griffen' depending on which link was previously clicked.
would i need to somehow assign an ID to the link url so php knew which row to parse etc.. ?
One straightforward way is to use the query string. Make the link <a href='stuff.php?id=1'>View #1</a>. Then use a where id='$id' clause in the SQL query, setting $id equal to the query string parameter $_GET['id'].

PHP Search engine help

I'm making a search engine which creates a query depending on what's chosen on the search page. Since the query is limited I can not just include anything in it, which is where I have to do the rest with IF clauses and query while-loops. So, there's no age field in the table, but a birthdate field, so I use an IF function to check if the age is correct and then prints out the username(s) in a while loop.
This works alright, but I also need to add two more fields into the search which are as follows: County and City, right now working on the County selection of the page. What I can't figure out by myself is the logic behind how I'm supposed to manage to print out the users that fit all the required fields without having 1000 IF ELSE's.
I thought of SELECTing and filtering out all the correct zip codes to the county/region chosen, then put it in an array, and then validate the output query while-loop against it, but that didn't work so well either.
In my database I have 3 tables which look like this:
county_table
id, name_of_county
municipial_table
id, county_id, municipial_name
zip_code_table
zip, zip_place_name, municipial_id
These are pre-made for my country. So, given the zip code of the user, I will have to do two different SELECT queries to connect it to the county_table (zip->municipial->county).
So basically, what I'm trying to say; I want the search engine to output the users that have the correct data, this depending if the age, region and city fields are selected. They need to be independent and not like:
if($age>X){
if($county==Y){
if($city==Z){
-OUTPUT RESULTS HERE-
} } }
Now, the problem with this is: What if one of the fields are not requested in the search? Say, the age? The county number? The city? I think what I need are non-nested and independent IF blocks, but I'm not sure how to set it up correctly.
Help very much appreciated, thank you alot.
My re-inventing the wheel? Unless you're making a search engine for educational purpose, use something that's already been tested and optimized. See this related question.
.

PHP MYSQL Query Array Field

Ok. I'm using MySQL and PHP. I have a table called "Pictures" and a field in that table called "Tagged." This field is an array of UserIds which I imploded and stored as a string.
example: 114,159,14,334
Lets say I want to return all the rows in "Pictures" where a particular UserID, say 14, exists somewhere in the "Tagged" field's array. How can this be done?
select * from pictures where FIND_IN_SET(14,Tagged);
Start it with a , and end with a , like this: ,114,159,14,334, and then do a SELECT FROM Pictures WHERE tagged='%,14,%'
Just an idea, I really don't know... I've never done this before. Traditional blog tagging may be similar enough that you could look up some tutorials to learn from them.

Categories