Dynadot โ€” .com Registration $8.99

Returning MySQL data in PHP array via function.

Spaceship Spaceship
Watch
Impact
167
I am creating a function and I'm coming to a bit of an issue.

In the function i need to return ordered data into an array and return that array via a function. I have 3 columns on each row I have to return.

Then i need to display them using the foreach loop. Im a bit new to arrays.. any help..
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
What have you done so far? also which mysql function are you using to retrieve the data?
 
0
•••
im trying to make a message box.

im using the mysql array. what exactly im trying to do is take the mysql results and put them in an array. ex subject message id and sender. Then i want to expand the results outside of the custom php function.
 
0
•••
So if I am understanding you correctly, something like this?
PHP:
// assuming you already have a database connection
function getMessages()
{
	$results = mysql_query("
		SELECT id, sender, subject, message
		FROM messages
		ORDER BY id DESC
	");

	$messages = array();

	if (mysql_num_rows($results))
	{
		while ($result = mysql_fetch_assoc($results))
		{
			$messages["$result[id]"] = $result;
		}
	}
	mysql_free_result($results);

	return $messages;
}

$messages = getMessages();

if (count($messages))
{
	foreach ($messages AS $id => $data)
	{
		echo "Sender: $data[sender], Subject: $data[subject], Messages: $data[message]<br />\n";
	}
}
// Sender: Eric3, Subject: Test3, Message: This is a test. 3<br />
// Sender: Eric2, Subject: Test2, Message: This is a test. 2<br />
// Sender: Eric1, Subject: Test1, Message: This is a test. 1<br />
 
2
•••
So if I am understanding you correctly, something like this?
PHP:
// assuming you already have a database connection
function getMessages()
{
	$results = mysql_query("
		SELECT id, sender, subject, message
		FROM messages
		ORDER BY id DESC
	");

	$messages = array();

	if (mysql_num_rows($results))
	{
		while ($result = mysql_fetch_assoc($results))
		{
			$messages["$result[id]"] = $result;
		}
	}
	mysql_free_result($results);

	return $messages;
}

$messages = getMessages();

if (count($messages))
{
	foreach ($messages AS $id => $data)
	{
		echo "Sender: $data[sender], Subject: $data[subject], Messages: $data[message]<br />\n";
	}
}
// Sender: Eric3, Subject: Test3, Message: This is a test. 3<br />
// Sender: Eric2, Subject: Test2, Message: This is a test. 2<br />
// Sender: Eric1, Subject: Test1, Message: This is a test. 1<br />

Yes that is exactly what I was looking for. My have a question tho.. does that put all the values into an array or just the message id
 
0
•••
it retrieves the data from the database in and puts it into a multidimensional array.

Each row in the database is a row in the array with a key that matches the ID. Each row in itself is an array with the following keys:

id
sender
subject
message

The value of course for each is the data pulled from the the row.

If you want to see it in the raw form within the array you could always do:

print_r(messages);
 
0
•••
gotcha.. i understand now.. i was trying to put it into a multidimensional array

---------- Post added at 07:20 PM ---------- Previous post was at 07:15 PM ----------

several*
 
0
•••
okay.. i uploaded it and everything, but for some reason its only displaying one row.. this is what i have. using print_r i only get one result, so for some reason its only passing one var in the array.

PHP:
function msgbox() {
 $conn = mysql_connect('localhost','bbb','bbbb') or die('Iam dying');
 $rs = @mysql_select_db( "bbb", $conn) or die( "Err:Db" );

 #create the query 
 $sql = "select * from messages order by msgid ASC";

 #execute the query
 $results = mysql_query( $sql, $conn);


    $messages = array();

    if (mysql_num_rows($results))
    {
        while ($result = mysql_fetch_assoc($results))
        {
            $messages["$result[id]"] = $result;
        }
    }
    mysql_free_result($results);

    return $messages;
} 


$messages = msgbox();


if (count($messages)) 
{ 
    foreach ($messages AS $id => $data) 
    { 
        echo "Sender: $data[uidsen], Subject: $data[subject], Messages: $data[message]<br />\n"; 
    } 
}
 
Last edited:
0
•••
for some reason its only displaying one row..

First of all, you should edit your code to this:
Code:
...
            $messages["[B]{[/B]$result[id][B]}[/B]"] = $result;
...
(notice the curly brackets around $result[id])

Then, you should be very careful about your table fields. Watch this:

1)
Code:
$messages["$result[[B]id[/B]]"] = $result
This means that you should have field named 'id' in your database.

2)
Code:
 $sql = "select * from messages order by [B]msgid[/B] ASC";
This supposes that your table contain field 'msgid'

3)
Code:
echo "Sender: $data[[B]uidsen[/B]] ...
And this supposes that you have field 'uidsen' in your table.

Is this an error, or you really need all these three id's? :)
 
0
•••
curly brackets are not necessary unless the array key is being quoted, which again in this case is not necessary.

Change:

$messages["$result[id]"] = $result;

to:

$messages["$result[msgid]"] = $result;
 
0
•••
sweet.. it works.. thanks everyone
 
0
•••
Appraise.net
Unstoppable Domains
Domain Recover
DomainEasy โ€” Live Options
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back