0

Basically, I try to fetch player id it outputs id 17 it's a correct number and I'm pretty sure it's correct to there. Then use these values to get a list of trade info. The database looks like this:

enter image description here The PHP function that I call

function listtrade() {
    
    
    global $db;
    $damnid = usertouserid($usernick);
    //SQL queries really suck dude
    $dt = "SELECT * FROM trades WHERE tradereceiver = '$damnid' ORDER BY tradeid DESC";
    $dbdata = array();
    $tsr = mysqli_query($db, $dt);
    while ($dbdatadt = mysqli_fetch_assoc($tsr)) {
        $dbdata[] = $dbdatadt;
    }
    return $dbdata;

}

Steps I tried

  1. adding and removing '
  2. removing order by

but it does not solve, any reason this happens? It looks very dumb for me, it should run correctly, the syntax is correct.

bruhhy15
  • 1
  • 5
  • Where is `$usernick` set? – Nigel Ren May 17 '21 at 19:19
  • this is test code not used in production. everything is in prepared statements just for testing before trying prepared statements. – bruhhy15 May 17 '21 at 19:19
  • i have a function gathers user id from db. Returns correctly fine too. 17 is expected result and result is 17. Usernick set in header.php file takes it from session. Given username checked on db converts to id (tested fine) but it does not do main statement correctly – bruhhy15 May 17 '21 at 19:20
  • 1
    But it's not set within this function. – Nigel Ren May 17 '21 at 19:25
  • If it's a global variable you need `global $usernick;` – Barmar May 17 '21 at 19:26
  • can you explain this "$dbdata[] = $dbdatadt" , did you mean to use array_push ? – Reda Bourial May 17 '21 at 19:26
  • yes true its not set in this function but its exists beacuse from other queries i did it does them correctly just this one for some reason fails (copying pasting code from other queries dont solve too if i use tradereceiver – bruhhy15 May 17 '21 at 19:26
  • 1
    @RedaBourial That's standard PHP syntax, it's the same as `array_push()`. – Barmar May 17 '21 at 19:27
  • yes thank you global is thing i forgot – bruhhy15 May 17 '21 at 19:27
  • 1
    Maybe you should get out of the habit of using global variables. Pass everything as parameters. – Barmar May 17 '21 at 19:28
  • actually you saying right. Im coming from csharp side i thought its pretty similar and i forget sometimes you need global blahblah; and sometimes i try to define variables like string $variable – bruhhy15 May 17 '21 at 19:30

1 Answers1

0

Check below things: 1. global $db;->print this, check the connection is correct? 2. $damnid = usertouserid($usernick);-> print this getting value 17? 3. Check table name 'trades' exists in database? 4. $usernick is not defined in your code, it seems?

user27976
  • 149
  • 1
  • 7
  • This content (asking for clarification) is fitting for a comment, not an answer. Once you have [sufficient reputation](https://stackoverflow.com/help/privileges/comment), you'll be able to comment on other people's posts, but in the meantime, please avoid using the answer space to get around this limitation. – El_Vanja May 17 '21 at 19:51
  • Thanks for the information, i will follow! – user27976 May 17 '21 at 19:56
  • thanks really helpful – bruhhy15 May 17 '21 at 20:09