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 unset
function. I understand that it 'destroys' a variable to free up memory(?), but I am not entirely sure where/ if to use it.
<?php
$sql = "DESCRIBE $table"; // get a list of all fields and datatypes (ignore the latter)
$retval = mysqli_query($conn, $sql);
$iii = 0; // counter variable
while ($tableHead = mysqli_fetch_array($retval, MYSQLI_ASSOC))
{
$colNames[$iii] = $tableHead["Field"]; // store each column name in array
$iii++;
}
unset($iii);
$sql = "SELECT * FROM $table"; // get a list of all fields and datatypes (ignore the latter)
$retval = mysqli_query($conn, $sql);
$iii = 0; // counter variable
while ($row = mysqli_fetch_array($retval, MYSQLI_ASSOC))
{
// create a 2D array, $iii for x and $jjj for y
for ($jjj = 0; $jjj < $colCount; $jjj++)
{
$tableContent[$iii][$colNames[$jjj]] = $row[$colNames[$jjj]];
}
$iii++; // move onto next row
}
unset($iii, $jjj);
?>
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,i
The front page:
The result: