title: php pdo get only one value from mysql; value that equals to variable
body: <p>I have defined variable $row_id1 (for example $row_id1 = 1;)</p>
  
  <p>In mysql is column called Number.</p>
  
  <p>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.</p>
  
  <p>What is wrong with the code?</p>
  
  <pre><code>$stmt = $db-&gt;prepare("SELECT Number FROM 2_1_journal WHERE Number = :Number1");
  $stmt-&gt;bindParam(':Number1', $row_id1); //value from $_POST
  $stmt-&gt;execute();
  $Number1 = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC); // seems here something is wrong
  
  echo $Number1 .' $Number1&lt;br&gt;';
  </code></pre>
  
  <p>In output I get Array $Number1</p>
  
  <p>As if simple (stupid) question but searching for some hours and no solution</p>
  
  <p>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++</p>
  
  <p><strong>Updated code</strong></p>
  
  <p>This part is not related to problem (only for information)</p>
  
  <pre><code>require($_SERVER['DOCUMENT_ROOT'] . "/_additionally_protected/session.class.php"); 
  $session = new session();
  // Set to true if using https
  $session-&gt;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);
  </code></pre>
  
  <p>This is related to problem (here I get values from input)</p>
  
  <pre><code>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&lt;br&gt;';
  </code></pre>
  
  <p>Connect to mysql</p>
  
  <pre><code>try {
  $db = new PDO("mysql:host={$dbhost};dbname={$dbname};charset=utf8", $dbuser, $dbpass//, array(PDO::ATTR_EMULATE_PREPARES =&gt; false, PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION));
  , array(PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION));
  //echo "Connected to database"; // check for connection
  }
  catch(PDOException $ex) {
  //echo "An Error occured!"; //user friendly message
  //print "Error!: " . $ex-&gt;getMessage() . "&lt;br/&gt;";
  //some_logging_function($ex-&gt;getMessage());
  exit;
  }
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>//fetch last Number to pass to row_id and entry_id
  if( !$_POST['register'] ) {
  echo '!$_POST[register] &lt;br&gt;';
  //+++++++++++++ now multiple PDO
  
  // Select table with query
  $stmt = $db-&gt;query("
  SELECT Number FROM 2_1_journal ORDER BY Number DESC LIMIT 1
  ");
  // Set fetching mode
  $stmt-&gt;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&lt;br&gt;';
  $_SESSION['last_number_from_mysql'] = $row['Number'];
  endforeach;
  }//if( !$_POST['register'] ) {
  </code></pre>
  
  <p><strong>Session (above)</strong></p>
  
  <p><strong>This is the part that makes problem for me</strong>
  // 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)</p>
  
  <pre><code>if( $_POST['register'] ) {
  </code></pre>
  
  <p>If user click on button only then execute the code</p>
  
  <pre><code>echo $row_id1 .' row_id1 before select Number where Number is $row_id1&lt;br&gt;';
  </code></pre>
  
  <p>This is to check. I see row_id1 number; That means that the number is received. And just below want to use it</p>
  
  <pre><code>$stmt = $db-&gt;prepare("SELECT Number FROM 2_1_journal WHERE Number = :Number1"); 
  $stmt-&gt;bindParam(':Number1', $row_id1);
  $stmt-&gt;execute();
  echo $Number1 = $stmt-&gt;fetchColumn() .' $Number1&lt;br&gt;';
  </code></pre>
  
  <p>This echo only if 2 times click on button</p>
  
  <p>Here is html input</p>
  
  <pre><code>&lt;input type="tex" name="row_id1" id="row_id1" value="
  &lt;?php
  //if( ($_POST['register']) === 'Save draft' ) {
  //echo $_POST['row_id1'];
  //}
  //else {
  echo ($_SESSION['last_number_from_mysql'] + 1);
  //}
  ?&gt;
  "&gt;
  </code></pre>