PHP Getting data from user and searching into multiple Columns - php

I have a database table named
group(group_name, admin_name);
I've designed a form for searching the group (A Simple TextBox and a Search Button)
Now the query is, whatever the user enters into the TextBox, it should search the string into the entire table and return the values (i.e. Group Name and Admin Name)
I can do this for only a single column
$text = // the String entered by User;
SELECT * FROM `group` WHERE `group_name` LIKE '%$text%'; (For group_name column)
SELECT * FROM `group` WHERE `admin_name` LIKE '%$text%'; (For admin_name column)
I want to do it for both....
I use this for both columns
SELECT * FROM group WHERE group_name LIKE '%$text%' or admin_name LIKE '%$text%';
Here I'm getting Duplicate values
I want to make it like a Search Engine
Please Help

Try-
SELECT DISTINCT * FROM `group` WHERE `group_name` LIKE '%$text%' or `admin_name` LIKE '%$text%';

You can use distinct and group them in where
SELECT DISTINCT *
FROM group
WHERE (group_name LIKE '%$text%'
or admin_name LIKE '%$text%');

Related

How to write select query for pipe "|" separated values in a column?

I edited the post for the real problem
I tried to make a movie website.
I have a movie table
id, moviename, actorsid
1, movie1, 1|2|3
2, movie2, 1|5|4
And i have a actors table
id, actorname
1, actor1
2, actor2
3, actor3
I want to display all actors movies, so i made a SELECT query in movie table, with the actor id that it had
SELECT * FROM `movies` WHERE actors_id IN('4')
run successfully if the movie has i actor id only
SELECT * FROM `movies` WHERE actors_id LIKE '%4%'
run successfully if i don't have any other id contain 4.. like 41 or 14 or 214
Option 1:
you can try something like below:
SELECT * FROM `TB` WHERE
usersid REGEXP '^31\\|' OR
usersid REGEXP '\\|31$' OR
usersid REGEXP '\\|31\\|'
Fiddle: http://sqlfiddle.com/#!9/b88de5/22
Please test all scenario before you implement.
Option 2: You can use FIND_IN_SET after replacing pipe with comma like below:
SELECT * FROM `TB` WHERE FIND_IN_SET("31",replace(usersid,"|",","))
Fiddle: http://sqlfiddle.com/#!9/b88de5/6

retrieve results of 2 table using JOIN failed

I have 2 tables, names and phones I did this for the query
$result = mysqli_query($mysqli, "SELECT * FROM names ORDER BY fname ASC
RIGHT JOIN phones ON phones.id=names.phone_id"
);
I got $result as false. My names table has a column named phone_id and it's a PK of phones's id, like so
names
- phone_id (FK)
phones
- id (PK)
What's wrong with my sql above?
The syntax should go like this:
SELECT *
FROM names
RIGHT JOIN phones ON phones.id = names.phone_id
ORDER BY fname ASC
The ORDER BY had to be moved to the end.
Order by should be the last part of your query
SELECT *
FROM names n
RIGHT JOIN phones p ON p.id=n.phone_id
ORDER BY fname ASC
Start using alias names to make the query more readable
You should use ORDER BY as last clause of your query. Becasue order by i.e. sorting works at last after fetch.
SELECT *
FROM names as n
RIGHT JOIN phones as p ON p.id=n.phone_id
ORDER BY fname ASC
To see more abour ORDER BY you can check the manual link

SELECT * FROM tbl WHERE col1=$var1 and col2=$var2 and col3=$var3

Music Database Site:
I have a table named ps_albums with columns: artist, album, genre_id, and so on.
I also have a second table named ps_users with the following column: user_id, date_joined, fave_genre_1, fave_genre_2, fav_genre_3 and other.
My overall goal is to display only the genres of music that a user selects for the top 3 favorite selections.
I am able to INSERT the genre_id of all 3 favorite genre selections into ps_profiles which hold this info. Now I need to be able to pull the 3 genres_id's and display only them instead off all genres by default.
So by default SELECT * FROM ps_albums ORDER by DESC;
Thus displaying ALL albums on the front page.
Now, when that user clicks 'My Favorite Genres' I do this...
$query= "SELECT * FROM `ps_profiles` WHERE `user_id`= $user_id";
$row = #mysql_fetch_object(#mysql_query($query));
$genre1 = $row->fav_genre_1;
$genre2 = $row->fav_genre_2;
$genre3 = $row->fav_genre_3;
I want to be able to display all records from ps_albums according to the 3 favorite selections from ps_profiles.
How would I setup the select statement?
SELECT * FROM ps_albums WHERE genre=$genre1 AND genre=$genre2 AND genre=$genre3
How would I go about this? There are more then 10 genres but I only want to show the ones selected as favorites from the 3 columns. Hopes this clarifys a bit more.
SELECT * FROM ps_albums WHERE genre in ($genre1,$genre2,$genre3)
You could use either OR's or IN() to match a column value with multiple possible value.
For example:
SELECT * FROM t WHERE a=1 OR a=2 OR a=3;
SELECT * FROM t WHERE a IN(1,2,3);
So for your case, it could look something like this:
$query = "SELECT * FROM ps_albums WHERE genre IN('$genre1', '$genre2', '$genre3')";
I added quotes in the query, I'm assuming genre's are strings?

Selecting multiple tables from database

I am trying to make my php code to read off of two tables in my database. Like if it does not exist in one table it will check the other and see.
$mystyle = mysql_query("SELECT * FROM images WHERE `name` = '$name'");
How would I make it read from the table images and the table images_2
I tried doing this: [but didn't work of course]
$mystyle = mysql_query("SELECT * FROM images, images_2 WHERE `name` = '$name'");
Use UNION(implicit distinct) or UNION ALL :
SELECT * FROM images WHERE `name` = '$name'
UNION ALL
SELECT * FROM images_2 WHERE `name` = '$name'
Assuming images and images_2 has the same table structure, otherwise you have to list the columns' names explicitly instead of SELECT *.
Note that: Use PDO instead of Mysql_* functions, it is deprecated and vulnerable to SQL Injection.
I think you will have to return a count of the rows from the SELECT on images and if zero then run the SELECT against images_2
You can use UNION ,
(SELECT 1 AS sort_col, col1a, col1b, ... FROM t1)
UNION
(SELECT 2, col2a, col2b, ... FROM t2) ORDER BY sort_col;
http://dev.mysql.com/doc/refman/5.0/en/union.html

MySQL select * with distinct id

I am trying to select a row with a distinct id, yet return all the fields.
SELECT * DISTINCT(ID) FROM table WHERE ...
I ultimately need the ID, City, State and Zip. How can I get rows that are not duplicate ID's and return all fields into my mysql_fetch_array?
I have tried the following:
SELECT * DISTINCT(ID) FROM table WHERE ...
SELECT DISTINCT ID * FROM table WHERE ...
SELECT ID,City,State,Zip DISTINCT ID FROM ...
SELECT ID,City,State,Zip DISTINCT(ID) FROM ...
I was reading other questions here and none seem to help. Thanks in advance!
Try using GROUP BY:
select id, city, state, zip
from mytable
group by id
Note that this will return an arbitrary address for each id if there are duplicates.
Demo: http://www.sqlfiddle.com/#!2/c0eba/1

Categories