Mysql using LIKE to match phone number - php

I have an app where user phone book uploaded on server same like whatsapp,telegram,facebook etc.
User imported phone book structure look like this
Table Name: PhoneBook
id user_id first_name mobile(Varchar(20))
1 100 John +91981000000
2 100 Tom 91981000001
3 100 Ron 9810-000-02
4 100 Mat 91981000003
5 100 Miley 981000004
Table Name: Registered_User (Main Table For Existing Users)
ID Name Phone (Varchar(20))
1 Sam 09811111111
2 Abby 919811000000
3 Ben +19854646464
4 Tom +91981000001
5 Ron 981000002
6 Sam 981111145
So When user wants to see how many friends of my existing contact on the app then first i need to grab all contacts of user with his/her user_id (100 in above example) from PhoneBook table then i need to check if anyone from user_id = 100 phonebook's exist on Table Registered_User and if yes then returns all of Registered_User table column
Here is my query
SELECT * FROM Registered_User as MA
Join PhoneBook as N ON N.mobile Like '%' MA.Phone
where N.user_id = 100
Table Schema
CREATE TABLE `Phonebook` (
`id` int NOT NULL AUTO_INCREMENT,
`user_id` int NOT NULL,
`first_name` varchar(250) NOT NULL,
`mobile` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3520 DEFAULT CHARSET=latin1
The query above returns zero result even when the user 100(user id) has few users in Registered_User table. Can you help me to fix this query. I am not expert in creating query.
Table data should return result Tom 981000001 and Ron 981000002. Please let me know if i missed something and is this the best way to get the get the existing users of the app?

Related

return result with another result set as an alias MySQL

I am looking for some advice on how to build a MySQL query for the results I am looking for.
I have 2 tables tbl_features and tbl_feature_content which contain data required on my page.
tbl_features holds all the relationship info in which content is saved to the tbl_feature_content table via the parent id which is the idno of the tbl_features table.
What I am looking for is to pull a single result from the tbl_features table by its ID number and return that information in that table with also the results from the tbl_features_content table under that alias page_content.
My query to pull data from the table looks like the below.
SELECT
feature_idno AS page_idno,
feature_title AS page_title
FROM tbl_features WHERE feature_idno = 1;
How do i pull all the results from tbl_feature_content WHERE feature_content_parent = feature_idno and store it as page_content.
Table Creation scripts:
CREATE TABLE IF NOT EXISTS `tbl_features` (
`feature_idno` int(6) unsigned NOT NULL,
`feature_title` varchar(200) NOT NULL,
PRIMARY KEY (`feature_idno`)
) DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `tbl_feature_content` (
`feature_content_idno` int(6) unsigned NOT NULL,
`feature_content_parent` int(6) unsigned NOT NULL,
`feature_content_description` varchar(200) NOT NULL,
PRIMARY KEY (`feature_content_idno`)
) DEFAULT CHARSET=utf8;
Sample data:
tbl_features
feature_idno feature_title
1 Feature 1
2 Feature 2
tbl_feature_content
feature_content_idno feature_content_parent feature_content_description
1 1 Something About The Feature 1
2 1 Something else About Feature 1
3 2 Something About The Feature 2
4 2 Something else About Feature 2
SQL Fiddle: http://sqlfiddle.com/#!9/3a61eb/2
EDIT.
What i am trying to achieve is the below.
Page_idno
-- 1
Page_title
-- Feature 1
Page_content
--Something About The Feature 1
--Something else About Feature 1
It sounds as though you are asking how to join tables together - like this:
SELECT
feature_idno AS page_idno,
feature_title AS page_title,
feature_content_description AS page_content
FROM tbl_features
JOIN tbl_feature_content ON feature_idno = feature_content_parent
WHERE feature_idno = 1;
SQLFiddle here: http://sqlfiddle.com/#!9/3a61eb/4

How to create table with dynamic number of columns MYSQL

I'm trying to create table in mysql.
There is $column_str which stores the names of the column.
If we have 3 columns it will be $columns_str="123", 4 columns -> $columns_str="1234"
So, I need to create table using variable $columns_str.
This code creates table1 with 1 column: "123":
$columns_str="123"; $table_name = table1;
$connection->query("CREATE TABLE `kcup`.`$table_name` ( `$columns_str` TEXT NOT NULL ) ENGINE = InnoDB;")
I need table with 3 columns: "1","2","3".
Help pls and thank you!
To create a table with three columns named 1, 2, and 3 when you have a variable $columns_str="123" you could use something like this;
<?php
$columns_str="123";
$table_name = 'table1';
$cols = str_split($columns_str);
$colQuery = '`id` int(11) NOT NULL AUTO_INCREMENT,';
foreach($cols as $col)
{
$colQuery .= "
`$col` TEXT NOT NULL,";
}
$colQuery .= "
PRIMARY KEY (`id`)";
$connection->query("CREATE TABLE `kcup`.`$table_name` ( $colQuery ) ENGINE = InnoDB;")
This would run the following SQL command;
CREATE TABLE `kcup`.`table1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`1` TEXT NOT NULL,
`2` TEXT NOT NULL,
`3` TEXT NOT NULL,
PRIMARY KEY (`id`)
) ENGINE = InnoDB;
You can't create a table with dynamics columns, but you can simulate it with a table like this one
ID FIELD VALUE
1 ID 4345
1 NAME PAUL
1 SURNAME SMITH
2 ID 4346
2 NAME MARC
2 SURNAME BROWN
The PK of that table is ID, FIELD
Addyng a new field is equivalent to add a new row.
So adding the field EMAIL is equivalent to add two rows (one for PAUL and one for MARC) and you will have the following records
ID FIELD VALUE
1 ID 4345
1 NAME PAUL
1 SURNAME SMITH
2 ID 4346
2 NAME MARC
2 SURNAME BROWN
1 EMAIL paul.smith#email.com
2 EMAIL marc.brown#email.com

How to handle a bidirectional many-to-many relationship with PHP

I am trying to setup a bidirectional many-to-many relationship using the same table data, namely User, I also have a link table named Userusers which joins the one user with another user, but I am not sure how to handle the bidirectional side, because the result of my code only shows one direction.
--Table User:
create table User (
UserID int auto_increment not null,
UserFirstName varchar(30) not null,
UserSurname varchar(30) not null,
UserTel char(10),
UserCell char(10),
UserEmail varchar(50) not null,
UserPassword varchar(50) not null,
UserImage varchar(50),
UserAddress1 varchar(50),
UserAddress2 varchar(50),
UserTown/City varchar(50),
UserProvince varchar(50),
UserCountry varchar(50),
UserPostalCode varchar(50),
Primary key(UserID)
)
--Table Userusers:
create table UserUsers (
UserID int not null,
FriendID int not null,
primary key(UserID, FriendID),
foreign key(UserID) references User(UserID),
foreign key(FriendID) references User(UserID)
)
PHP code:
$sql="SELECT * FROM User u INNER JOIN UserUsers uu ON uu.UserID = u.UserID INNER JOIN User f ON f.UserID = uu.FriendID WHERE uu.UserID = " . $_SESSION['userID'];
Actually, no, your tables do show bidirectional relationships.
Consider your table UsersUsers
The column userID represents the person who is FRIENDS WITH friendID.
Say user 1 wants to be friends with user 2
Our UsersUsers table will look like:
UserID | FriendID
1 2
Sure, we see that there is now a relationship between User 1 and 2, but we also see that User1 has initiated the friendship, showing the direction from 1 -> 2
Now when User2 wants to accept the friendship, we can insert another record into the table, making it:
UserID | FriendID
1 2
2 1
There's definitely some better meta data you can add to the relationship table to understand more about the relationship created, but the UserID field should be considered the owner, or sender, and the FriendID should be considered the receiver.
So - you already have most of the work cut out for you, now all you have to do is create your queries to act upon this bidirectional relationship.
Getting all confirmed friends for User1:
We will assume $the_logged_in_user_id = 1
SELECT * from Users U JOIN UsersUsers F ON U.UserID = F.UserID WHERE F.FriendID = '$the_logged_in_user_id'
That will show all the confirmed friendships for $the_logged_in_user_id, assuming this is what you're going for.

Get value selected from drop down list in where clause using php code

My code is like this:
$zone = $_POST['zone'];
$state = $_POST['state'];
$sql = "Select zone_id from tbl_zone where zone_name = $zone";
$query = mysql_query($sql);
I am selecting zone name which is fetched from database and listed in drop down list.
Now i want to get id of zone name to store in other table.
In where clause zone_name is blank.
What changes should be done in above code?
Thanks in advance..
Try this one..
$sql = "Select zone_id from tbl_zone where zone_name = '$zone'";
I suggest you to design a proper database. Like your question is somewhat confusing. Let me tell you the process. First design 2 tables with 2 columns each. Let us assume table 1 as state and table 2 as city. state table contains 2 columns namely zone_id and zone_state. city table contains 2 columns namely zone_id and zone_city. make state table with zone_id as primary key with auto_increment field and give each id with unique state like zone_id 1 as AP, zone_id 2 as Tamilnadu, zone_id 3 as Karnataka, zone_id 4 as Kerala, zone_id 5 as Maharashtra etc...now for the city table add zone_id with repeated values like zone_id 1 as Hyderabad,zone_id 1 as Vijayawada,zone_id 1 as Vizag, etc..zone_id 2 as Chennai,zone_id 2 as Coimbatore, etc...i hope you got my point...If you design like this then retrieval of results will be easy for you.
see this code:
CREATE TABLE IF NOT EXISTS `state` (
`zone_id` int(2) NOT NULL AUTO_INCREMENT,
`zone_state` varchar(25) NOT NULL DEFAULT '',
PRIMARY KEY (`zone_id`)
) ENGINE=MyISAM;
CREATE TABLE IF NOT EXISTS `city` (
`zone_id` int(2) NOT NULL DEFAULT '0',
`zone_city` varchar(25) NOT NULL DEFAULT ''
) ENGINE=MyISAM;

PHP: Idea for friend-system?

I need help on making a friend-system.
I am thinking how to make so both users can see that they are friends together.
Should i just make a user1 field and user2 field, and then when displaying him/her´s friends, it should select where user1 ='$id' OR user2 = '$id' ?
Or should i make two rows each time people are being friends?
Smart way and example would be appreciated. Thank you.
I am storing in mysql database.
My thoughts is exactly in how should i list that who is friend with who. Lets say i use method 1) with user1 and user2 column, then i should have WHERE user1 or user2 is $id (users id) but can this work properly?
I just tried this and it shows the userid for user2 in user1´s friendslist,
but in user2´s friendslist it just shows his own userid and not the user1s..
On a big projects with sharding you should always replicate data for each user.
For not so big project it's okay to keep one table with "initiator" and "accepter" fields for user_id. Don't forget to add indexes and "status" field for friendship
I'm assuming you're storing them in a database. A simple way would be to have a new table, lets call it "friendships" with two columns, user_1, user_2. If two users are friends, they should have a row in this table. This is an extremely simple way of implementing this, but it should work.
SELECT users.name, fr.name
FROM users, friends, users AS fr
WHERE users.user_id = ?
AND users.user_id = friends.user_id1
AND friends.user_id2 = fr.user_id
UNION
SELECT users.name, fr.name
FROM users, friends, users AS fr
WHERE users.user_id = ?
AND users.user_id = friends.user_id2
AND friends.user_id1 = fr.user_id
This will allow you check against an intermediary table for your many-to-many relationship and not have to duplicate (inverse) rows.
CREATE TABLE `users` (
`user_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`user_id`),
);
CREATE TABLE `friends` (
`user_id1` bigint(20) unsigned NOT NULL,
`user_id2` bigint(20) unsigned NOT NULL,
PRIMARY KEY (`user_id1`,`user_id2`)
);
Some good answers here, If I had to do this I would do it one way, this way you can determine if the friendship has been confirmed and also ensure the friendship is mutual, so for example
Lets assume status has two values
0 = Unconfirmed
10 = Confirmed
Using simple tables with status for acceptance levels
CREATE TABLE `users` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`user_id`),
);
CREATE TABLE `friend` (
`user_id` bigint(20) unsigned NOT NULL,
`friend_id` bigint(20) unsigned NOT NULL,
`status` int(11) unsigned NOT NULL,
PRIMARY KEY (`user_id`,`friend_id`)
);
INSERT INTO `users` (`id`, `name`) VALUES
(1, 'Jack'),
(2, 'John');
Jack would like to be friends with John, so you would create a relationship between the two only one way:
INSERT INTO `friend` (`user_id`, `friend_id`, `status`) VALUES (1, 2, 0);
Now you can query the database to find Jacks friends or Jacks requests and Johns friends or Johns requests using simple queries
To find Jacks unconfirmed friends you would use something like
SELECT users.* FROM users JOIN friend ON users.id = friend.user_id WHERE friend.friend_id = 1 AND friend.status = 0
To find Jacks confirmed friends you would use something like
SELECT users.* FROM users JOIN friend ON users.id = friend.user_id WHERE friend.friend_id = 1 AND friend.status = 10
To find Jacks any friend requests you would use something like
SELECT users.* FROM users JOIN friend ON users.id = friend.user_id WHERE friend.friend_id = 1
When someone makes a confirmation of friendship you would perform 2 queries, one for updating the record and one as a reverse confirmation
UPDATE friend SET status = 10 WHERE user_id = 1 AND friend_id = 2;
INSERT INTO `friend` (`user_id`, `friend_id`, `status`) VALUES (2, 1, 10);
On a different note, I would also use a Graph database for complex relationship queries whilst maintaining a firm copy in the MySQL database
Graph teaser for friends of friends to build relationships
MATCH (Person { id: 1 })-[:knows*2..2]->(friend_of_friend) WHERE NOT (Person)-[:knows]->(friend_of_friend) AND NOT friend_of_friend.id = 1 RETURN friend_of_friend.id, friend_of_friend.name LIMIT 10

Categories