Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

php pdo get only one value from mysql; value that equals to variable

I have defined variable $row_id1 (for example $row_id1 = 1;)

In mysql is column called Number.

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.

What is wrong with the code?

$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>';

In output I get Array $Number1

As if simple (stupid) question but searching for some hours and no solution

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Updated code

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>';

Connect to 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;
}

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'] ) {

Session (above)

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>';

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>';

This echo only if 2 times click on button

Here is html input

<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);
//}
?>
">
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to use the fetch function according to your desired result.

  1. If you need a single scalar value, no column name is ever needed:

     $Number1 = $stmt->fetchColumn();
    
  2. 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.

  3. If you need a single row with multiple values, then use fetch()

  4. If you need array of rows, use fetchAll()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...