Ich habe diese Funktion durch mein Javascript-Modell mit Ajax und erwartet genannt json Array zurückWarum kann ich mein Array nicht als JSON wiedergeben?
function languages($host, $dbUsername, $dbPassword, $dbName) {
$mysqli = new mysqli($host, $dbUsername, $dbPassword, $dbName);
$languages = [];
$languagesQ = "SELECT id, language FROM languages ORDER BY language";
if ($stmt = $mysqli->prepare($languagesQ)) {
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($id, $language);
while ($stmt->fetch()) {
$languages[] = array(
'id' => $id,
'language' => $language
);
}
$stmt->free_result();
$stmt->close();
}
$mysqli->close();
print_r($languages);
echo json_encode($languages);
}
Das Problem, dass der null
ist Linie echo json_encode($languages);
Ausgang zu bekommen, also habe ich versucht, das Array zu drucken print_r($languages);
verwenden und es ist überhaupt nicht leer. Es sieht wie folgt aus:
Array
(
[0] => Array
(
[id] => 1
[language] => Afrikaans
)
[1] => Array
(
[id] => 2
[language] => Albanian
)
[2] => Array
(
[id] => 3
[language] => Arabic
)
[3] => Array
(
[id] => 4
[language] => Armenian
)
[4] => Array
(
[id] => 5
[language] => Basque
)
[5] => Array
(
[id] => 6
[language] => Bengali
)
[6] => Array
(
[id] => 7
[language] => Bulgarian
)
[7] => Array
(
[id] => 9
[language] => Cambodian
)
[8] => Array
(
[id] => 8
[language] => Catalan
)
[9] => Array
(
[id] => 10
[language] => Chinese (Mandarin)
)
[10] => Array
(
[id] => 11
[language] => Croatian
)
[11] => Array
(
[id] => 12
[language] => Czech
)
[12] => Array
(
[id] => 13
[language] => Danish
)
[13] => Array
(
[id] => 14
[language] => Dutch
)
[14] => Array
(
[id] => 15
[language] => English
)
[15] => Array
(
[id] => 16
[language] => Estonian
)
[16] => Array
(
[id] => 17
[language] => Fiji
)
[17] => Array
(
[id] => 18
[language] => Finnish
)
[18] => Array
(
[id] => 19
[language] => French
)
[19] => Array
(
[id] => 20
[language] => Georgian
)
[20] => Array
(
[id] => 21
[language] => German
)
[21] => Array
(
[id] => 22
[language] => Greek
)
[22] => Array
(
[id] => 23
[language] => Gujarati
)
[23] => Array
(
[id] => 24
[language] => Hebrew
)
[24] => Array
(
[id] => 25
[language] => Hindi
)
[25] => Array
(
[id] => 26
[language] => Hungarian
)
[26] => Array
(
[id] => 27
[language] => Icelandic
)
[27] => Array
(
[id] => 28
[language] => Indonesian
)
[28] => Array
(
[id] => 29
[language] => Irish
)
[29] => Array
(
[id] => 30
[language] => Italian
)
[30] => Array
(
[id] => 31
[language] => Japanese
)
[31] => Array
(
[id] => 32
[language] => Javanese
)
[32] => Array
(
[id] => 33
[language] => Korean
)
[33] => Array
(
[id] => 34
[language] => Latin
)
[34] => Array
(
[id] => 35
[language] => Latvian
)
[35] => Array
(
[id] => 36
[language] => Lithuanian
)
[36] => Array
(
[id] => 37
[language] => Macedonian
)
[37] => Array
(
[id] => 38
[language] => Malay
)
[38] => Array
(
[id] => 39
[language] => Malayalam
)
[39] => Array
(
[id] => 40
[language] => Maltese
)
[40] => Array
(
[id] => 41
[language] => Maori
)
[41] => Array
(
[id] => 42
[language] => Marathi
)
[42] => Array
(
[id] => 43
[language] => Mongolian
)
[43] => Array
(
[id] => 44
[language] => Nepali
)
[44] => Array
(
[id] => 45
[language] => Norwegian
)
[45] => Array
(
[id] => 46
[language] => Persian
)
[46] => Array
(
[id] => 47
[language] => Polish
)
[47] => Array
(
[id] => 48
[language] => Portuguese
)
[48] => Array
(
[id] => 49
[language] => Punjabi
)
[49] => Array
(
[id] => 50
[language] => Quechua
)
[50] => Array
(
[id] => 51
[language] => Romanian
)
[51] => Array
(
[id] => 52
[language] => Russian
)
[52] => Array
(
[id] => 53
[language] => Samoan
)
[53] => Array
(
[id] => 54
[language] => Serbian
)
[54] => Array
(
[id] => 55
[language] => Slovak
)
[55] => Array
(
[id] => 56
[language] => Slovenian
)
[56] => Array
(
[id] => 57
[language] => Spanish
)
[57] => Array
(
[id] => 58
[language] => Swahili
)
[58] => Array
(
[id] => 59
[language] => Swedish�
)
[59] => Array
(
[id] => 60
[language] => Tamil
)
[60] => Array
(
[id] => 61
[language] => Tatar
)
[61] => Array
(
[id] => 62
[language] => Telugu
)
[62] => Array
(
[id] => 63
[language] => Thai
)
[63] => Array
(
[id] => 64
[language] => Tibetan
)
[64] => Array
(
[id] => 65
[language] => Tonga
)
[65] => Array
(
[id] => 66
[language] => Turkish
)
[66] => Array
(
[id] => 67
[language] => Ukrainian
)
[67] => Array
(
[id] => 68
[language] => Urdu
)
[68] => Array
(
[id] => 69
[language] => Uzbek
)
[69] => Array
(
[id] => 70
[language] => Vietnamese
)
[70] => Array
(
[id] => 71
[language] => Welsh
)
[71] => Array
(
[id] => 72
[language] => Xhosa
)
)
Und meine Frage ist, warum json_encode()
ist gar nicht funktioniert? Mache ich etwas falsch?
Können Sie mir hier einen Schub geben?
Gibt es [this] (http://stackoverflow.com/questions/1972006/json-encode-is-returning-null) Frage Hilfe? – FrankerZ
Versuchen Sie, vor der SELECT-Abfrage mysql_query ('SET CHARACTER SET utf8') zu verwenden – srmilon