/    Sign up×
Community /Pin to ProfileBookmark

loading data from a mysql database into a PHP array

Hi all,

I currently have a problem that I hope you may be able to give me some pointers on.

I shall explain what I am trying to do and how I am trying (unsuccessfully) to achieve it.

The data that I want to manipulate is say:

Table “names”

name_id | __ first

(pri key)
——————————-

1 _____ | __ dom
2 _
____ | __ john
3 _
____ | __ jo
5 _
____ | __ jason
6 _
____ | __ dave

Note: omission of 4

I am currently querying the database to find out how many names are in the database (in this case 5), then I am generating a random integer between 1 and 5 and using that integer $x to query the database a second time:

[color=red][u]query2[/u][/color][color=red]

SELECT first
FROM names
WHERE name_id=’$x’ [/color]

This gives me one result to which I assign to a variable and do stuff with.

The problem that I am having is in the random number generation

[code]$x = rand(1, $num2); [/code]

($num2 is from the first query and is the number names in the database)

As $num2 is the total number of names I can get $x = 4 (according to the code this is a valid value for $x, but when the second query comes along 4 is not a valid name_id in the table).

I can not control name_id number order as names can be deleted. Therefore
I am trying to find simple (worked) examples of loading data (eg name_id & first) from a mysql database into a PHP array and then randomly selecting the data based on a random key. I think this will give me more flexibility.

Could anyone first confirm that this is the way to go and then help me with some example code (or links to worked examples).

Thanks again,

Dom.

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 17.2004 — Here's a chunk of code I used to get data from a database into an array:
[code=php]
<?php
#...
$query = "select col1, col2 from table;";
$result = mysql_query($query);
for($i=1; $i<=mysql_num_rows($result); $i++)
{
$a = mysql_fetch_row($result);
$myArray[$a[0]] = $a[1];
}
#...
?>
[/code]
Copy linkTweet thisAlerts:
@domukauthorAug 18.2004 — Thanks Nodog,

That piece of code will be useful.

I am sure that once you have data in the array that you can sort it depending on the data. e.g. alphabetical, numeric, between, number of "names" etc.

Have you any examples of this type of manipulation from an array?

Thanks,

Dom.
Copy linkTweet thisAlerts:
@NogDogAug 18.2004 — Haven't had occaision to do any array sorting in PHP yet, but you could look at the following functions:
[list]
  • [*][url=http://us4.php.net/manual/en/function.asort.php]asort[/url]

  • [*][url=http://us4.php.net/manual/en/function.arsort.php]arsort[/url]

  • [*][url=http://us4.php.net/manual/en/function.sort.php]sort[/url]

  • [*][url=http://us4.php.net/manual/en/function.rsort.php]rsort[/url]

  • [/list]
    Copy linkTweet thisAlerts:
    @JavaHead_JonnieAug 19.2004 — I'm not too sure what you want but I saw the word 'Alphabetic'. I once needed to sort an array alphabetically and it took me an entire weekend to find this function. It has the stupidest (Well, maybe not) name ever: [URL=http://www.php.net/manual/en/function.natcasesort.php]natcasesort( array &array )[/URL].

    Hope this helps.
    Copy linkTweet thisAlerts:
    @domukauthorAug 20.2004 — thanks all for your help,

    dom..?
    Copy linkTweet thisAlerts:
    @shimonAug 20.2004 — If you're trying to find one random record, you might want to try:

    SELECT col1, col2
    FROM yourTable
    ORDER BY rand()
    LIMIT 1;


    And then you don't need to mess around with it in PHP.

    Just a thought.
    Copy linkTweet thisAlerts:
    @domukauthorAug 20.2004 — thanks..
    ×

    Success!

    Help @domuk spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...