I am new to PHP and hope someone can help me with this.
I currently have the following PHP which is part of an Ajax call in jQuery. When entering IDs manually instead of ? (e.g. 1,2,3,4,5) then this works as intended but when I use the query as below it only returns one item as shown below so I believe the combination of IN(?) and my attempt to prevent SQL injection don't work here.
Can someone tell me what I am doing wrong here ?
Also, this creates a multi-dimensional array and I was wondering if this could be simplified since I only need the ID (tID
) and value (content
) for each item.
My PHP:
$content = implode(",", $_POST["content"]); // an array containing IDs retrieved from Ajax
$languageFrm = $_POST["languageFrm"];
$stmt = $conn->prepare("SELECT tID, " . $languageFrm . " FROM TranslationsMain WHERE tID IN(?) ORDER BY tID");
$stmt->bind_param("s", $content);
$stmt->execute();
$result = $stmt->get_result();
while($arrTranslations = $result->fetch_assoc()){
$translations[] = array("tID" => $arrTranslations["tID"], "content" => $arrTranslations[$languageFrm]);
}
var_dump($translations);
Current result in Ajax:
array(1) {
[0]=>
array(2) {
["tID"]=>
int(1)
["content"]=>
string(6) "Value1"
}
}
Update:
My issue is that even though the posted links and current answer seem to refer to the proper solutions I am not able to get the rest of the PHP code working since whenever I use one of the suggested solutions I get the error "Call to a member function fetch_assoc() on a non-object...
".
Many thanks for any help with this,
Mike