bear
bear

Reputation: 11625

mysqli array -> query not attaining correct data

my mysqli_fetch_array(mysqi_query($db, $query)) doesn't appear to be getting the correct information from the database, and therefore, the PHP application is not working as it should.

Here are the queries,

<?php
if($_GET){
$current = mysqli_fetch_array(mysqli_query($db, "SELECT * from `tbl_user` WHERE `userid` = '".$_GET['userid']."'"));
$currentperms  = mysqli_fetch_array(mysqli_query($db, "SELECT * from `tbl_perms` WHERE `userid` = '".$_GET['userid']."'"));
} 
?>

Concurrently, the if ($current['userid'] == "1") {echo(" selected ");} isn't outputting anything at all, so the correct option isn't selected in the SELECT tag.

So, where: I'd expect: echo($currentperms['newapp']); not to equal 1, becuase it is set so in the database, the result of which is "1". I have test this by echoing the string gained. Newapp is not a column in the table either, so it shouldn't be returning "1" as a result.

With this:

 if $current['userid'] == "1") {
 echo(" selected ");
 }

Nothing is being echoed, however, the variable has been used in the script earlier, and the output of which is "1".

Please help me, I'm going through the roof :|

@sasa: output: Success!Array ( [0] => 1 [userid] => 1 [1] => shamil.nunhuck [username] => shamil.nunhuck [2] => Shamil Nunhuck [userfullname] => Shamil Nunhuck [3] => shamil.nunhuck@localhost [useremail] => shamil.nunhuck@localhost [4] => 6363d731bd7492fe4c33fc3d90fd61bc [userpassword] => 6363d731bd7492fe4c33fc3d90fd61bc [5] => 1 [userlevel] => 1 [6] => Administrator [usertitle] => Administrator [7] => 1 [tos] => 1 ) Array ( [0] => 1 [userid] => 1 [1] => 0 [ptodo] => 0 [2] => 0 [usercp] => 0 [3] => 0 [pm] => 0 [4] => 0 [bug] => 0 [5] => 0 [abug] => 0 [6] => 0 [admincp] => 0 [7] => 0 [intmgs] => 0 [8] => 0 [adduser] => 0 [9] => 0 [pass] => 0 [10] => 0 [useredit] => 0 [11] => 0 [listuser] => 0 [12] => 0 [newapp] => 0 )

Upvotes: 1

Views: 403

Answers (3)

sasa
sasa

Reputation: 2443

Try this code:

//  I put some echo for testing
if(!empty($_GET['id'])) {
    $id = (int)$_GET['id'];
    $sql = mysqli_query($db, "SELECT * from tbl_user WHERE userid = $id LIMIT 1");
    if(mysqli_affected_rows($db) == 0) {
        echo "There is no user with that id";
    } else {
        $current = mysql_fetch_assoc($sql);
        $currentperms  = mysql_fetch_assoc(mysqli_query($db, "SELECT * from tbl_perms WHERE userid = $id"));
        echo "Success!";
        // Or try to print result
        print_r($current);
        print_r($currentperms);
    }
} else {
    echo "There is no user id";
}

OR maybe just a syntax error:

With this:

 if $current['userid'] == "1") {
 echo(" selected ");
 }

It should be:

if($current['userid'] == 1) {
    echo(" selected ");
}

Upvotes: 0

Rob
Rob

Reputation: 48387

I'm not 100% sure I follow the question, but you appear to be saying that the result of $current['userid'] == "1" is false, despite $current['userid'] printing out as "1", correct?

If that's the case, it's possible there's some whitespace or other junk getting into the value which causes the comparison to fail, although I doubt that's the case if userid is auto-generated by the database.

You can verify this by more thoroughly inspecting the value, e.g.

var_dump( $current['userid'] ); // Check for whitespace
echo strlen( $current['userid'] ); // Is this greater than 1?

Upvotes: 1

Damian Casale
Damian Casale

Reputation:

If the issue is that the database is returning incorrect information it's probably best you just post the mysql portion of the code (esp. the query), the current results and what results you expected to see.

also fyi, I hope your parsing your $_GET and $_POST vars prior to dumping them into that query. Passing them straight into the SQL like that is not a good idea.

Upvotes: 2

Related Questions