Results 1 to 10 of 10

Thread: [SOLVED] MySQL query works, but not from php...help?

  1. #1
    Join Date
    Oct 2005
    Location
    Gainesville, FL
    Beans
    Hidden!
    Distro
    Kubuntu 9.10 Karmic Koala

    [SOLVED] MySQL query works, but not from php...help?

    from php it looks like this:
    $query = "UPDATE Flight F SET F.seats = F.seats-$rseats WHERE (F.fid = $fid)";
    $result = mysql_query($query);

    I tested it from the mysql command prompt first though like so:
    UPDATE Flight F SET F.seats = F.seats-1 WHERE (F.fid = 1);

    The bottom one works, but it won't from php and it's driving me crazy! I'm not receiving an error message either!

    Thanks,
    Griff

  2. #2
    Join Date
    Nov 2006
    Location
    40.31996,-80.607213
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: MySQL query works, but not from php...help?

    For one, that type of query should have no usable result (besides true or false).
    Try this:
    PHP Code:
    $query "UPDATE Flight F SET F.seats = F.seats-$rseats WHERE (F.fid = $fid)";
    mysql_query($query) or die(mysql_error()); 
    "Security lies within the user of who runs the system. Think smart, live safe." - Dr Small
    Linux User #441960 | Wiki: DrSmall

  3. #3
    Join Date
    Oct 2005
    Location
    Gainesville, FL
    Beans
    Hidden!
    Distro
    Kubuntu 9.10 Karmic Koala

    Re: MySQL query works, but not from php...help?

    Quote Originally Posted by Dr Small View Post
    For one, that type of query should have no usable result (besides true or false).

    Try this:
    PHP Code:
    $query "UPDATE Flight F SET F.seats = F.seats-$rseats WHERE (F.fid = $fid)";
    mysql_query($query) or die(mysql_error()); 
    I was looking for a change in the database itself.

    mysql_error() doesn't give me anything for this query.

    Thanks,
    Griff

  4. #4
    Join Date
    Nov 2006
    Location
    40.31996,-80.607213
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: MySQL query works, but not from php...help?

    Quote Originally Posted by Griff View Post
    I was looking for a change in the database itself.

    mysql_error() doesn't give me anything for this query.

    Thanks,
    Griff
    Oh, then in that case, you would probably want to use:
    PHP Code:
    $query "UPDATE Flight F SET F.seats = F.seats-$rseats WHERE (F.fid = $fid)";
    $result mysql_query($query);

    if(
    $result){
        echo 
    'The database has been updated. Preform whatever you choose in this area.';
    } else {
        echo 
    'There was nothing to update in the database.';

    "Security lies within the user of who runs the system. Think smart, live safe." - Dr Small
    Linux User #441960 | Wiki: DrSmall

  5. #5
    Join Date
    Oct 2005
    Location
    Gainesville, FL
    Beans
    Hidden!
    Distro
    Kubuntu 9.10 Karmic Koala

    Re: MySQL query works, but not from php...help?

    $query = "UPDATE Flight F SET F.seats = F.seats-1 WHERE (F.fid = 3)";
    $result = mysql_query($query);

    if($result){
    echo 'The database has been updated. Preform whatever you choose in this area.';
    } else {
    echo 'There was nothing to update in the database.';
    }

    I changed my variable to constants above, and no matter what I get the 'There was nothing to update in the database.' response. Why would that query not work? If I copy and paste than into the mysql cli it works fine.

  6. #6
    Join Date
    Oct 2005
    Location
    Gainesville, FL
    Beans
    Hidden!
    Distro
    Kubuntu 9.10 Karmic Koala

    Re: MySQL query works, but not from php...help?

    This is what I'm trying to do. The user is reserving so many seats for a given flight. I'm wanting to subtract this number from the number of total seats on the plane. I.E. $total_seats = $total_seats - $reserved_seats. Maybe there's another way to do this? I really cannot see why this works on the mysql cli and not under php.

    Thanks,
    Griff

  7. #7

    Re: MySQL query works, but not from php...help?

    You are actually connecting to your Database aren't you?
    If not use something like this:
    PHP Code:
    $conn mysql_connect("localhost""username""password");
    // Select the database
    $db mysql_select_db("database");
    // Query the table
    $query 'SELECT * from tableName';
    $result mysql_query($query) or die(mysql_error()); 
    Hope this helps

  8. #8
    Join Date
    Dec 2007
    Beans
    75

    Re: MySQL query works, but not from php...help?

    try:
    PHP Code:
    $query "UPDATE `Flight` AS F SET F.`seats` = F.`seats` - " $rseats " WHERE F.`fid` = " $fid;
    echo(
    $query); //just to see how it comes out on the other side 
    Note all the ``s ... thats ` not '

  9. #9
    Join Date
    Oct 2005
    Location
    Gainesville, FL
    Beans
    Hidden!
    Distro
    Kubuntu 9.10 Karmic Koala

    Re: MySQL query works, but not from php...help?

    Quote Originally Posted by Ash R View Post
    You are actually connecting to your Database aren't you?
    If not use something like this:
    PHP Code:
    $conn mysql_connect("localhost""username""password");
    // Select the database
    $db mysql_select_db("database");
    // Query the table
    $query 'SELECT * from tableName';
    $result mysql_query($query) or die(mysql_error()); 
    Hope this helps
    Yep, I have a query above this one that does execute.

  10. #10
    Join Date
    Oct 2005
    Location
    Gainesville, FL
    Beans
    Hidden!
    Distro
    Kubuntu 9.10 Karmic Koala

    Re: MySQL query works, but not from php...help?

    OK. Solved. Everyone may now yell at for not applying Occam's Razor to this problem. The user I was testing the webpage under did not have permission to access or update the Flight table. This is somewhat troubling though, because according to the php wiki:

    mysql_query() will also fail and return FALSE if the user does not have permission to access the table(s) referenced by the query.

    Which it didn't or at least didn't pass anything to mysql_error().

    Thanks all for the help. I'll be applying 'thanks' for all your time spent trying to be helpful.

    Thank you,
    Griff

Tags for this Thread

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
  •