Update MYSQL and SET particular row Content - php

I am trying to update my table t1 which has rows as following :-
id
menu
Currently i am having data in it as
id = "1"
menu = "menu1 ,menu2 ,menu3, menu4"
I am using explode method of PHP to get MENU row of my table t1.
$show_data = mysql_query("SELECT menu FROM t1");
$showrow = mysql_fetch_assoc($show_data);
$showmenu = $showrow['menu'];
$pieces = explode(",", $showmenu);
Now I want to delete content menu3 from row MENU ,
Please provide me which query should i use , UPDATE , ALTER or DELETE.

You should store your menus in a separate table, linked to this one by a unique identifier.
Then edit that table in the usual way.

It is better to separate $menu1 $manu2 $menu3 and $menu4
You can implode() them as a single $string with \t separator
and Insert it to Mysql.
When nessesary,select the field from table
explode() it to separate strings using \t separator
remove the $menu3 variable frome being imploded this time
again implode() them
and UPDATE the field

Related

Is there a SQLI function that cycles through an array to see if any elements are in a MYSQL row?

I have a small SQL table.
There is a 'tags' column that has several words separated by a comma.
Using PHP & MYSQLI I would like to take a Search Value and compare it's individual words with the individual words in the tag SQL 'tags' column.
It is a relatively small database. I can think of a way to do this where I create a seperate column for every tag. But I would rather not. Only if that is the only option.
Example SQL layout,
Table: Books
"title" -- "author" -- "tags"
[Potter]-- [J.K.] -- [Wizards, WandsnShit,Magic]
[50 shades]-- [James] -- [Boobies, Sex]
[Ulysses]-- [Joyce] -- [WTF]
So far my direction has been:
//obtains searchValue from HTML
$searchValue=$_GET["searchValue"];
//turns the values individual words into an array
$proxy = $searchValue;
$tags = explode(" ", $proxy);
//This is where I need help
SELECT * FROM books WHERE tags CONTAINS (cycle through 'tags' array)
If all works correctly, typing "Gandalf is a wizard" should return the book "Potter".
Because "Wizard" is a tag of "Potter" book.
Also while I'm at it. Does the PHP function "explode" alter the original string or create a copy string an alter that?
Thanks in advance.
This is a bad design. Let's search on the net for database normalization.
In your book table should be a unique id (primary key, int, not null, auto increment) field. After that, you need to create a relation table, what has the tags.
For example:
Book table:
id
name
author
Tags table
id
book_id
tag
After that you can use:
$sql = "SELECT * FROM books"
. " INNER JOIN tags ON tags.book_id = books.id"
. " WHERE tags.tag = " . mysqli_real_escape_string($_GET["searchValue"]);
Or you can use LIKE keyword.
Note:
I am always wondering, why a lot of developer create 2 variable for nothing?
$searchValue=$_GET["searchValue"];
//turns the values individual words into an array
$proxy = $searchValue;
$tags = explode(" ", $proxy);
instead: $tags = explode(" ", $_GET["searchValue");

Query find value in comma seperated list mysql

in my script a "restaurant" can have multiple locations so, i made a column in the restaurant table containing a coma seperated list with locations.
Now i want to make a msql query that checks if the id can be fount is this column (comma seperated list) and if so then select it. i came up with this
SELECT restaurant_id,restaurant_name
FROM restaurant WHERE ('.$locIdList.') IN (locationRes)
ORDER BY restaurant_name ASC'
It does work... but i have some restaurants where I added location 16 and 17 so (16,17) now when i do this query for location 16 it shows the restaurant but when i dot this for location 17 it does not... but the whole point was to get the multi values from the comma seperated list.
So how to do this ?
You can use PHP to generate the query for each comma-delimited value. i.e., run a PHP loop on comma-delimited comparison string, convert it into individual items and compare each item through LIKE Operator and an IN () function.
SELECT restaurant_id,restaurant_name
FROM restaurant WHERE ('16') IN (locationRes)
OR
FROM restaurant WHERE ('17') IN (locationRes)
ORDER BY restaurant_name ASC'
The best solution would be to create a relation table that implements the many-to-many relationship between restaurants and locations. Then you can use a solution like How to return rows that have the same column values in MySql to find all the restaurants that are in all locations.
To search for a value in a comma-separated list, you use FIND_IN_SET. But this can only search for one value at a time. If you want to find restaurants that are in all locations, you need to combine multiple calls:
$locArray = explode(',', $locIdList);
$locQuery = implode(' AND ', array_map(function($loc) { return "FIND_IN_SET($loc, locationRes)"; }, $locArray));
$query = "SELECT restaurant_id,restaurant_name
FROM restaurant
WHERE $locQuery
ORDER BY restaurant_name ASC";
If you want to find restaurants that are in any of the locations instead of all locations, change AND to OR.
You should avoid putting multiple values inside a single column.
Instead, it's recommended to create another table locations(location_id, col1, col2, restaurant_id), while the restaurant_id field references to the primary key in table restaurant.
If you have a comma separated list you are pulling from a database, you could use PHP to separate the list and create an array of each item.
$result_of_sql = "restaurant 1, restaurant 2, restaurant 3, restaurant 4, restaurant 5, restaurant 6";
$restaurants = explode(',', $result_of_sql);
echo '<ul>';
foreach ($restaurants as $restaurant) {
echo '<li>' . trim($restaurant) . '</li>';
}
echo '</ul>';
What happens here is first, you pull out all of you restaurants (the comma separated list). Then you use explode to take away the commas and create an array. Then you use the foreach loop to echo the entire array out. trim is just to clean everything up by removing whitespace you might have before and after the restaurant name.

Deleted from comma separated list in MySQL table

I have a comma separated list stored in one row in a column in a mysql database (eg. phrase1, phrase2, phrase3, phrase4). I want to write a query that will deleted one of the values from that comma separated list, say 'phrase3', and its comma, making the list (phrase1, phrase2, phrase4). Ideas?
The list is generated with the following code if that helps:
$q6 = "UPDATE post SET denied_user = CONCAT_WS(', ',denied_user,'$claimer_id'), denied_time = CONCAT_WS(', ',denied_time,NOW()) WHERE post_id = '$post_id' AND user_id='$user_id' AND denied_user<>'' AND denied_user NOT LIKE '%$claimer_id%' AND post_status='active";

Mysql Query In codeigniter

This is my mysql query
$acountry = 1;
$this->db->where_in('varcountry', $acountry);
$val = $this->db->get('tblagencies')->result();
In database table the varcountry filed is stored like this 1,2,3,4 its type is varchar.Each row in table have multiple countries that is the reason to use varchar datatype.
Here i want to select table rows which have $acountry value in the filed varcountry.
How can i do that?The above code is it correct?
You have choosen a wrong data type for storing a comma separated value 1,2,3,4 into varchar,
you should chose a data-type of set, or normalize into a separate table, like :-
create table country (id, name ...);
create table agencies_country ( agency_id, country_id);
insert into agencies_country (agency_id, country_id)
values (x,1), (x,2), (x,3), (x,4);
// meaning 1,2,3,4 = 4 rows
// grabbing result using inner join
Using set is easier, but common practice is to normalize the data (which require some understanding).
I don't like the active record in codeigniter,
is easy to use (not doubt with this),
but it dis-allowed lost of flexibility
Personally I like the construct my own query,
provided you have the understanding of the table schema (which you have to anyway)
use this query..
$search_field = array('varcountry'=>$acountry)
$result = $this->db->get_where('tblagencies' , $search_field );
but in codeignator you can use your own queries like
$sql = "select * from tblagencies where varcountry like '%acountry%'";
$result = $this->db->query($sql);

Move Multiple Tables into One, with Addition of Extra Field Value

I have 31 separate tables (actually I have 365, but lets keep this simple) in a MySQL database, each containing data for a given day. The tables are (badly) named based on the day.
Example:
island01Aug07
island02Aug07
island03Aug07
island04Aug07
...
island31Aug07
I would like to combine all the tables into one master table:
island_08
It would be simple to use INSERT INTO but my problem is that the tables do not have a column to denote the day. It would have to be added into the destination table, and then I would need to populate that when moving/copying the tables over.
Suggestions, advice and solutions welcome.
CREATE TABLE island_08 (mydate DATE NOT NULL, field1 …)
INSERT
INTO island_08 (mydate, field1, field2)
SELECT '2007-07-01', field1, field2
FROM island01Aug07
UNION ALL
SELECT '2007-07-02', field1, field2
FROM island02Aug07
UNION ALL
…
As alternative option you can list all tables in to array like table_name=>mysql_date,
after that loop through and copy data from one table and insert in to another. After data was transferred successfully you can remove the table.
Here is example of getting list of tables and extracting date from it:
$prefix = 'island';
$lenght = strlen($prefix);
$result = $this->query("SHOW TABLES LIKE '{$prefix}%'");
$arrayDates = array();
if($db->num_rows($result))
{
while($v = $db->fetch_array($result))
{
$mysql_table = current($v);
$arrayDates[$mysql_table] = date('d-m-Y',strtotime(substr($mysql_table,0,$lenght)));
}
}
//Now you can walk through your array and copy data from one table tyo another and append you mysql value

Categories