php pdo 只从 mysql 获取一个值;等于变量的值
我已经定义了变量 $row_id1(例如 $row_id1 = 1;)
I have defined variable $row_id1 (for example $row_id1 = 1;)
在 mysql 中,列名为 Number.
In mysql is column called Number.
想要选择列Number,如果列中存在$row_id,想要获取(获取,定义)值.实际想检查列 Number 中是否存在 $row_id 值.
Want to select the column Number and if in the column exists $row_id, want to get (fetch, define) the value. Actually want to check if in column Number exists $row_id value.
代码有什么问题?
$stmt = $db->prepare("SELECT Number FROM 2_1_journal WHERE Number = :Number1");
$stmt->bindParam(':Number1', $row_id1); //value from $_POST
$stmt->execute();
$Number1 = $stmt->fetchAll(PDO::FETCH_ASSOC); // seems here something is wrong
echo $Number1 .' $Number1<br>';
在输出中我得到数组 $Number1
In output I get Array $Number1
好像简单(愚蠢)的问题,但搜索了几个小时却没有解决方案
As if simple (stupid) question but searching for some hours and no solution
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
更新代码
这部分与问题无关(仅供参考)
This part is not related to problem (only for information)
require($_SERVER['DOCUMENT_ROOT'] . "/_additionally_protected/session.class.php");
$session = new session();
// Set to true if using https
$session->start_session('_a', false);
session_regenerate_id();
//require($_SERVER['DOCUMENT_ROOT'].'/_additionally_protected/request_blocker.php'); //slow page load
require($_SERVER['DOCUMENT_ROOT'].'/_measure_time_start.php');
require($_SERVER['DOCUMENT_ROOT'].'/_additionally_protected/db_config.php');
header('Content-type: text/html; charset=utf8');
require($_SERVER['DOCUMENT_ROOT'] . "/only_private/blackhole.php");
ini_set('session.bug_compat_warn', 0);
ini_set('session.bug_compat_42', 0);
这与问题有关(这里我从输入中获取值)
This is related to problem (here I get values from input)
if(get_magic_quotes_gpc())
$row_id1 = htmlspecialchars(stripslashes($_POST['row_id1']));
else
$row_id1 = htmlspecialchars($_POST['row_id1']);
echo $row_id1 .' row_id1 from $_POST<br>';
连接到mysql
try {
$db = new PDO("mysql:host={$dbhost};dbname={$dbname};charset=utf8", $dbuser, $dbpass//, array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
//echo "Connected to database"; // check for connection
}
catch(PDOException $ex) {
//echo "An Error occured!"; //user friendly message
//print "Error!: " . $ex->getMessage() . "<br/>";
//some_logging_function($ex->getMessage());
exit;
}
这里我从 mysql 获取最后一行编号.后来我将数字+1 传递给 html 隐藏的 iput 字段.我的意思是在页面加载时我得到最后一个数字.如果用户点击按钮不执行代码.
Here I get last row number from mysql. Latter I pass the number+1 to html hidden iput field. I mean on page load I get last number. If user clicks on button do not execute the code.
//fetch last Number to pass to row_id and entry_id
if( !$_POST['register'] ) {
echo '!$_POST[register] <br>';
//+++++++++++++ now multiple PDO
// Select table with query
$stmt = $db->query("
SELECT Number FROM 2_1_journal ORDER BY Number DESC LIMIT 1
");
// Set fetching mode
$stmt->setFetchMode(PDO::FETCH_ASSOC);
// Assign $row as your key to access Table fields
foreach ($stmt as $row) :
echo $row['Number'] .' $row[Number] On page load (reload) get number of last row in mysql; to pass to Entry ID and rowid<br>';
$_SESSION['last_number_from_mysql'] = $row['Number'];
endforeach;
}//if( !$_POST['register'] ) {
会话(上)
这是给我带来问题的部分//从 DB 中获取 Number 以与行 ID(隐藏)进行比较.旨在决定是否需要记录新行或是否更新现有行.代码在 $_POST 上执行.所以必须定义$row_id(上面定义的)
This is the part that makes problem for me // Get Number from the DB latter to compare with row ID (hidden). Aim to decide if need to record new row or if to update existing. The code is executed on $_POST. So must define $row_id (defined above)
if( $_POST['register'] ) {
如果用户只点击按钮然后执行代码
If user click on button only then execute the code
echo $row_id1 .' row_id1 before select Number where Number is $row_id1<br>';
这是检查.我看到 row_id1 号;这意味着收到了号码.就在下面想用
This is to check. I see row_id1 number; That means that the number is received. And just below want to use it
$stmt = $db->prepare("SELECT Number FROM 2_1_journal WHERE Number = :Number1");
$stmt->bindParam(':Number1', $row_id1);
$stmt->execute();
echo $Number1 = $stmt->fetchColumn() .' $Number1<br>';
仅当点击按钮 2 次时此回显
This echo only if 2 times click on button
这里是html输入
<input type="tex" name="row_id1" id="row_id1" value="
<?php
//if( ($_POST['register']) === 'Save draft' ) {
//echo $_POST['row_id1'];
//}
//else {
echo ($_SESSION['last_number_from_mysql'] + 1);
//}
?>
">
推荐答案
你必须根据你想要的结果使用 fetch 函数.
You have to use the fetch function according to your desired result.
如果您需要单个标量值,则永远不需要列名:
If you need a single scalar value, no column name is ever needed:
$Number1 = $stmt->fetchColumn();
如果要返回的结果很多,但每行只有一列,则不需要再次输入列名:
If there are many results to be returned, but only one column per row, no need for the column name again:
$numbers = $sth->fetchAll(PDO::FETCH_COLUMN);
它将返回一个数字数组.
It will return an array of numbers.
如果你需要一行有多个值,那么使用 fetch()
If you need a single row with multiple values, then use fetch()
如果您需要行数组,请使用 fetchAll()
If you need array of rows, use fetchAll()
相关文章