Jump to content
MakeWebGames

more sql problems.....


Recommended Posts

Posted (edited)

i am using php 8 & PDO but i am trying to select enum from the database for friends and enemys list and im not getting an error but i can only get it to say that i am friends with them so it just says remove friends  i have tried several different things to get it to select if your friends or enemys but it will not let me do it here is my code

 

    $db->query('SELECT type, user, person FROM lists WHERE user = ? AND person = ?');
    $db->execute([$i['userid'], $u['userid']]);
    $row = $db->fetch_row();
if(!$row) {
        $fri = '<a href="friends?add='.$profile_info->name.'">Add friend</a>';
        $ene = '<a href="enemys?add='.$profile_info->name.'">Add enemy</a>';
    } else {
if($row['type'] = 'friend') {
    $fri = 'remove friend';
    $ene = '';


} else if($row['type'] = 'enemy') {
    $fri = '';
    $ene = 'Remove enemy';

}
}

 

Edited by WarMad
Changed name because realized it’s an sql problem
Posted

If you are doing if statements for comparisons you should be using at least == operator

 

if($row['type'] = 'enemy')

That is your is statements which is setting the variables to enemy as you are only using single =

  • Like 2
Posted (edited)
$db->query('SELECT type, user, person FROM lists WHERE user = ? AND person = ?');
$db->execute([$i['userid'], $u['userid']]);
$row = $db->fetch_row();
$fri = (!$row ? '<a href="friends?add='.$profile_info->name.'">Add friend</a>' : ($row['type'] == 'enemy' ? '' : ($row['type'] == 'friend' ? 'Remove Friend' : '')));
$ene = (!$row ? '<a href="enemys?add='.$profile_info->name.'">Add enemy</a>' : ($row['type'] == 'friend' ? '' : ($row['type'] == 'enemy' ? 'Remove Enemy' : '')));

i would use this cuts down the code alot and does the same thing.

  

55 minutes ago, ags_cs4 said:

i will say its not an enum error should be an sql 

use == no just one

and !$row use the count function in pdo rowCount()

 

missed you are using MySQLi prepared statements and not PDO 

Edited by URBANZ
  • WarMad changed the title to more sql problems.....
Posted
8 minutes ago, Magictallguy said:

I believe he's using a PDO wrapper I released a few years ago

yes i am

I found out that the sql is not working for some reason I double checked everything and I dont see anything wrong with it.

47 minutes ago, URBANZ said:

Solved lol

How is it solved?

Posted (edited)
1 hour ago, WarMad said:

yes i am

I found out that the sql is not working for some reason I double checked everything and I dont see anything wrong with it.

How is it solved?

i mean the wrapper part not the actual sql lol was wondering if you was using a wrapper as thats not the correct way PDO does things thats MySQLi.

try this, not sure it will work, @Magictallguy will know more as he made the wrapper but im going by MySQLi prepared statments based on you using fetch_row();

 

MySQLi Version

$lists = $db->prepare("SELECT type, user, person FROM lists WHERE user = ? AND person = ?");
$lists->bind_param('ii', $i['userid'], $u['userid']);
$lists->execute();
$list = $lists->fetch_row();
$fri = (!$list ? '<a href="friends?add='.$profile_info->name.'">Add friend</a>' : ($list['type'] == 'enemy' ? '' : ($list['type'] == 'friend' ? 'Remove Friend' : '')));
$ene = (!$list ? '<a href="enemys?add='.$profile_info->name.'">Add enemy</a>' : ($list['type'] == 'friend' ? '' : ($list['type'] == 'enemy' ? 'Remove Enemy' : '')));

 

PDO Version

$lists = $db->prepare("SELECT type, user, person FROM lists WHERE user = :user AND person = :person");
$lists->bindParam(':user', $i['userid'], PDO::PARAM_INT);
$lists->bindParam(':person', $u['userid'], PDO::PARAM_INT);
$lists->execute();
$list = $lists->fetch();   

$fri = (!$list ? '<a href="friends?add='.$profile_info->name.'">Add friend</a>' : ($list['type'] == 'enemy' ? '' : ($list['type'] == 'friend' ? 'Remove Friend' : '')));
$ene = (!$list ? '<a href="enemys?add='.$profile_info->name.'">Add enemy</a>' : ($list['type'] == 'friend' ? '' : ($list['type'] == 'enemy' ? 'Remove Enemy' : '')));

 

Edited by URBANZ
Posted
2 hours ago, URBANZ said:

i mean the wrapper part not the actual sql lol was wondering if you was using a wrapper as thats not the correct way PDO does things thats MySQLi.

try this, not sure it will work, @Magictallguy will know more as he made the wrapper but im going by MySQLi prepared statments based on you using fetch_row();

 

MySQLi Version

$lists = $db->prepare("SELECT type, user, person FROM lists WHERE user = ? AND person = ?"); $lists->bind_param('ii', $i['userid'], $u['userid']); $lists->execute(); $list = $lists->fetch_row(); $fri = (!$list ? '<a href="friends?add='.$profile_info->name.'">Add friend</a>' : ($list['type'] == 'enemy' ? '' : ($list['type'] == 'friend' ? 'Remove Friend' : ''))); $ene = (!$list ? '<a href="enemys?add='.$profile_info->name.'">Add enemy</a>' : ($list['type'] == 'friend' ? '' : ($list['type'] == 'enemy' ? 'Remove Enemy' : '')));

$lists = $db->prepare("SELECT type, user, person FROM lists WHERE user = ? AND person = ?");
$lists->bind_param('ii', $i['userid'], $u['userid']);
$lists->execute();
$list = $lists->fetch_row();
$fri = (!$list ? '<a href="friends?add='.$profile_info->name.'">Add friend</a>' : ($list['type'] == 'enemy' ? '' : ($list['type'] == 'friend' ? 'Remove Friend' : '')));
$ene = (!$list ? '<a href="enemys?add='.$profile_info->name.'">Add enemy</a>' : ($list['type'] == 'friend' ? '' : ($list['type'] == 'enemy' ? 'Remove Enemy' : '')));

 

PDO Version

$lists = $db->prepare("SELECT type, user, person FROM lists WHERE user = :user AND person = :person"); $lists->bindParam(':user', $i['userid'], PDO::PARAM_INT); $lists->bindParam(':person', $u['userid'], PDO::PARAM_INT); $lists->execute(); $list = $lists->fetch(); $fri = (!$list ? '<a href="friends?add='.$profile_info->name.'">Add friend</a>' : ($list['type'] == 'enemy' ? '' : ($list['type'] == 'friend' ? 'Remove Friend' : ''))); $ene = (!$list ? '<a href="enemys?add='.$profile_info->name.'">Add enemy</a>' : ($list['type'] == 'friend' ? '' : ($list['type'] == 'enemy' ? 'Remove Enemy' : '')));

$lists = $db->prepare("SELECT type, user, person FROM lists WHERE user = :user AND person = :person");
$lists->bindParam(':user', $i['userid'], PDO::PARAM_INT);
$lists->bindParam(':person', $u['userid'], PDO::PARAM_INT);
$lists->execute();
$list = $lists->fetch();   

$fri = (!$list ? '<a href="friends?add='.$profile_info->name.'">Add friend</a>' : ($list['type'] == 'enemy' ? '' : ($list['type'] == 'friend' ? 'Remove Friend' : '')));
$ene = (!$list ? '<a href="enemys?add='.$profile_info->name.'">Add enemy</a>' : ($list['type'] == 'friend' ? '' : ($list['type'] == 'enemy' ? 'Remove Enemy' : '')));

 

I will give this a try I’m trying to wait and see if mtg reply’s first sense he did make the wrapper 

Posted (edited)

You're using MTGs wrapper? I think the issue has just been overlooked, I use this wrapper for pretty much every PHP project i do.

I'm not sure if yours is any different than mine but I see you did this here: 

$row = $db->fetch_row();

His wrapper is meant to handle most of this for you, just try switching that particular line to: 

$row = $db->fetch();

I ran into this issue once and I swear that's how it was solved.

Edited by InverTed
  • 3 weeks later...
Posted
$db->query('SELECT type, user, person FROM lists WHERE user = ? AND person = ?');
$db->execute([$i['userid'], $u['userid']]);
$row = $db->fetch(true);
if(empty($row)) {
    $fri = '<a href="friends?add='.$profile_info->name.'">Add friend</a>';
    $ene = '<a href="enemys?add='.$profile_info->name.'">Add enemy</a>';
} elseif($row['type'] === 'friend') {
    $fri = 'Remove Friend';
    $ene = '';
} else if($row['type'] === 'enemy') {
    $fri = '';
    $ene = 'Remove enemy';
}

 

Posted
On 9/30/2021 at 5:44 PM, Magictallguy said:

$db->query('SELECT type, user, person FROM lists WHERE user = ? AND person = ?'); $db->execute([$i['userid'], $u['userid']]); $row = $db->fetch(); if(empty($row)) { $fri = '<a href="friends?add='.$profile_info->name.'">Add friend</a>'; $ene = '<a href="enemys?add='.$profile_info->name.'">Add enemy</a>'; } elseif($row['type'] === 'friend') { $fri = 'Remove Friend'; $ene = ''; } else if($row['type'] === 'enemy') { $fri = ''; $ene = 'Remove enemy'; }

$db->query('SELECT type, user, person FROM lists WHERE user = ? AND person = ?');
$db->execute([$i['userid'], $u['userid']]);
$row = $db->fetch(true);
if(empty($row)) {
    $fri = '<a href="friends?add='.$profile_info->name.'">Add friend</a>';
    $ene = '<a href="enemys?add='.$profile_info->name.'">Add enemy</a>';
} elseif($row['type'] === 'friend') {
    $fri = 'Remove Friend';
    $ene = '';
} else if($row['type'] === 'enemy') {
    $fri = '';
    $ene = 'Remove enemy';
}

 

I will give that a try

  • 5 months later...
Posted
On 9/30/2021 at 12:44 PM, Magictallguy said:

$db->query('SELECT type, user, person FROM lists WHERE user = ? AND person = ?'); $db->execute([$i['userid'], $u['userid']]); $row = $db->fetch(true); if(empty($row)) { $fri = '<a href="friends?add='.$profile_info->name.'">Add friend</a>'; $ene = '<a href="enemys?add='.$profile_info->name.'">Add enemy</a>'; } elseif($row['type'] === 'friend') { $fri = 'Remove Friend'; $ene = ''; } else if($row['type'] === 'enemy') { $fri = ''; $ene = 'Remove enemy'; }

$db->query('SELECT type, user, person FROM lists WHERE user = ? AND person = ?');
$db->execute([$i['userid'], $u['userid']]);
$row = $db->fetch(true);
if(empty($row)) {
    $fri = '<a href="friends?add='.$profile_info->name.'">Add friend</a>';
    $ene = '<a href="enemys?add='.$profile_info->name.'">Add enemy</a>';
} elseif($row['type'] === 'friend') {
    $fri = 'Remove Friend';
    $ene = '';
} else if($row['type'] === 'enemy') {
    $fri = '';
    $ene = 'Remove enemy';
}

 

That didn’t work I ended up just canceling the whole thing and giving it 1000 different tries to get the wrapper to work and I can’t seem to get it 

 

I can get it to not display any errors but when it’s not then it still doesn’t send the query to the database it acts as if the query is not there but with that being said it will pull from the database just not update it it….

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