Jump to content
MakeWebGames

updating from php 7 to php 8 enum issue


Recommended Posts

Posted (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 by WarMad
adding more detail
Posted (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 by WarMad
Posted

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’];

A761F079-7A5B-4F20-BB62-52FEBED68E63.jpeg

Posted

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.

  • Like 1
Posted

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?

Posted

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 

Posted
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();

 

Posted

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;

  • Thanks 1
Posted
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

  • Like 1
Posted

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

Posted (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 by SRB
  • Like 1
  • Thanks 1

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.   Paste as plain text instead

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