Using PHP have a MySQL statement into multiple PHP variables -


a little backdrop i'm trying accomplish..

i'm making simple cms / blog , i'm trying have signature auto created database's firstname / lastname values selecting them username.. after selected trying put them 1 variable

example:

$firstname = row['firstname']; $lastname = row['lastname'];  $signature = $firstname + " " + $lastname;  echo 'created by: ' . $signature; 

the above mentally i'm trying accomplish can't seem quite there. have far, , i'm not having luck...

$username = $_session['username'];   $sqlname = "select * users username = $username";  $connname->setattribute(pdo::attr_errmode, pdo::errmode_exception);  $resultname = $connname->query($sqlname);   foreach ($resultname $row) {     $firstname = $rown['firstname'];     $lastname = $rowname['lastname']; } 

this current rendition wondering:

   $username = $_session['username']; $connname = new pdo('mysql:host=localhost;dbname=platform', 'tyler', 'h011mann');  $connname->setattribute(pdo::attr_errmode, pdo::errmode_exception);   $sqlname = "select * users username = $username";   $resultname = $connname->query($sqlname); $name = 'created : '; foreach ($resultname $row) {     $name .= $row['firstname'] . ' ' . $row['lastname']; } echo '<div>' . $name. '</div>'; 

there issue code. @ first glance, missing pdo object. on closer inspection, i've noticed using wrong concatenation operator , didn't seem use prepared statements either.

prepared statements protect sql injection users using characters might cause issues mysql database. i've written following code shoul deal issues. please make sure take @ comments inside:

<?php session_start();  //get username $username = $_session['username'];  //mysql server data $dbhost = ""; $dbname = ""; $dbuser = ""; $dbpass = "";  //pdo object $dsn = 'mysql:host=' . $dbhost . ';dbname=' . $dbname; // set pdo options $options = array(     pdo::attr_persistent    => true,     pdo::attr_errmode       => pdo::errmode_exception ); // create new pdo instance try{     $pdo = new pdo($dsn, $dbuser, $dbpass, $options); } // catch errors catch(pdoexception $e){     print $e->getmessage();     exit; }  try {     //setup query     $sql = "select * users username = :username";      //prepare query     $pdo->prepare($sql);      //bind values (to prevent sql injection)     $pdo->bindparam(':username', $username);      //execute query     $pdo->execute();      //fetch data     $data = $pdo->fetch(pdo::fetch_assoc);      //combine results     $signature = $data['firstname']. " " .$data['lastname'];     echo $signature; } catch (pdoexception $e) {     print $e->getmessage();     exit; }  ?> 

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -