Page 1 of 3 123 Last
  • Jump to page:
    #1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Location
    Glendale AZ
    Posts
    188
    Rep Power
    93

    Another update prepared statement not working


    Sorry, guys. I seem to have an endless supply of stupid problems. I've been staring at this damn thing for an hour, trying different things and I can't get it to do what it should be doing. I'm sure it's embarrassingly simple (like my last fail to fetch) but I can't see it...

    PHP Code:
    echo "Donkey: ".$donkey."<br />";
    echo 
    "Password: ".$password."<br />";

            
    $update "UPDATE donkeys SET password = ? WHERE donkey = ? LIMIT 1";
            if (
    $stmt $conn->prepare($update)) {

            echo 
    "Made it into update loop...<br />";

                
    $stmt->bind_param('si'$password$donkey);
                if (!
    $updated $stmt->execute()) {echo $stmt->error;}
                
    $stmt->close();
            }
            else {echo 
    "Broke in IF statement 88 - 92<br />";} 
    Donkey and password are echoing proper values.
    $conn is opened and used several other times in the program.
    donkeys is a valid table and password and donkey are valid columns with the correct and expected data types.

    Most of it is copied/pasted from other scripts but even comparing side by side I'm failing to an error.

    When run I get the donkey and password echoed followed by the "Broke" line echoed. It's not even entering the if ($stmt = $conn->prepare($update)) loop so I don't get a $stmt->error.

    Any ideas?

    Thanks,

    Mike
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,295
    Rep Power
    9400
    Originally Posted by big0mike
    It's not even entering the if ($stmt = $conn->prepare($update)) loop so I don't get a $stmt->error.
    Which means it couldn't prepare() that statement, right? Check for errors about that.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Location
    Glendale AZ
    Posts
    188
    Rep Power
    93
    Originally Posted by requinix
    Which means it couldn't prepare() that statement, right? Check for errors about that.
    That's my understanding. Which means there's either an error in $update or $conn. I've used $conn twice in this same script previous to this instance with no problem so it must be in $update but I have yet to see the error...

    I just manually did the update in phpMyAdmin so I'm pretty sure $update is correct and I know the values are correct and expected and appropriate for the columns they'll be placed in.

    Googling has yet to be much help... I'll keep looking...
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,600
    Rep Power
    595
    Rather than an 'if' statement use a try/catch and output the error on the exception.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Location
    Glendale AZ
    Posts
    188
    Rep Power
    93
    Originally Posted by gw1500se
    Rather than an 'if' statement use a try/catch and output the error on the exception.
    Never seen or heard of that so I'm not sure this is correct but based on the samples I saw it should work...

    PHP Code:
    $update "UPDATE donkeys SET password = ? WHERE donkey = ? LIMIT 1";
            try {
    $stmt $conn->prepare($update);}
            catch (
    Exception $e) {throw new Exception('Broke on 89'0$e);}
            die(
    'Stopped on 90'); 
    But I get no output but Stopped on 90 from the die() statement. I assume if there was an error I should get Broke on 89 followed by the error ($e)?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,600
    Rep Power
    595
    PHP Code:
      $update "UPDATE donkeys SET password = ? WHERE donkey = ? LIMIT 1";
       try {
             
    $stmt $conn->prepare($update);
       }
       catch (
    PDOException $e) {
            die(
    "Prepare error: ".$e->getMessage());
      } 
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Location
    Glendale AZ
    Posts
    188
    Rep Power
    93
    Originally Posted by gw1500se
    PHP Code:
      $update "UPDATE donkeys SET password = ? WHERE donkey = ? LIMIT 1";
       try {
             
    $stmt $conn->prepare($update);
       }
       catch (
    PDOException $e) {
            die(
    "Prepare error: ".$e->getMessage());
      } 
    Doesn't even die()... I also tried mysqli_sql_exception (found googling) since I'm not using PDO with the same results.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,600
    Rep Power
    595
    If it doesn't die then it is not throwing an exception. In other words the prepare was successful. The the problem may be in the bind. Use try/catch on that.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Location
    Glendale AZ
    Posts
    188
    Rep Power
    93
    Originally Posted by gw1500se
    If it doesn't die then it is not throwing an exception. In other words the prepare was successful. The the problem may be in the bind. Use try/catch on that.
    Hmmm... if this was my original code:
    PHP Code:
            if ($stmt $conn->prepare($update)) {

            echo 
    "Made it into update loop...<br />";

                
    $stmt->bind_param('si'$password$donkey);
                if (!
    $updated $stmt->execute()) {echo $stmt->error;}
                
    $stmt->close();
            } 
    and it never output "Made it into update loop..." then wouldn't that imply that the prepare didn't work? And thus it never gets the chance to make it to the bind_param?

    What are the chances that the prepare is not working yet not throwing an error?

    I'll give the try/catch on bind_param a shot and see what happens...
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,600
    Rep Power
    595
    One might think that but the documentation says it either succeeds or fails returning false or an exception depending on error handling. If it was returning false previously then it will throw an exception when try/catch is used. The implication is that it stopped failing when you added try/catch.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  20. #11
  21. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,555
    Rep Power
    2338
    I don't use mysqli, but you could try

    php Code:
    $stmt = $conn->prepare($update);
    if (!$stmt) 
    {
        echo "Statement preparation error: {$conn->error}<br />";
    }
    else
    {
        echo "Made it into update loop...<br />";
        if (!$stmt->bind_param('si', $password, $donkey))
        {
            echo "Bind param error: {$stmt->error}<br />";
        }
        else
        {
            $updated = $stmt->execute();
            if (!$updated)
            {
                echo "Execute error: {$stmt->error}<br />";
            }
        }
        $stmt->close();
    }
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  22. #12
  23. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Location
    Glendale AZ
    Posts
    188
    Rep Power
    93
    PHP Code:
    $stmt $conn->prepare($update);
    if (!
    $stmt
    {
        echo 
    "Statement preparation error: {$conn->error}<br />";
    }
    else

    that gave "Statement preparation error:" but no error was actually output.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,481
    Rep Power
    1752
    A very silly question, since you don't show the connect, but you ARE using mysqli?
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  26. #14
  27. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Location
    Glendale AZ
    Posts
    188
    Rep Power
    93
    Originally Posted by SimonJM
    A very silly question, since you don't show the connect, but you ARE using mysqli?
    Given some of my really dumb problems, not so silly. But, yes. mysqli is the only thing I know...
    PHP Code:
    function dbConnect($type) {

        if (
    $type == "query") {
            
    $user "...";
            
    $pwd "...";
            }
        elseif (
    $type == "admin") {
            
    $user "...";
            
    $pwd "...";
            }
        else {
            exit (
    "Unrecognized connection type");
            }
        
    $conn = new mysqli("localhost"$user$pwd"...") or die ("Cannot open database");
        return 
    $conn;
        } 
    I didn't bother posting it because it works in two different spots before this problem section as well as every other part of the site.

    And, just for giggles...
    PHP Code:
    --
    -- 
    Table structure for table `donkeys`
    --

    CREATE TABLE IF NOT EXISTS `donkeys` (
      `
    donkeyint(10unsigned NOT NULL AUTO_INCREMENT,
      `
    last_namevarchar(48NOT NULL,
      `
    first_namevarchar(48NOT NULL,
      `
    nick_namevarchar(48) DEFAULT NULL,
      `
    emailvarchar(48NOT NULL,
      `
    passwordvarchar(65) DEFAULT NULL,
      `
    phonevarchar(12) DEFAULT NULL,
      `
    joineddate DEFAULT NULL,
      `
    photovarchar(32) DEFAULT NULL,
      `
    approvedvarchar(1NOT NULL DEFAULT 'N',
      `
    invitevarchar(8NOT NULL DEFAULT 'Y',
      `
    adminvarchar(1NOT NULL DEFAULT 'N',
      
    PRIMARY KEY (`donkey`),
      
    UNIQUE KEY `email` (`email`),
      
    KEY `name` (`last_name`,`first_name`)
    ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=56 
  28. #15
  29. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    password is a reserved word in mysql so should be enclosed in back-ticks `password`.

    [Did you change it from key which is also a reserved word ]

    Luckily, though, donkey is not a reserved word
    Edit This sounds ruder than I intended..
    Last edited by AndrewSW; September 6th, 2012 at 05:42 PM.
Page 1 of 3 123 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo