I have a problem with mysqli result set. I have a table containing a bunch of posts. Each row of the table represents one message. I have several columns, such as ID, heading, body and "public". The common column contains booleans that indicate whether the message should be displayed to everyone, or only to the person who posted it. I have a page where I want to display all public messages, and if you click on the message, you will get a page with a single message and some additional options. For this, I want to load the result of the mysqli query into a two-dimensional array. This will mean an array of messages, and each message will be an array on itself with an identifier, header, body, etc. In the form of columns.
So, I started with the following code. The $ link variable contains the mysqli connection (the witch works fine).
$result = $link->query("SELECT * FROM messages WHERE public = '1'");
$array = $result->fetch_assoc();
print_r($array);
This only results in a one-dimensional array with the last message. So I tried the following while loop:
$result = $link->query("SELECT * FROM messages WHERE public = '1'");
while($message = $result->fetch_assoc()){
$title = $message['title'];
$body = $message['body'];
}
This works in a way: it displays all the messages, but does not put them in an array (after all, I want to perform tasks based on the identifier and the position of the message array in the containing array.) Does anyone know how to convert this query result to a good two-dimensional array? Or a completely different, great way to do this? Thanks in advance.
PS. Sorry for my English, I'm not a native speaker.
source
share