Assigning arrays help

Discussion in 'PHP' started by gkz, Jan 26, 2008.

  1. #1
    Hi All

    I keep getting stuck with array errors on the following, any ideas?

    Ta
    gkz

    <html>
    <head>
    </head>
    <body>
    <?php

    // Open a connection to the database
    $link = mysql_connect ("localhost", "xxxxx", "xxxxx") or die ('I cannot connect to the database because: ' . mysql_error());
    mysql_select_db ("xxxxx");

    // Define a query that retrieves each field
    $query = "SELECT sForename, sSurname, sCountry FROM tblusers WHERE (nUser_ID = '2')";

    // Run the query and store the result
    $result = mysql_query($link, $query);

    // Assign each record in the result to an array
    while ($row = mysql_fetch_array($result))
    {

    // Assign each array element to a variable
    $sForename = $row['sForename'];
    $sSurname = $row['sSurname'];
    $sCountry = $row['sCountry'];

    // Print results
    echo $sForename;
    echo $sSurname;
    echo $sCountry;
    }
    ?>
    </body>
    </html>
     
    gkz, Jan 26, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Err.. can you be way more specific? What errors? What's wrong?
     
    nico_swd, Jan 26, 2008 IP
  3. gkz

    gkz Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Sorry - error message follow -

    Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/xxxxx/public_html/authors/index.php on line 15

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/xxxxx/public_html/authors/index.php on line 18

    Ta
    gkz
     
    gkz, Jan 26, 2008 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    The order of the parameters in mysql_query() is wrong.

    
    $result = mysql_query($query, $link);
    
    PHP:
    The link identifier should be the second, like above. (The identifier is optional though, you only need it if you have multiple connections)
     
    nico_swd, Jan 26, 2008 IP
  5. gkz

    gkz Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    OK, thanks, I'll try that.

    Ta
    gkz
     
    gkz, Jan 26, 2008 IP
  6. gkz

    gkz Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Cool, that worked (why is it always something so simple yet you never see it til it's pointed out - LOL)!!

    Thanks
    gkz
     
    gkz, Jan 26, 2008 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    Lol, that usually happens to me when I'm really tired.
     
    nico_swd, Jan 26, 2008 IP
  8. gkz

    gkz Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    LOL - tired? What's that (as my eyeballs rebound of the keyboard and pop back into their sockets . . .)


    Another one-

    with WHERE (nUser_ID = '2') is it possible to substitute the '2' with part of the referring URL? I have no idea if this can be done.

    e.g. if a member is handing out their 'member page' URL and the '2' is their member number, can you grab their profile by referring to the '2' on the end of the URL?

    I'll probably have to start a new thread with the correct subject line.

    Ta
    gkz
     
    gkz, Jan 26, 2008 IP
  9. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #9
    You can get values from the URL using the $_GET variable.

    For example for the ID, if the URL was file.php?id=2

    Then in PHP, you'd do this to get it:
    
    $id = $_GET['id'];
    
    PHP:
    And to combine that with your query:
    
    $query = sprintf("
        SELECT sForename, sSurname, sCountry
        FROM tblusers
        WHERE nUser_ID = %d
        LIMIT 1"
    , intval($_GET['id']));
    
    PHP:
     
    nico_swd, Jan 26, 2008 IP
  10. gkz

    gkz Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Ahh (slaps head), sprintf returns a formatted string, then I can get the integer.

    That should do it, I'll give it a go and see what happens.

    Thanks again
    gkz
     
    gkz, Jan 26, 2008 IP
  11. gkz

    gkz Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    WOW, that worked to (am currently dancing on the ceiling, but don't look up, I'm not wearing any knickers - LOL).

    Thanks again
    gkz
     
    gkz, Jan 26, 2008 IP
  12. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #12
    Hehe, glad I could help.
     
    nico_swd, Jan 26, 2008 IP