views:

190

answers:

3

Hello, 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 )

+2  A: 

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.

Updated the post. I currently don't dump the post and vars, however, this is currently being revised throughout the whole of the PHP application.
Shamil
+1  A: 

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?
Rob
Results:string(1) "1" Echo - 1It's a match!
Shamil
+1  A: 

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 ");
}
sasa
Warning: mysqli_affected_rows() expects parameter 1 to be mysqli, object given in /home/radonsys/public_html/control/useredit.php on line 11Nope. Line 11 is: if(mysqli_affected_rows($sql) == 0) {
Shamil
Sorry, I'm just edited post. Try it again.
sasa
I've updated my post to include the output!
Shamil
Maybe syntax error? You have no opened bracket after if
sasa
Nope, that's present in the code here - must have accidentally removed it when copying and paste.
Shamil
Try to use mysql_fetch_assoc instead mysql_fetch_array, or try $current['userid'] == 1 without quotes...
sasa
Well, the echoed stuff is correct, but the checkbox, and statement:$currentperms['newapp'], still show 1 :|
Shamil
Have you thought any more on this?
Shamil