Jeff13246 Posted August 29, 2019 Share Posted August 29, 2019 Completely new to PHP here, so on my website I'd like a specific image to appear according to whether or not the user in my DB has a row called 'premium', with a value of 1. I've tried figuring it out on my own but I'm stuck. I watched a video of someone doing something similar so this is what I have currently, but I'm aware it's totally incorrect. //init.php file <?php if (session_status() == PHP_SESSION_NONE) {//php >5.4 session_start(); } $_SESSION['user_id'] = 1; $db = new PDO('mysql:host=127.0.0.1;dbname=loginsystem', 'root', ''); $userQuery = $db->prepare(" SELECT idUsers, uidUsers, emailUsers, pwdUsers, premium FROM users WHERE idUsers =:user_id "); $userQuery->execute(['user_id' => $_SESSION['user_id']]); $user = $userQuery->fetchObject(); //webpage in which the image will appear <?php require_once '../includes/init.php'; ?> <?php if($user-> premium): ?> <img src="../../img/premium.png" alt="Swiftify+"></a> <?php else: ?> <img src="../../img/swiftify.png" alt="Swiftify+"></a> <?php endif; ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted August 29, 2019 Share Posted August 29, 2019 And what happens when you run it? Quote Link to comment Share on other sites More sharing options...
Jeff13246 Posted August 29, 2019 Author Share Posted August 29, 2019 (edited) 44 minutes ago, Barand said: And what happens when you run it? If I assign only one of the users as having a value of 1 for the premium row, the image: "../../img/premium.png" appears on each users profile. I want this image to appear only on the users profile's who has the 'premium' row set as 1. I attached a picture of the user who has the specified row set as 1 if it helps to see. If I set it as 0, then the image "../../img/swiftify.png" appears on every users profile. Also if it helps, when I var_dump($user) I get object(stdClass)#3 (5) { ["idUsers"]=> string(1) "1" ["uidUsers"]=> string(11) "beanbean123" ["emailUsers"]=> string(20) "jeffgee999@gmail.com" ["pwdUsers"]=> string(60) "$2y$10$VzYSkM7PoVTEzcY9rCnBXejCCMp0kAaXnSa630sBuXWkx/jqh6NSG" ["premium"]=> string(1) "1" } Edited August 29, 2019 by Jeff13246 additional info Quote Link to comment Share on other sites More sharing options...
Barand Posted August 29, 2019 Share Posted August 29, 2019 The code you posted only fetches the first record. I don't see how it can behave as you describe. Quote Link to comment Share on other sites More sharing options...
Jeff13246 Posted August 29, 2019 Author Share Posted August 29, 2019 29 minutes ago, Barand said: The code you posted only fetches the first record. I don't see how it can behave as you describe. Like I said I'm completely new to PHP haha. I've been learning the basics through instructional guides and I'm completely stuck on how I can make this work. If you can give a brief explanation of where to go next, it would be a lot of help! Quote Link to comment Share on other sites More sharing options...
Barand Posted August 29, 2019 Share Posted August 29, 2019 First off, I'd put just the connection code in the included file. You also need to add a few options when connecting with PDO. EG $db = new PDO("mysql:host=".HOST.";dbname=$dbname;charset=utf8",USERNAME,PASSWORD); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); Put the query and result processing in the main file. $userQuery = $db->prepare(" SELECT idUsers, uidUsers, emailUsers, pwdUsers, premium FROM users WHERE idUsers =:user_id "); $userQuery->execute(['user_id' => $_SESSION['user_id']]); while ($user = $userQuery->fetchObject()) { echo $user->premium ? '<img src="../../img/premium.png" alt="Swiftify+">' : '<img src="../../img/swiftify.png" alt="Swiftify+">' ; echo '<br>'; } Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted August 29, 2019 Share Posted August 29, 2019 1 hour ago, Jeff13246 said: appears on each users profile. probably because the code is assigning a 1 to the session variable. your code should - unconditionally start the session, in a common 'required' configuration/initialization file. first detect if there is a logged in user. if there's not a logged in user, there's no point in querying to get any user data. you should instead set up a default set of $user data. if there is a logged in user, run the query to get the user's data. you should only SELECT the columns you want, and unless this is part of a login script, you should not SELECT the password hash. if the query didn't find any matching user data, you either have a programming mistake or a user's row got deleted between the time the user logged in and when you executed this query. if the query did find matching user data, fetch it to replace the default values set in item #2. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.