Is joining tables necessary

I was wondering if working with multiple tables requires joining in the sql query or if there is a more rudimentary way? For example i’m trying to figure out how a user can load an image to a post they create and have that image only be shown in that post they create. There would be an image table and post table and the post table would hold the id of the image from the image table. I’m just seeing if joining is the only option. Thank you

Sure you can do it without joining tables (I do it all the time) for example in the users table you can have id as part of the table then in the post that you want to show the image you can have a column called user_id in a table called cms as an example. Then one can simply write a query like the following as an example →

$query = 'SELECT image FROM cms WHERE user_id = :user_id'';

I personally don’t see anything rudimentary about doing it this way as in my opinion it’s easy to see the logic behind it doing it this way. There’s nothing wrong doing this either way and it’s just a coding preference of the programmer.

1 Like

This is backwards. The post is the primary piece of data. The image is related to the post. When you insert the post data, this establishes a post id (the autoincrement primary index column in the post table.) you would have a post id column in the image table, to relate the image(s) back to the post they correspond to.

When you query to get the post data you are currently trying to display, the simplest way to get the matching image data is to use an appropriate type of join query.

1 Like

Thank you are there any resources you can recommend to study image/file uploads to a database, especially involving a cms type of system? This does make sense but I think I need to go back and learn more about image uploading to a database.
The php book im learning from starts with the very basics of uploading and displaying an image without involving a database, but when it shows the process involving uploading to a database and retrieving the corresponding image there are other activities taking place tied to that code so its difficult to decipher only the code involving image uploads and displaying them.

The actual image file should be stored in the file system (this is what the file system is designed to be used for), with the image information, including the post id the image(s) are related to, stored in a database table.

After you have validated all the post and image data, deal with unique image file naming, and move the uploaded image file(s) to its(their) final location, you would insert a row in the post table, get the last insert id from that query, and insert a row into the image table for each image related to the post.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service