Jump to content

Need help with SELECT WHERE statement


JoeBrenan
Go to solution Solved by benanamen,

Recommended Posts

  • Solution

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';
    }

?>

Edited by benanamen
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!

Edited by JoeBrenan
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.
Edited by benanamen
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.