使用mysqli和php从数据库中检索语言短语

时间:2014-02-03 08:54:44

标签: php mysqli

我有以下3个表格:

短语

PhraseID PhraseType Phrase
14       error      regexNameError
15       error      regexSurnameError
16       error      regexEmailError

Phrase_Language

PhraseLanguageID PhraseID LanguageID NativePhrase
4                14       1          Name must contain at least two characters, and may not contain numbers.
5                14       2          Nom doit contenir au moins deux caractères, et ne peut contenir que des chiffres.
6                14       3          Naam moet ten minste twee karakters bevat, en geen nommers nie.
7                15       1          Surname must contain at least three characters, and may not contain numbers.
8                15       2          Nom doit contenir au moins trois caractères, et ne peut contenir que des chiffres.
9                15       3          Van moet ten minste drie karakters, en geen nommers nie.
10               16       1          Please enter a valid email address.
11               16       2          S'il vous plaît entrer une adresse email valide.
12               16       3          Voer asseblief 'n geldige e-posadres.

语言

LanuageID Language
1         en
2         fr
3         af

我正在尝试做的是将所有错误短语放入某种数组或对象或类或我可以在PHP脚本中轻松使用的东西。

目前我正在使用以下查询来获取短语类型'错误'的所有短语:

  

SELECT * FROM Phrase JOIN Phrase_Language USING(PhraseID)JOIN语言使用(LanguageID)WHERE Language ='en'AND PhraseType ='error';

这让我得到了我需要的东西,但是我不知道如何把它变成一个对象或者我可以这样查询的东西:

  

$ phraseError-> regexNameError-> NativePhrase

因为我需要的是NativePhrase。我已经按照PhraseType对它们进行了分组,这样我就不必将整个表加载到我的查询中,而且我只得到了我需要的东西。 - 从另一个帖子得到了这个提示。

我最终得到的是尽可能少的查询,以便我的网站不会因为整个节目中的多个(/重复)查询而变慢,但也没有包含我的信息的长查询不一定需要在当前页面上。

1 个答案:

答案 0 :(得分:0)

这应该有效 -

<?php
    //Selecting only the Phrase and the NativePhrase columns.
    $query = "SELECT Phrase, NativePhrase FROM Phrase JOIN Phrase_Language USING(PhraseID) JOIN Language Using(LanguageID) WHERE Language = 'en' AND PhraseType = 'error'";

    //Run the query.
    $res = $db->query($query) or die(mysqli_query($db));

    $PhraseError = Array();

    while($row = $res->fetch_assoc()){
        $PhraseError[$row['Phrase']] = $row['NativePhrase'];
    }

    /*
        This will now allow you to search like this,
        echo $PhraseError['regexSurnameError'];
        //will return the nativePhrase for that.
    */
?>
相关问题