Jump to content

Archived

This topic is now archived and is closed to further replies.

par15h

displaying individual recordset via php/mysql?

Recommended Posts

ok i am having a small problem, im sure its easy to over come and i still have not been able to manage it.

so i have

Recordset1 = database for composers

now i want to show the 2nd record, from the databse composers, on the page freely, without having to use tables

- Name {Recordset1.Name}
- Age {Recordset1.Age}
- Date {Recordset1.Date}
- Time {Recordset1.Time}
- Place {Recordset1.Place}

to show the first record at the moment, it is of course:

<?php echo $row_Recordset1['Name']; ?> = [b][i]"John"[/i][/b]

now say for instance i wanted to show the second record within the database on the page?

Record 1 = [b]John[/b] 53 12/01/06 13:45 London
Record 2 = Paul 65 15/12/05 14:56 Kent

i want to show "Paul" not [b][i]"John"[/i][/b]

does anyone know how to go about this? or needs anymore information? please do help!

thank you



Share this post


Link to post
Share on other sites
There are two ways depending on what you need.
If you need just the second recordset then make your query like

"SELECT .......................... LIMIT 2,1"

If your're going to need all the results then here's a function I wrote to return an entire recordset as an array.
[code]
function mysql_fetch_all($sql)
    {
        $q = mysql_query($sql);
        if (!$q)
        {
            //perform error checking
            return false;
        }
        else
        {
            $x = array();
            $i = 0;            
            while ($row = @mysql_fetch_assoc($q))
            {
               $x[$i] = $row;
               $i++;
            }
            return $x;
        }
    }
[/code]

Calling $recordset = mysql_fetch_all("SELECT * FROM ..........") returns an array with rows starting from 0.
To access row 2 column 'Name' simply use $recordset[1]['Name']. With this function you can even loop through the recordset.

Does this answer your question?
Note: If you're unsure how to use the returned array do something like:
[code]
echo "<pre>\n";
print_r($recordset);
echo "\n</pre>";
[/code]

That should show you how the array is structured.

Share this post


Link to post
Share on other sites
ah thank you, i do not need to show all the rows no, my initial thought is just to have scattered information from say row 3 on the page somewhere,... then show another row on another part of the page.. in another part of the design on the page.

im not to good with php, but with the information i have posted could you possible help me out and write the code i would need to show just row 2, and if i wish to show row 3 on another part of the page further down the design of the site.. what i would need to write?

thank you for reply and the help!

Share this post


Link to post
Share on other sites
After scouring the internet, I think that this thread holds the foundations for a solution I seek hence my bumping it.

My problem is simple, I'm calling data from a phpbb install into my design. The problem arises when I'm trying to display usernames which belong only to one certain group_id. Querying both phpbb_users and phpbb_user_group WHERE group_id = 107 seems to bring up many duplicate results.

In search of a solution I created two recordsets.
1 : queries phpbb_user_group WHERE group_id = 107.
This provides the correct amount of results alongside the user_id's.
2 : queries phpbb_users for user_id and username

Now what I want to do is display data from phpbb_users where user_id's are the same as the 15 or so displayed on the first recordset.

For the record : I've had no success implementing the array function above, though that could be because I am an absolute php novice and just don't know how to go about using it. The same excuse applies as to why I'm either missing the obvious, or making no sense.

If anyone's able to help or requires more information just ask.

Thanks.

-Matt.

Share this post


Link to post
Share on other sites
At 5am I found the solution.

The solution (as always) was simpler than I had expected. I spent a few hours reading up on SQL SELECT's and found that I just needed to be more specific with the query so as to avoid anonymous data problems impeding properly filtering the results. For future reference, here's my new query.

[quote]
SELECT
`phpbb_user_group`.`group_id`,
`phpbb_user_group`.`user_id`,
`phpbb_users`.`user_id`,
`phpbb_users`.`username`
FROM
`phpbb_user_group` ,
`phpbb_users`
WHERE
'107' =  `phpbb_user_group`.`group_id` AND
`phpbb_users`.`user_id` =  `phpbb_user_group`.`user_id`
ORDER BY
`phpbb_users`.`user_id` ASC
[/quote]

Night guys.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.