2

I am new to PHP and MySQL and hope you can help me with some tipps or comments on this.

I have a MySQL db with a table "Languages" from which I want to fetch all data and then echo it on my page with some HTML tags around it.

In the example below "ISO" is a language code and "$trans" the selected translation of the language names, e.g. English. The output here is a list of (customized) checkboxes with a checkbox for each language.

So far I have the following which works as intended but since I am new to this I was wondering if I actually need the While loop here or if there is a shorter / better way to get the same - in the end I only need to echo the results of my Select.

My PHP:

// select data from db
$tbl = "Languages";

$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
if($conn->connect_error){
    die("Connection failed: " . $conn->connect_error);
} 
$sql = "SELECT * FROM " . $tbl . " ORDER BY ISO ASC";
$result = $conn->query($sql);  
// create array
while($arrLanguages = $result->fetch_assoc()){
    $languages[] = array("ISO" => $arrLanguages["ISO"], "trans" => $arrLanguages[$lang]);
}
$conn->close();

// echo results with some HTML tags
$i = 1;
foreach($languages as $language){
    echo "<input type='checkbox' class='checkSingle' id='language" . $i . "' name='language' value='" . $language['ISO'] . " - " . $language["trans"] . "' />";
    echo "<label for='language" . $i . "'>" . $language['ISO'] . "</label>";
    $i++;
}
5
  • 1
    Seems pretty good to me! Commented Jun 16, 2015 at 14:42
  • 1
    Post your question here codereview.stackexchange.com . stackoverflow is for problems, not for codereviews Commented Jun 16, 2015 at 14:43
  • 2
    don't do select *, then. never suck in fields you're not going to use. just have select ISO, trans, then $languages[] = $arrLanguages. Commented Jun 16, 2015 at 14:44
  • @MarcB: Thanks a lot for that - this is great ! Will definitely apply. :) If you post it as an answer then I'll accept this. Commented Jun 16, 2015 at 14:45
  • @TeeDeJee: Thanks - will try again next time. I tried this a few times before but it took very long to get a response there, if any. Commented Jun 16, 2015 at 14:48

1 Answer 1

2

Never do select * if you don't intend to use all of the fields in the table. It's a waste of bandwidth/cpu cycles to request fields that'll simply be thrown away later.

$sql = "SELECT ISO, trans ...";
... execute ...
while($arrLanguages = ...) {
   $languages[] = $arrLanguages;
}

Since you only selected the two fields you really wanted, $arrLanguages will contain ONLY those fields, and you don't have type in the full-blown array creation. That was done automatically by the fetch_assoc() call.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks again, Marc - will follow your suggestion and accept as soon as I can. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.