2

A little backdrop to what I'm trying to accomplish..

I'm making a simple CMS / blog and I'm trying to have the signature auto created from the database's firstname / lastname values by selecting them by the username.. Then after they are selected I am trying to put them into one variable

Example:

$firstname = row['firstname'];
$lastname = row['lastname'];

$signature = $firstname + " " + $lastname; 
echo 'Created by: ' . $signature;

The above is what mentally I'm trying to accomplish but I just can't seem to get quite there. This is what I have so far, and I'm not having any luck...

$username = $_SESSION['username'];

 $sqlName = "SELECT * FROM users WHERE username = $username";
 $connName->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$resultName = $connName->query($sqlName);

 foreach ($resultName as $row) {
    $firstname = $rowN['firstname'];
    $lastname = $rowName['lastname'];
}

This is my most current rendition for those wondering:

   $username = $_SESSION['username'];
$connName = new PDO('mysql:host=localhost;dbname=platform', 'tyler', 'H011mann');
 $connName->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

 $sqlName = "SELECT * FROM users WHERE username = $username";


$resultName = $connName->query($sqlName);
$name = 'Created by : ';
foreach ($resultName as $row) {
    $name .= $row['firstname'] . ' ' . $row['lastname'];
}
echo '<div>' . $name. '</div>';
4
  • 2
    I'm missing the part where you create your pdo object? Commented Jun 4, 2015 at 0:26
  • @icecub Now i see where I messed up. I was using a global db file and it was using a different name for the pdo. Thanks. Commented Jun 4, 2015 at 0:38
  • No problem. I'm already working on a better answer for you because you still have some other issues in there ;) Commented Jun 4, 2015 at 0:40
  • @icecub I have updated my code in the question, still running in to problems, it is not posting now.. Although the username / pass are correct.. :| Commented Jun 4, 2015 at 0:48

4 Answers 4

2

There are some issue with your code. At first glance, I was missing the PDO object. On closer inspection, I've noticed you were using the wrong concatenation operator and you didn't seem to use Prepared Statements either.

Prepared Statements will protect you from SQL injection as well as users using characters that might cause issues for your MySQL database. I've written the following code for you that shoul deal with all your issues. Please make sure to take a look at the 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 a new PDO instance
try{
    $pdo = new PDO($dsn, $dbuser, $dbpass, $options);
}
// Catch any errors
catch(PDOException $e){
    print $e->getMessage();
    exit;
}

try {
    //Setup Query
    $sql = "SELECT * FROM users WHERE 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;
}

?>
Sign up to request clarification or add additional context in comments.

9 Comments

Getting a blank page now.. :( Filled out the user,dbname etc
@TylerH Ofcourse you do :P It's simply setting $signature. It's not doing anything with it yet. If you wish to see the result, add echo $signature; underneath it. Wait, I'll update the code.
@TylerH There, try it now.
I did. I Declared a variable at the top and tested echoing it in each section of the code and it kept displaying until i reached the try statement try{ $pdo = new PDO($dsn, $dbuser, $dbpass, $options); }
You get my +1 for showing correct practice with PDO by using prepared statements. Good answer!
|
0

Try this

$resultName = $connName->query($sqlName);
$signature = 'Created by : ';
foreach ($resultName as $row) {
    $signature .= $row['firstname'] . ' ' . $row['lastname'];
}
echo $signature;

3 Comments

That's not gonna work if he doesn't even create a pdo object in the first place.. All he does is set some pdo attributes and expect it to magicly be an actual object.
True, but that seems not to be his problem since he didn't show nothing about it. He's just asking about concatenating strings. But yeah, you're right.
@EricMartinez A little of both, I'm not sure that it's concatenating that is my primary problem as it is not even selecting the first / last name into the variable.. I have my updated code posted above, Thanks! :)
0
$username = $_SESSION['username'];

$sqlName = "SELECT * FROM users WHERE username = $username";
$connName->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$resultName = $connName->query($sqlName);

foreach ($resultName as $row) {
$firstname = $row['firstname'];
$lastname = $row['lastname'];
}
$signature=$firstname.' '.$lastname;

Comments

0

Unless your sample code up there was just some form of pseudo code, the concatenation operator in PHP is ".", not "+". Just use that to combine the 2 values returned into a variable:

$firstname = $row['firstname'];
$lastname = $row['lastname'];
$signature = $firstname . ' ' . $lastname;

1 Comment

It was just a form of pseudo, I'm having an issue grabbing the files from the db and inserting the values into variables firstname and lastname, Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.