snippetphpMinor
Create a 2D array from SQL table
Viewed 0 times
createarraysqlfromtable
Problem
I am a beginner to PHP programming. I wanted to make a 2D array that would store the values of a table into the array. Below is the Frankenstein code I created (every time I wanted to achieve a task I researched a solution, so this is a bunch of solutions plus my own doing put together).
I would be really grateful if anyone could offer any improvements to my code. One thing I am slightly unsure of is the use of the
EDIT: I have attached some pictures of what I have created. The quick report just reads the ID and returns any ID that is not found in the database or in the CSV. The full report is not yet developed but will check in more depth by checking each field one at a time and reporting the differences to the user.
The CSV looks like this:
The front page:
The result:
I would be really grateful if anyone could offer any improvements to my code. One thing I am slightly unsure of is the use of the
unset function. I understand that it 'destroys' a variable to free up memory(?), but I am not entirely sure where/ if to use it.EDIT: I have attached some pictures of what I have created. The quick report just reads the ID and returns any ID that is not found in the database or in the CSV. The full report is not yet developed but will check in more depth by checking each field one at a time and reporting the differences to the user.
The CSV looks like this:
1,a
2,b
3,c
4,d
5,e
6,f
7,g
8,h
9,iThe front page:
The result:
Solution
-
This comment lies.
You copy pasted the whole line and then neglected to update/remove the comment.
-
It's traditional to use a simple
This comment lies.
$sql = "SELECT * FROM $table"; // get a list of all fields and datatypes (ignore the latter)You copy pasted the whole line and then neglected to update/remove the comment.
-
It's traditional to use a simple
i for loop counter variables. I see no reason to use a triple iii or jjj. However, in this case it makes much more sense to use more verbose (and hence, descriptive) names like $row and $col.Code Snippets
$sql = "SELECT * FROM $table"; // get a list of all fields and datatypes (ignore the latter)Context
StackExchange Code Review Q#85786, answer score: 4
Revisions (0)
No revisions yet.