When I ran the following query in PHPMyAdmin, it returned the correct number of results. However when I try to echo the results in PHP, it only outputs one result, even when there is more thn one. How do I fix this so that every result is displayed?
$sql1 = "SELECT userFirstname FROM users WHERE userID IN (SELECT userID FROM note_editors WHERE noteID = (SELECT noteID FROM notes WHERE uniqueID = ?))"; $stmt1 = mysqli_stmt_init($conn); if (!mysqli_stmt_prepare($stmt1, $sql1)) { header("Location: note-premium.php?error=sql"); exit(); } else { mysqli_stmt_bind_param($stmt1, "s", $unique); mysqli_stmt_execute($stmt1); $result1 = mysqli_stmt_get_result($stmt1); while ($row1 = mysqli_fetch_assoc($result1)) { $names = $row1['userFirstname']; } } echo($names); Second attempt: I tried creating an array. But this just outputs the word array and the error message, "Notice: Array to string conversion". Why?
$sql1 = "SELECT userFirstname FROM users WHERE userID IN (SELECT userID FROM note_editors WHERE noteID = (SELECT noteID FROM notes WHERE uniqueID = ?))"; $stmt1 = mysqli_stmt_init($conn); if (!mysqli_stmt_prepare($stmt1, $sql1)) { header("Location: note-premium.php?error=sql"); exit(); } else { mysqli_stmt_bind_param($stmt1, "s", $unique); mysqli_stmt_execute($stmt1); $result1 = mysqli_stmt_get_result($stmt1); $column = array(); while ($row1 = mysqli_fetch_assoc($result1)) { $column[] = $row1['userFirstname']; } } echo($column);
$nameseach time - you need to build up an array of values insteadmysqli_fetch_array($result1)echo($column);print_r($column);Notice: Array to string conversion.