php pdo 只从 mysql 获取一个值;等于变量的值

php pdo get only one value from mysql; value that equals to variable(php pdo 只从 mysql 获取一个值;等于变量的值)
本文介绍了php pdo 只从 mysql 获取一个值;等于变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我已经定义了变量 $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'];
/
                
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死锁异常代码?)
PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滚动游标不起作用)
PHP PDO ODBC connection(PHP PDO ODBC 连接)
Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔术方法)
MSSQL PDO could not find driver(MSSQL PDO 找不到驱动程序)
PDO connection works from command line, but not through Apache?(PDO 连接从命令行工作,而不是通过 Apache?)