NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page Returning MySQL data in PHP array via function.

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search
6 members in live chat ~  


Reply
 
LinkBack Thread Tools
Old 07-20-2011, 11:57 PM THREAD STARTER               #1 (permalink)
Ray
CEO
 
Ray's Avatar
Join Date: Jun 2005
Location: Tennessee
Posts: 1,894
Ray has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud of
 



Returning MySQL data in PHP array via function.


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..
Ray is online now   Reply With Quote
Old 07-23-2011, 06:08 AM   #2 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,074
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
What have you done so far? also which mysql function are you using to retrieve the data?
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline   Reply With Quote
Old 07-23-2011, 01:35 PM THREAD STARTER               #3 (permalink)
Ray
CEO
 
Ray's Avatar
Join Date: Jun 2005
Location: Tennessee
Posts: 1,894
Ray has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud of
 



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.
Ray is online now   Reply With Quote
Old 07-23-2011, 02:24 PM   #4 (permalink)
Tech Support
Join Date: Mar 2005
Posts: 4,944
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
So if I am understanding you correctly, something like this?
PHP Code:
// assuming you already have a database connection
function getMessages()
{
    
$results mysql_query("
????: NamePros.com http://www.namepros.com/programming/723956-returning-mysql-data-php-array-via.html
        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 />
????: NamePros.com http://www.namepros.com/showthread.php?t=723956
// Sender: Eric1, Subject: Test1, Message: This is a test. 1<br /> 
Eric is offline   Reply With Quote
Old 07-23-2011, 04:37 PM THREAD STARTER               #5 (permalink)
Ray
CEO
 
Ray's Avatar
Join Date: Jun 2005
Location: Tennessee
Posts: 1,894
Ray has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud of
 



Originally Posted by Eric View Post
So if I am understanding you correctly, something like this?
PHP Code:
// 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;
????: NamePros.com http://www.namepros.com/showthread.php?t=723956
}

$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 /> 
????: NamePros.com http://www.namepros.com/showthread.php?t=723956
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
Ray is online now   Reply With Quote
Old 07-23-2011, 04:57 PM   #6 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,074
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
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);
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline   Reply With Quote
Old 07-23-2011, 05:20 PM THREAD STARTER               #7 (permalink)
Ray
CEO
 
Ray's Avatar
Join Date: Jun 2005
Location: Tennessee
Posts: 1,894
Ray has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud of
 



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*
Ray is online now   Reply With Quote
Old 07-24-2011, 09:02 AM THREAD STARTER               #8 (permalink)
Ray
CEO
 
Ray's Avatar
Join Date: Jun 2005
Location: Tennessee
Posts: 1,894
Ray has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud of
 



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 Code:
function msgbox() {
 
$conn mysql_connect('localhost','bbb','bbbb') or die('Iam dying');
 
$rs = @mysql_select_db"bbb"$conn) or die( "Err:Db" );
????: NamePros.com http://www.namepros.com/showthread.php?t=723956

 
#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)) 
????: NamePros.com http://www.namepros.com/showthread.php?t=723956

    foreach (
$messages AS $id => $data
    { 
        echo 
"Sender: $data[uidsen], Subject: $data[subject], Messages: $data[message]<br />\n"
    } 

Last edited by Ray; 07-24-2011 at 09:05 AM.
Ray is online now   Reply With Quote
Old 07-24-2011, 05:55 PM   #9 (permalink)
NamePros Member
 
un4given[MAD]'s Avatar
Join Date: Aug 2008
Location: Solar System \ Earth \ Ukraine \ Kiev
Posts: 43
un4given[MAD] is on a distinguished road
 



Originally Posted by -Ray- View Post
for some reason its only displaying one row..
First of all, you should edit your code to this:
Code:
...
            $messages["{$result[id]}"] = $result;
...
(notice the curly brackets around $result[id])
????: NamePros.com http://www.namepros.com/showthread.php?t=723956

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

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

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

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

Is this an error, or you really need all these three id's?
__________________
Tired playing Freecell, Klondike or Spider? Discover 800 solitaire games at SolitairesUnlimited.com and play'em all!
un4given[MAD] is offline   Reply With Quote
Old 07-24-2011, 06:33 PM   #10 (permalink)
Tech Support
Join Date: Mar 2005
Posts: 4,944
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
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;
Eric is offline   Reply With Quote
Old 07-26-2011, 12:54 PM THREAD STARTER               #11 (permalink)
Ray
CEO
 
Ray's Avatar
Join Date: Jun 2005
Location: Tennessee
Posts: 1,894
Ray has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud ofRay has much to be proud of
 



sweet.. it works.. thanks everyone
Ray is online now   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
[4HIRE] PHP / MySQL / Javascript - Frameworks and OOP alecksmart Developers For Hire 0 06-24-2011 12:47 AM
Domainate Weekly 6/9: SeniorsGroups(.)com, MigraineRemedies(.)org + 28, < $130 each Domainate.com Domains For Sale - Fixed Price 1 06-13-2011 11:40 AM
Great Scripts for Sale With Resale Rights! Zeeble Scripts For Sale 20 01-04-2006 01:39 AM
Tutorial: How to Install Apache2 MySQL and PHP on Windows deadserious Webmaster Tutorials 35 09-21-2005 09:46 PM
Tutorial: Getting Started With MySQL (The Basics) deadserious Webmaster Tutorials 3 04-18-2004 01:17 PM

 
All times are GMT -7. The time now is 02:48 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger