The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP5 - Another update prepared statement not working
Discuss Another update prepared statement not working in the PHP Development forum on Dev Shed. Another update prepared statement not working PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 6th, 2012, 12:31 AM
|
 |
Contributing User
|
|
Join Date: Apr 2007
Location: Glendale AZ
|
|
|
PHP5 - 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
|

September 6th, 2012, 03:11 AM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
Quote: | 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.
|

September 6th, 2012, 10:38 AM
|
 |
Contributing User
|
|
Join Date: Apr 2007
Location: Glendale AZ
|
|
Quote: | 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...
|

September 6th, 2012, 10:42 AM
|
|
|
|
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.
|

September 6th, 2012, 11:25 AM
|
 |
Contributing User
|
|
Join Date: Apr 2007
Location: Glendale AZ
|
|
Quote: | 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)?
|

September 6th, 2012, 11:31 AM
|
|
|
PHP Code:
$update = "UPDATE donkeys SET password = ? WHERE donkey = ? LIMIT 1";
try {
$stmt = $conn->prepare($update);
}
catch (PDOException $e) {
die("Prepare error: ".$e->getMessage());
}
|

September 6th, 2012, 12:15 PM
|
 |
Contributing User
|
|
Join Date: Apr 2007
Location: Glendale AZ
|
|
Quote: | 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.
|

September 6th, 2012, 12:25 PM
|
|
|
|
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.
|

September 6th, 2012, 01:43 PM
|
 |
Contributing User
|
|
Join Date: Apr 2007
Location: Glendale AZ
|
|
Quote: | 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...
|

September 6th, 2012, 01:53 PM
|
|
|
|
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.
|

September 6th, 2012, 03:02 PM
|
|
|
I don't use mysqli, but you could try
php Code:
Original
- 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!
|

September 6th, 2012, 03:31 PM
|
 |
Contributing User
|
|
Join Date: Apr 2007
Location: Glendale AZ
|
|
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.
|

September 6th, 2012, 03:40 PM
|
|
|
|
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
|

September 6th, 2012, 04:01 PM
|
 |
Contributing User
|
|
Join Date: Apr 2007
Location: Glendale AZ
|
|
Quote: | 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` (
`donkey` int(10) unsigned NOT NULL AUTO_INCREMENT,
`last_name` varchar(48) NOT NULL,
`first_name` varchar(48) NOT NULL,
`nick_name` varchar(48) DEFAULT NULL,
`email` varchar(48) NOT NULL,
`password` varchar(65) DEFAULT NULL,
`phone` varchar(12) DEFAULT NULL,
`joined` date DEFAULT NULL,
`photo` varchar(32) DEFAULT NULL,
`approved` varchar(1) NOT NULL DEFAULT 'N',
`invite` varchar(8) NOT NULL DEFAULT 'Y',
`admin` varchar(1) NOT NULL DEFAULT 'N',
PRIMARY KEY (`donkey`),
UNIQUE KEY `email` (`email`),
KEY `name` (`last_name`,`first_name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=56 ;
|

September 6th, 2012, 04:36 PM
|
 |
JavaScript is not spelt java
|
|
Join Date: Feb 2011
Location: Landan, England
|
|
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 04:42 PM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|