WarMad Posted August 16, 2021 Posted August 16, 2021 (edited) ok I am making my own game script and I for some reason started it in php 7. Now that i upgraded to php 8 i cant get gender(in the database as enum) to display as text i can only get it to display a blank spot where the text should be which gives an array offset error or i get it to display as a number the game is also running on PDO. Does anyone have any suggestions? oh and im doing it all from xampp not sure if that matters i had to change it to int and add if 1 male else if 2 female etc if anyone knows a better way to do it in php 8 let me know Edited September 1, 2021 by WarMad adding more detail Quote
WarMad Posted August 18, 2021 Author Posted August 18, 2021 And it’s still not displaying There are no errors it’s just a blank spot where gender should be displayed Quote
AdamHull Posted August 18, 2021 Posted August 18, 2021 what did you try? add the code and we will be able to help you better Quote
WarMad Posted August 18, 2021 Author Posted August 18, 2021 (edited) Maybe I’m not understanding isset I know with pho 8 you can’t just have $row[‘gender’] it needs to be isset($row[‘gender’]) maybe it’s the isset? I don’t fully understand isset Without it I get array offset errors I will post the code tomorrow I need to wait for my data to renew I don’t have internet I only use mobile hotspot…. Edited August 19, 2021 by WarMad Quote
AdamHull Posted August 19, 2021 Posted August 19, 2021 Isset check to see if a array is set (anything in it) its the same as using !empty($_POST['gender']) 1 Quote
Magictallguy Posted August 19, 2021 Posted August 19, 2021 13 hours ago, WarMad said: I will post the code That'd help 😉 2 1 Quote
WarMad Posted August 20, 2021 Author Posted August 20, 2021 So I got it to work but I still get this error it was the isset causing it not to display but I’m not sure how to get it to display without throwing an error the code that is actually causing the problem are these 2(it will be more once I remove all the isset now that I understand it a little more) $this->sex = $row[‘gender’]; $this->crazy = $row[‘crazy’]; Quote
SRB Posted August 20, 2021 Posted August 20, 2021 It's telling you it's returning null in the error, so I'd say that either, 1) You do not have a default, or have not set, the value for gender (use NOT NULL in the database creation SQL) 2) You are selecting an invalid response. Note: If you are posting code, please post code - 5 lines before, 5 lines after - PHP's error handling doesn't already pick the correct line. 1 Quote
WarMad Posted August 20, 2021 Author Posted August 20, 2021 Ok I will try that I thought it already was not null but I might be wrong thank you and I have edited it so I knew that those 2 are causing the issue once I get home and on the pc I will try it ya it’s already not null And 5 lines above and 5 lines under are the same just with isset() because it was full of them I did that thinking it would fix it because I didn’t fully understand isset not I know it basically says 0 or 1 lol I remembered I had to do __construct($foo = NULL) { To get it to work is there something I can change that to? Quote
WarMad Posted August 23, 2021 Author Posted August 23, 2021 The only way I can get it to go away is if above the error I add. $row[‘gender’] = ‘’; above the error but I know that’s the incorrect way to fix it That being said it’s backend stuff and I would like to do that for the whole users table so it would be an insane amount of extra bs Quote
Magictallguy Posted August 24, 2021 Posted August 24, 2021 Provide us with the code. We can't help you if you're only posting your guesses to it 2 Quote
WarMad Posted August 24, 2021 Author Posted August 24, 2021 function __construct($id = null) { global $db; /*users*/ $db->query('SELECT userid, name, pass, level, experance, jail, hospital, gender, race, FROM players WHERE userid = ?'); $db->execute([$id]); $row = $db->fetch_row(true); $this->id = isset($row['userid']); $this->name = isset($row['name']); $this->pas = isset($row['pass']); $this->level = isset($row['level']); $this->exp = isset($row['experance']); $this->jail = isset($row['jail']); $this->hospital = isset($row['hospital']); $this->gender = $row['gender']; $this->race = $row['race']; $players = players::getInstance(); Quote
KyleMassacre Posted August 24, 2021 Posted August 24, 2021 I know you mentioned that you are not too familiar with isset but isset returns a bool, so everything you posted is either setting it to true or false. You could do (on phone can’t do code tags): $this->sex = isset($row['gender']) ? $row['gender'] : null; 1 Quote
WarMad Posted August 24, 2021 Author Posted August 24, 2021 I was going to remove isset once I fixed it without having it I will give that a try soon as I can Thank you Quote
KyleMassacre Posted August 25, 2021 Posted August 25, 2021 2 hours ago, WarMad said: I was going to remove isset once I fixed it without having it I will give that a try soon as I can Thank you You should always have a check to make sure that it in fact exists and to keep control so I wouldn't remove it 1 Quote
WarMad Posted August 25, 2021 Author Posted August 25, 2021 So i tried that and the error went away but it’s still not showing I am a male it’s just showing the default which I have it as ‘Male’,’Female’,’Unsure’ “Unsure” is default and that’s all that is displaying Never mind that fixed it it was displaying the default because of one of my fix attempts going wrong lol thank you Quote
SRB Posted August 25, 2021 Posted August 25, 2021 (edited) On 8/24/2021 at 11:31 PM, KyleMassacre said: $this->sex = isset($row['gender']) ? $row['gender'] : null; Null Coalesce, baby! $this->sex = $row['gender'] ?? null; Onto that function - that is TOTALLY the wrong approach. At the very least, that function should early-return an error, since you are selecting from the database by an integer field. If you are selecting by integer, your $id default should be an integer, so: function __construct($id = 0) Querying and then converting all the queries seems like you're adding complexity for no reason too. Simply assign the results of $row to $this->user, like so: function __construct($id = 0) { global $db; $db->query('SELECT userid, name, pass, level, experance, jail, hospital, gender, race FROM players WHERE userid = ?'); $db->execute([$id]); $row = $db->fetch_row(true); if (is_null($row)) { throw new Exception('User could not be found.'); } $this->user = $row; } Also, note the early-return? If there is no result, you should stop processing because you don't have the data required for the function I'd say, since you're loading this in the __construct, that would normally indicate it's needed. The above then let's you access all object properties using $this->user->field $this->user->userid $this->user->name $this->user->pass $this->user->level $this->user->experience $this->user->jail $this->user->hospital $this->user->gender $this->user->race PS: Experience is spelt wrong. Shouldn't be a comma after race in the query either; that will break the query and return nothing. Edited August 26, 2021 by SRB 1 1 Quote
WarMad Posted August 26, 2021 Author Posted August 26, 2021 Thank you I will play with it and change things around and see what result I get Quote
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.