How to fetch and display "unpivot" SQL in php? - php

I built a table which had to contain 42 columns which would be a large one, I decided to use rows as columns and used a column to add values of the respective fields. I've used dtsid as common entity.
Assuming that there are different values for every dtsid.
If in a page, I set $dtsid = $_GET['dtsid'] and run this url "exam.ple/result.php?dtsid=%Any_dtsid%"
How do I fetch the data and display values in different parts of html using php?

Related

Is there a way to add multiple values with same ID in two different tables?

Using PHP and mySQL, I need to add multiple values in 2 mysql tables : the first table would have the more important informations and the second one would have the less important informations about each items.
To be more clear, ONE element would have his informations split in two tables.
(I need this for some reasons but two of them are : having the first table the less weight possible, and the second table would store datas that will be erase after a short time (meanwhile the first table keeps all the datas it stored).)
In the best scenario, I'd like to add a row in each table about one item/element with the same id in each table. Something like this :
Table 1 id|data_1_a|data_1_b|...
Table 2 id|data_2_a|data_2_b|...
So if I add an element which get the ID "12345" in the table 1, it adds the datas in the table 2 with the same ID "12345".
To achieve this, I think of two solutions :
Create the ID myself for each element (instead of having an auto_increment on table 1). The con is that it would probably be better to check if the ID doesn't already exist in the tables everytime I generate an ID...
Add the element on table 1, get its ID with $db->lastInsertId(); and use it to add the element's datas on table 2. The con is that I have to add one element by one element to get all the IDs, while most of the time I want to add a lot of elements (like one, two or three hundreds !) at once
Maybe there's a better way to achieve this ?
lastInsertId() reports the first value generated by the last INSERT statement executed. It's reliable to assume that when you insert many rows, they are given consecutive id values following that first value. For example, the MySQL JDBC driver relies on this assumption, so it can report the set of id values generated.
This assumption breaks only if you deliberately set innodb_autoinc_lock_mode=2 (interleaved). See https://dev.mysql.com/doc/refman/8.0/en/innodb-auto-increment-handling.html for details about that.
But if it were my task, I would still choose to use a single table. When you find you don't need some of the columns anymore, use UPDATE to set them to NULL. This will eliminate the problems you're facing with assuring the same id is used across two tables.

transpose column to row php html mysql

I have some data in a MYSQL database table that I need to "print" in a HTML web page, but I need that this information be shown dynamically in a 3 row and a maximum 12 column table.
Each column must show three sets of information, that will be displayed in each row, like the table below.
Is there a way to do this using PHP and/or HTML?
Thanks for the help

Dispalying the result of different tables using tabs

I want some suggestions regarding this topic. I have different types of tables. What I want is whenever mysql returns results from multiple tables it gets displayed according to table's name. Also I am using pagination so that if one table returns more than 10 results the results get displayed on multiple pages. How can I achieve this?

Laravel: Split model across two tables

I have a model titles
My titles DB table is getting rather large 100,000+ rows of data.
What is the best way to create a new titles2 table and join it with the titles table so that they can both be the same titles model?
Also if I finish adding rows to titles at id 100,000 should i start incrementing titles2 at id 100,001 to that the ids will always be unique?
You can add another column ex. source to differentiate these two table records. This way you can make sure that there's no way we will run into the problem of mixing these two datasets.
However for conveniency, we still want to make sure the id itself is unique across different dataset, which requires you not to use auto-increment for id field, because you'll gonna be the one to handle the id generation.
Some of the issues that I run into includes using the following line to update or insert the record,
$db->table('users')->insert($record);
instead of
User::create($record); // assume you are using auto-increment ids

Storing multiple data in different tables vs. inserting into one table using serialized array in PHP

I need to store the value of about 20 different fields into MySQL. They needs different supported length. Some of them are numbers, some of them have only one character as their value, and some others may need a space as big as text data type in MySQL.
I have 2 choises:
1- Storing all of these values as an array into one record of tables using PHP Serilalize() function (Save PHP array to MySQL?). In this case I need only one field with text data type.
2- Storing these values in a new table inside relational database. But in this case I need different rows that I am not sure about the value that must be stored in them. So I must assign text data type for all of them to support maximum values.
I'm sorry if I could not say my mean completely.
What is your suggestion for this case ?

Categories