SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: Code Error

  1. #1
    SitePoint Zealot
    Join Date
    Dec 2003
    Location
    Ireland
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Code Error

    Hi everyone, Can anyone tell me what's wrong with this code, I keep getting this error:

    Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\Web\test2.php on line 17

    The connection is coming back as successful and I've double-checked the table and field exists.

    PHP Code:
    <?php  


    $dbhost
    ='localhost';
    $dbusername='myname';
    $dbuserpass='mypassword';
    $dbname='tenders';

    $link_id mysql_connect($dbhost$dbusername$dbuserpass);
    echo 
    "success in database connection.";

    $query "Select county FROM counties";

    $result mysql_query($query,$link_id);

    while(
    $row mysql_fetch_row($result)) {
    $counties $row['county'];
    echo 
    $counties;
    }

    ?>

  2. #2
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it seems your query is not being executed. and you are trying to use the $result which is not actually a resultset because your query din't execute.
    use die like

    $result = mysql_query($query,$link_id) or die(mysql_error());

    it'lltell you why your query did not execute properly
    ---------------------------
    Errors = Improved Programming.
    My Site

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2003
    Location
    Ireland
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's odd, the mysql_error is 'No Database Selected', but the info I've supplied is correct.

  4. #4
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i dont see you using anywherre mysql_select_db()
    ---------------------------
    Errors = Improved Programming.
    My Site

  5. #5
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    Southwest US
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you need to add:

    mysql_select_db ('database_name' , $link_id);

    before sending your query

  6. #6
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Itshim
    you need to add:

    mysql_select_db ('database_name' , $link_id);

    before sending your query
    NO OFFENSE, but i think you meant mysql_select_db ('database_name' [,$link_id]);

    square bracket mean, optional
    ---------------------------
    Errors = Improved Programming.
    My Site

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2003
    Location
    Ireland
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, the 'no database selected' error is gone but I'm not seeing any counties being printed out??.

    PHP Code:
    <?php  


    $dbhost
    ='localhost';
    $dbusername='myname';
    $dbuserpass='mypassword';
    $dbname='tenders';

    $link_id mysql_connect($dbhost$dbusername$dbuserpass);

    mysql_select_db('tenders',$link_id);

    $query "Select county FROM counties";

    $result mysql_query($query,$link_id) or die(mysql_error());

    while(
    $row mysql_fetch_row($result)) {

    $counties $row['county'];
    print 
    $counties;

    }

    ?>
    Thanks for your help on this.

  8. #8
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    Southwest US
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jaswinder_rana
    NO OFFENSE, but i think you meant mysql_select_db ('database_name' [,$link_id]);
    You are absolutely correct, I just have an personal issue with not using the link identifier.

    I know that with a select statement it really isn't needed, but I feel it is a bad idea to get into the habit of not using it, because with other mysql functions, you should definitely use it even though the manual says it is optional.

    Edit:

    misspelling

  9. #9
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i dont think you need to use it, unless, you are using 2 databases in the same script and you want to seperate the queries for them.

    But again, it sometimesdepend on the style of the programmer, what to use and what not.
    ---------------------------
    Errors = Improved Programming.
    My Site

  10. #10
    SitePoint Zealot
    Join Date
    Dec 2003
    Location
    Ireland
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, it's woking now, I changed mysql_fetch_row into mysql_fetch_array, can anyone tell me why mysql_fetch_row didn't work?

  11. #11
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    Southwest US
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Meltdown:
    mysql_fetch_row: returns an enumerated array not an associative array.
    mysql_fetch array: returns both an enumerated array and an associative array.

    jaswinder_rana:
    I also use the link identifier for mysql_insert_id(), becuase I don't just want the last insert id, but the insert id associated with a specific link identifier.

  12. #12
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Itshim
    jaswinder_rana:
    I also use the link identifier for mysql_insert_id(), becuase I don't just want the last insert id, but the insert id associated with a specific link identifier.
    Don't wanna start a debate or something, but i said before, its a personal thing and also necessary when you usee multiple databases on one page.
    ---------------------------
    Errors = Improved Programming.
    My Site


Bookmarks

Posting Permissions

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