Jump to content

Need help with SELECT WHERE statement


JoeBrenan

Recommended Posts

Ok, we are getting somewhere.

 

Now, just use this code alone while you get things going. The difference is I have now added what is called a "Positional Placeholder" for username which is the question mark. There is another way to do it called "Named Parameters" but we can get into that another time. The code will now give you the individual column data.

 

This is complete standalone with the connection code. Just practice with this. you can integrate it later. Set the connection parameters to your DB.

 

 

<?php
error_reporting
(-1);
ini_set('display_errors''1');

$hostdb   'localhost';
$dbname   'phphelp_joe';
$username 'root';
$password '';

if (!empty(
$_GET['username']))
    {  
    
$pdo = new PDO("mysql:host=localhost;dbname=$dbname"$username$password);
    
$pdo->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
    
    
$sql  "SELECT username, firstname, lastname, email FROM users WHERE username=?";
    
$stmt $pdo->prepare($sql);
    
$stmt->execute(array(
        
$_GET['username']
    ));
    
$row $stmt->fetch();
    echo 
"{$row['username']} {$row['lastname']} {$row['email']}";
    }
else
    {
    echo 
'No username in URL';
    }

?>

Link to comment
Share on other sites

Yeah this works! :D

 

It pulls in the username from the URL then displays the data relating to the username from the database on the page, and have implemented the code into the view_profile.php.

 

Thanks a lot, i know it was hard work but i learned a lot :)

 

Now to create a friends system :P

 

Happy Halloween!

Link to comment
Share on other sites

Glad your learning.

 

When I asked you for an SQL data dump this is what I was asking for:

 

INSERT INTO `users` VALUES ('1', 'test', 'test', 'test', '5c73b9801c80c790e4c9b5bf0f55cdf84bea07baa3af1d778845427339d71e12', '4f0819657e64c9ed', ' test@test.com', '1', '2015-10-31', '1');
 
The reason I wanted your table AND data was so I could quickly and easily recreate your database on my server for testing and to make sure your tables are setup correctly.
 
What I gave you is very basic code. It does not account for a nonexistent username among other things.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.