简体   繁体   English

php mysql从数据库检索单个值

[英]php mysql retrieving single value from database

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: 来自$ stmt的完整错误:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; 致命错误:消息为“ SQLSTATE [42000]”的未捕获的异常“ PDOException”:语法错误或访问冲突:1064 SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'column FROM teacher WHERE id = ?' 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在“来自教师的列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 在/var/www/duties/testTableDataUpload.php:25堆栈中的第1行:#0 /var/www/duties/testTableDataUpload.php(25):PDO-> prepare('SELECT column F ...')#在第25行的/var/www/duties/testTableDataUpload.php中抛出1个{main}

I know this is not an answer, just trying to post code to OP. 我知道这不是答案,只是尝试将代码发布到OP。

jquery: 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. 这样做是将结果添加到div blah中。 and you should plainly see it on your main page. 并且您应该在主页上清楚地看到它。 then c/p all you want. 然后选择c / p。 Have to head out for a bit. 必须走了一点。 will check back in. 将重新签入。

and on your php page: 并在您的php页面上:

$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 '`'. column是MySQL中的保留字 ,必须使用'`'进行转义。 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 如果您在示教表格中有一列名为“ first_name”的列,则应该能够执行以下操作

$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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM