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

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

Posted by: admin July 12, 2020 Leave a comment


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


//require($_SERVER['DOCUMENT_ROOT'].'/_additionally_protected/request_blocker.php');  //slow page load


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)

$row_id1 = htmlspecialchars(stripslashes($_POST['row_id1']));
$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));
//echo "Connected to database"; // check for connection
catch(PDOException $ex) {
//echo "An Error occured!"; //user friendly message
//print "Error!: " . $ex->getMessage() . "<br/>";

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
// 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'];
}//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);
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="
//if( ($_POST['register']) === 'Save draft' ) {
//echo $_POST['row_id1'];
//else {
echo ($_SESSION['last_number_from_mysql'] + 1);
How to&Answers:

You need to use fetch function according to your desired result.

  1. If you need single result, no column name ever needed:

    echo $Number1 = $stmt->fetchColumn();
  2. If there are many results to be returned, no need for the column name again:

    $numbers = $sth->fetchAll(PDO::FETCH_COLUMN);

It will return an array of numbers.

  1. If you need a row, use fetch()
  2. If you need array of rows, use fetchAll()



echo $Number1['Number'] .'<br>';


first change

$Number1 = $stmt->fetchAll(PDO::FETCH_ASSOC);


$Number1 = $stmt->fetch(PDO::FETCH_ASSOC);

since I believe you are retrieving only 1 row..

you will get ur number as

echo $Number1['Number']