Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jun 2012
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile randomly seeded array

    Attempting to load an array based on a random set of numbers generated through shuffle().
    However, this script outputs "Array" and nothing else.
    What am I missing?



    PHP Code:
    $play range(1100);
    shuffle($play);

    $year=$_GET["year"];
    if (empty(
    $year)) {$year=1960;}
    $year=(int)$year;

    $number="2";
    $number=(int)$number;

    $result mysql_query("SELECT atitle FROM A$year WHERE id = $number");
    if (!
    $result) { echo 'Could not run query: ' mysql_error();  exit; }
    $vid mysql_fetch_row($result);

    echo 
    $vid

  • #2
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,304
    Thanks
    4
    Thanked 204 Times in 201 Posts
    Use print_r($arrayname) for printing arrays.

    PHP Code:
    print_r($vid); 
    Dave .... HostMonster for all of your hosting needs

  • #3
    Regular Coder
    Join Date
    Jun 2012
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks. But that shows $vid as an array.
    Array([0]=>"value")
    I want to see "value".

  • #4
    Regular Coder
    Join Date
    Jun 2012
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Got it worked out I hope.

    PHP Code:


    $play 
    range(1100);
    shuffle($play);

    $year=$_GET["year"];
    if (empty(
    $year)) {$year=1960;}
    $year=(int)$year;
    echo 
    "<p>".$year."</p>";

    $numbers="0";

    while (
    $numbers<"100"
    {
    $number=$play[$numbers];
    $number=(int)$number;

    $result mysql_query("SELECT atitle,artist FROM A$year WHERE id = $number");
    if (!
    $result) { echo 'Could not run query: ' mysql_error();  exit; }
    $vid mysql_fetch_row($result);
    $avid=$vid[0]." - ".$vid[1];

    echo 
    $avid."<br>";

    $numbers++;


  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    With the mysql library, fetch_* is the only way to pull data out (well, and mysql_result as well, but that's slow as sludge compared to *every* other type). You're options are array, row, assoc, and object for the fetch types. Using mysql_fetch_row (or mysql_fetch_array with MYSQL_NUM or MYSQL_BOTH) will give you a numerically indexed array for your records. The offsets correspond to the order of the selected fields. Assoc (or array with MYSQL_ASSOC) can also be used to give the field => value map.
    However, you should look towards the use of mysqli since mysql library is now deprecated and will be removed. Since this has variable input data, than prepared statements would be warranted in which case you would use a bind_result on a statement to populate variables on request (technically you can also pull the mysqli_result from a prepared statement call, but that's just more work).

    This has me mighty concerned though:
    Code:
    SELECT atitle,artist FROM A$year WHERE id = $number
                               ^^^^^
    This combined with your previous population of $year indicates you have replicated databases with *different* names and the same structure. That would be a management burden; you should normalize the recordsets and have a single table that tracks its year.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •