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 4 of 4

Thread: Mysql query

  1. #1
    Regular Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    324
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Mysql query

    Hi

    Can somebody tell me why this wont work. It is supposed to fecth the highest value in the field Item_id.

    It wont work though. It just returns 'no' which I put there to see if it wasn't retreiving anything ??

    Thanks

    PHP Code:
    function display_add_item_form() {
    $conn db_connect(); //connect to database
    $query mysql_query("SELECT Item_id FROM products ORDER BY Item_id DESC LIMIT 1");
    $result mysql_query($query);
    if (!
    $result)
        echo 
    "no";
    $newid $result;
    print (
    "$newid");


  • #2
    New Coder
    Join Date
    Nov 2002
    Location
    Netherlands
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you're calling mysql_query two times, you should not do that. Instead you should directly assign the return value of the first mysql_query call to $result like so:

    PHP Code:
    $result_=_mysql_query("SELECT Item_id FROM products ORDER BY Item_id DESC LIMIT 1"); 
    Though you will have to make another call to fetch the actual data from the result. with one of the mysql_fetch_* calls, take a look at the php manual here

    btw, do try to use a more descriptive subject line next time, Thank you.

  • #3
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Yep,

    basically change your $resutt line to
    $result = mysql_fetch_assoc($query);

    then you can access the fileds this way
    $newid = $result['Item_Id'];

  • #4
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    No need to get all fancy:


    [php]

    function display_add_item_form() {
    $conn = db_connect(); //connect to database
    $query = "SELECT Item_id FROM products ORDER BY Item_id DESC LIMIT 1";
    $result = mysql_query($query);
    if (!$result)
    echo "no";
    $newid = $result;
    print ("$newid");
    }
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster


  •  

    Posting Permissions

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