I am new to PHP and MySQL and hope someone can help me with this.
I have a MySql db with a table "TranslationsMain
" and the following PHP query.
This returns all the items from column "German
" (incl. the corresponding ID) from the db table and works correctly so far.
Instead of echoing the whole list (I just did this here for testing), **how can I store the results in an array in a way that each item within the array is stored with its value and a unique ID + how I can I echo specific items from this array by referring to their ID ?
Example:
I would like to echo the item value for ID "xyz
" from this array (without calling the db again since this will be needed for multiple items on the page).
My PHP:
<?php
require_once("includes/header.php");
$tblTranslations = "TranslationsMain";
$conn = new mysqli($servername, $username, $password, $dbname);
if($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM " . $tblTranslations;
$translations = $conn->query($sql);
// for testing only
if($translations->num_rows > 0){
echo "<table><tr><th>ID</th><th>Translation</th></tr>";
while($translation = $translations->fetch_assoc()){
echo "<tr><td>" . $translation["ID"] . "</td><td>" . $translation["German"] . "</td></tr>";
}
echo "</table>";
}else{
echo "0 results";
}
$conn->close();
?>
Update: What I am looking for here is how to create a two dimensional array and how to fetch from it.
Many thanks in advance, Mike