My situation is that I am creating a wordpress plugin, it creates a table in the directory on activation.
This table holds information entered into the plugin, when information is being entered the user has the option to upload images. The number they will choose to upload I do not know.
The issue I am having is figuring out how to add these URLS into the database, sure I can just put them in there but again I do not know how many URLS need to be added.
As I cannot use normalization with wordpress, how would I store the URLS in the DB. Say they upload 5 images but I do not have 5 separate columns (URL 1, URL 2...)
I should also note that these images will be fetched using a for loop, so each image will be sent off regardless of the number they are uploading.
Any help would be appreciated
Cheers.
Also, You can make additional table with:
id, information_id, filename, ordr
Firstly, insert into infromation_table, get last inserted ID
Then, insert into filenames_table all your filenames in order
Finally, you can select data:
SELECT i.id, i.title, GROUP_CONCAT(a.filename ORDER BY a.ordr ASC SEPARATOR ',') AS filenames
FROM information_table AS i
JOIN filenames_table AS a ON (a.information_id = i.id)
If you want to store all image urls or other data into one DB row, you must create an array with all the information that you need, and then using serialize you can put this array in one filed in the database.
This filed must be TEXT or LONGTEXT to be able to collect all this information.
Then, when you get data back, you must use unserialize and data will be converted to arrays again if needed.
This is how WP stores information in the postmeta table.
Note that in this way you cannot easily query the data, so if this is important to you maybe will be better to store each image on separate row.
Related
I have an assignment where I need make a table for users which houses basic information like fname,lname,email...etc but I also need to store information about the users location address,zip,city,state...etc. A user can have multiple location information.
I've been just doing a csv but people are telling its pretty bad to do that so I'm trying to do things the right way.
I was learning about many to many and it seems to do the trick. But the problem is I need to load this data to a table for viewing.
I just don't see anyway that this would work without having to do a query inside the first query.
ie:
while($row=mysqli_fetch_assoc($query)){
$id = $row['id'];
///Get user locations based on id here.
}
From what I understand if we're storing user ID lets say to a table to make the relation to the locations table and a user can have multiple locations would join be useless here?
I need to pull up records 25 at a time so it's not only supposed to pull one. I'm using datatables with the collapse/show so the data needs to be in a separate container
The Approach of storing location separately in other table with user_id column will be good and as the user will be unique let's say he is unique regarding his email, his locations can be fetched.
Other approach, You can store locations(multiple) in form of object and fetch them then you can get individual location by decoding the json
I have a list of e-mails in a text file and a Person table with an email column.
I need to know which of the emails are in the table, and which are not.
So I was thinking in creating a query and do some kind of left join in my raw e-mail data with the Person table.
Now, I can do this:
select count(*) from PERSON p where p.EMAIL in ("email1#mail","XXXX#mail.com");
But, what I want is to return something like this:
Raw_Email email
email1#mail.com email1#mail.com
XXXX#mail.com null
XXXXX XXXXX
Loading the data from the text file into some table is the best option (the table may be temporary). Loaded data may then be used by the common way.
Another option is in using CSV Engine - but this engine must be available on your server instance (check SHOW ENGINES output). You must create table using this engine joined to your file then use this table by the common way.
Anycase you must have CREATE TABLE privilege.
The last option is in use LOAD_FILE() function. File content will be loaded as one string literal which may be parsed and splitted on separate values (this is easy enough on recent MySQL version). But you must to have secure_file_privilege option set, and if it is not empty then the file must be placed into specified folder. This option does not need in special additional privileges. Alternatively this function can be used in stored procedure where loaded data will be used in dynamic SQL.
I don't think that MySQL supports direct access to text files -- at least without extensions. So, you should import the data into the database. The simplest method is LOAD DATA INFILE.
Then you can use a LEFT JOIN to do what you want:
select e.raw_email, p.email
from staging_emails e left join
persons p
on p.email = e.raw_email;
I have table and in the images column I am saving the names of the images needed for that row data, I want to know if there is any way to read multiple image name which are in the same column(and row) and fetch corresponding image from the folder.
I can read one image name and fetch that image from the folder, but not more than one.
Do I have to create multiple column to achieve this?
Even of you could, its not a good design to store multiple image names on the same column(not just for images, but any type of data in general), research about "normal forms"(or normalization) in database design, the best approach would be to store them on another table with a 1 to many relationship(the images table would have a foreign key to the original row),this way you can easily fetch image by image with a simple join ,and use the current code "for one image" you already have
Microsoft SQL 2016 have JSON storage support. You could store the names in JSON in a single column and return the result as JSON. Something like:
SELECT a.ImageNames FROM table1 a FOR JSON
Then you could load the JSON into an object with a list property.
Some variation of that. It's difficult to tell because we don't know what language or framework you're using other than MS SQL. But if you want to store multiple values in a single column/row, JSON would be a good way to do it.
You could also just use a simple CSV value.
Say I have a database with 3 tables: Stream, Tag and Post
Stream has one tag. Multiple streams can have the same tag.
Stream is owned by one user.
A tag has many posts.
Different tags can have the same post.
Each user that opens his stream gets it's posts (via the tag) and can delete (or hide) the posts he doesn't want to see (but only for him, not for others).
What would be the appropriate way to achieve this? Obviously I'll need some kind of soft-delete flag on posts. I currently have 2 ideas in mind:
Create another table that will act as the view of the stream with columns stream_id, post_id and is_deleted
Add deleted column to Stream that holds a JSON array of deleted posts' ids. The problem with this approach is querying based on is_deleted state.
Are there any better ways?
Note: Currently, I need Tag to stay in it's own table, rather than storing it as text in a column in Stream
You should have a new table with the columns post_id, is_deleted & user_id.
In this way you can manage it easily or else it will be hectic work as per your options are considered.
The table name will be like cross reference table ex: user_deleted_posts_xref
I have an Activity Feed Module where i am storing Activity that's happening in a Company that has multiple members. The Activities are usually specific to USERs based on their Role. So i want to store the Users whom the activity should be shown to inside the table column so that while fetching the activities i do not have to query the users associated with that activity which would slow down the Data retrieval process.
since MySQL does not have array data type which is the best way to do it?
Right now i have
activities(id, category, action, date)
activity_users(id, activity_id, user_id)
Is This easier to query the activities which are based on the particular type of users that to add a column users and story an array [1,3,5,8] and try to query that from the activities table
Yes, you can use serialize() / unserialize() but you cant fire query on that fields the if need anything specific from that serialized data ( you ll have to fetch all , unserialize and then can perform search ) OR you can save data comma separated ( you can perform any query on that ) OR you can get seprate field for each data
If your code in <?php you can use json_encode()/json_decode(), serialize()/unserialize() and write string as normal. But it will be hard to build right condition for select this kind of data. If u need to find somthing use %LIKE% - not always helps. I suggest you to create separate fields for each kind of data.