Hey guys this is a really noob question but for some reason I can't seem to get a single value from a database.
Here is the code that I'm using:
$stmt = $pdo->prepare("SELECT column FROM teacher WHERE id = :id")
$stmt->bindParam(':id', $id);
$stmt->execute();
$oldValue = $stmt->fetchColumn();
I do filter the variables before in the code because I got them in this file as post data, here's the code for that part:
$column = filter_input(INPUT_POST, "column", FILTER_SANITIZE_STRING);
$id = filter_input(INPUT_POST, "id", FILTER_SANITIZE_STRING);
$value = filter_input(INPUT_POST, "value", FILTER_SANITIZE_STRING);
In this same file updating the database works so its probably not a problem with connecting to the database. Please help! Thanks
Full error from $stmt:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'column FROM teacher WHERE id = ?' at line 1' in /var/www/duties/testTableDataUpload.php:25Stack trace:#0 /var/www/duties/testTableDataUpload.php(25): PDO->prepare('SELECT column F...')#1 {main} thrown in /var/www/duties/testTableDataUpload.php on line 25
I know this is not an answer, just trying to post code to OP.
jquery:
var data="id="+id;
$.ajax({
type:"POST",
data: data,
url:"somePHPdbPage.php",
success: function(result){
$('#blah').html(result);
}
});
Then somewhere on your main page do:
<div id='blah'></div>
what this will do is add the result to the div blah. and you should plainly see it on your main page. then c/p all you want. Have to head out for a bit. will check back in.
and on your php page:
$stmt = $pdo->prepare("SELECT column FROM teacher WHERE id = :id")
$stmt->bindParam(':id', $id);
$stmt->execute();
print_r($stmt->errorInfo());
column
is a reserved word in MySQL and must be escaped using '`'. You are also missing a semicolon at the end of the line. Try the following:
$stmt = $pdo->prepare("SELECT `column` FROM teacher WHERE id = :id");
$stmt->bindParam(':id', $id);
$stmt->execute();
$oldValue = $stmt->fetch();
maybe the problem be with your data but if there be duplicated data you can use one of the belows:
$stmt = $pdo->prepare("SELECT distinct column FROM teacher WHERE id = :id")
or
$stmt = $pdo->prepare("SELECT column FROM teacher WHERE id = :id limit 1")
If you have a column in the teach tabled named 'first_name' you should be able to do the following
$stmt = $pdo->prepare("SELECT first_name FROM teacher WHERE id = :id ORDER BY id DESC limit 1")
$stmt->bindParam(':id', $id);
$stmt->execute();
$row = $stmt->fetch(PDO:FETCH_ASSOC);
echo $row['first_name']; //Will print out the first name (if that is a column in your table of course)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.