I have this code.
$stmt = $conn->prepare(\"SELECT * FROM UserData WHERE username = ?\");
$stmt->bind_param(\'s\',$username);
//$username = $_POST[\"use
var_dump(stmt['likedFour']);
return 's'
:Because you're missing the dollar sign before stmt
, so PHP thinks you're trying to access the constant stmt
instead of the variable $stmt
. Since you haven't defined that constant, it will fall back to assuming you're trying to access the string "stmt"
. For strings, indices must be numeric so PHP should be throwing you an "Illegal string offset" warning but will try to fix it for you by casting 'likedFour'
to an integer (which will be 0
).
Therefore, var_dump(stmt['likedFour'])
means the exact same thing as var_dump("stmt"[0])
to PHP, which is why you're getting the output "s"
: the first character from "stmt"
.
You first need to retrieve the resulting rows from your query. $stmt
is the mysqli_stmt
object that you use to execute the query and retrieve the results, it is not actually the result itself.
To save yourself headaches in the future, always check whether your query even executed successfully before trying to retrieve the results. Then fetch the row of data:
$success = $stmt->execute();
if (!$success) {
echo $stmt->error;
} else if ($stmt->num_rows == 0) {
echo 'No results matching that username';
} else {
$result = $stmt->get_result();
$row = $result->fetch_assoc();
var_dump($row['likedFour']);
}
If you don't know how many rows will be returned, loop through them just to be safe:
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
var_dump($row['likedFour']);
}