1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

expects parameter 1 to be mysqli_result, boolean

Discussion in 'PHP' started by WhitneyM, Jan 16, 2010.

  1. #1
    I am working on an update for my online horse sim game, I want to add a salary into everyones bank account so here is the code:

    $today=date("Y-m-d");

    $sql="SELECT * FROM upgrades";
    $result=mysqli_query($cxn, $sql) or die("Couldn't execute query.");
    while($row=mysqli_fetch_assoc($result))
    {
    extract($row);
    If($upgradetype=="free")
    {
    $sql="SELECT * FROM Bank WHERE user='$username'";
    $result=mysqli_query ($cxn, $sql) or die ("couldn't execute query.");
    $row=mysqli_fetch_row($result);
    extract($row);
    $newbalance=$balance+1000;
    echo"$username $newbalance";
    }
    Elseif ($upgradetype=="bronze")
    {
    $sql="SELECT * FROM Bank WHERE user='$username'";
    $result=mysqli_query ($cxn, $sql) or die ("couldn't execute query.");
    $row=mysqli_fetch_row($result);
    extract($row);
    $newbalance=$balance+2000;
    echo"$username $newbalance";
    }
    Elseif ($upgradetype=="silver")
    {
    $sql="SELECT * FROM Bank WHERE user='$username'";
    $result=mysqli_query ($cxn, $sql) or die ("couldn't execute query.");
    $row=mysqli_fetch_row($result);
    extract($row);
    $newbalance=$balance+4000;
    echo"$username $newbalance";
    }
    Elseif ($upgradetype=="gold")
    {
    $sql="SELECT * FROM Bank WHERE user='$username'";
    $result=mysqli_query ($cxn, $sql) or die ("couldn't execute query.");
    $row=mysqli_fetch_row($result);
    extract($row);
    $newbalance=$balance+6000;
    echo"$username $newbalance";
    }
    $query="UPDATE bank set balance='$newbalance' where user='$username'";
    $result=mysqli_query($cxn, $query) or die("Couldn't execute query.");
    }

    and I am getting this error message:

    test2 6000
    Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\AppServ\www\updatesalary.php on line 12

    test2 is a username that I am testing right now and the 6000 is the new balance. I am not sure what I am doing wrong. It will list the usernames if I replace the code in the IF statement with echo"$username" and dont have the query on the bottom. I don't know why it would be doing that because it seems to be giving me an error message about the first query which works fine. Thanks in advance for any help.
     
    WhitneyM, Jan 16, 2010 IP
  2. mbaldwin

    mbaldwin Active Member

    Messages:
    215
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    95
    #2
    Hi,
    Some echo statements might help find out where the problem is.

    after your line:
    
    $sql="SELECT * FROM Bank WHERE user='$username'";
    
    Code (markup):
    add
    
    echo 'free user sql = '.$sql.'<br/>';
    die();
    
    Code (markup):
    You can then see if your select statement is being setup like you want it to be.
    Michael
     
    mbaldwin, Jan 16, 2010 IP
  3. WhitneyM

    WhitneyM Guest

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    free user sql = SELECT * FROM bank WHERE user='test2'

    This is what I get when I enter that code. But I believe that is what I want to select. So I think my query would be correct then?
     
    WhitneyM, Jan 17, 2010 IP
  4. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The problem is not with the query, it's not an SQL error it's a PHP function error.

    You are not passing an SQLi result to the mysqli_fetch_assoc() function, as it returns an Object when using query() you must access it like an object.

    Try this.

    
    $today=date("Y-m-d");
    
    $sql="SELECT * FROM upgrades";
    $result=mysqli_query($cxn, $sql) or die("Couldn't execute query.");
    while ($row = $result->fetch_assoc())
    {
    extract($row);
    If($upgradetype=="free")
    {
    $sql="SELECT * FROM Bank WHERE user='$username'";
    $result=mysqli_query ($cxn, $sql) or die ("couldn't execute query.");
    $row=mysqli_fetch_row($result);
    extract($row);
    $newbalance=$balance+1000;
    echo"$username $newbalance";
    }
    
    PHP:
    Notice the while($row=mysqli_fetch_assoc($result)) was changed to while ($row = $result->fetch_assoc()) { as $result is now an object.

    You can view more examples of using this here: http://php.net/manual/en/mysqli-result.fetch-assoc.php

    Regards,
    Steve
     
    Steve136, Jan 17, 2010 IP
  5. mbaldwin

    mbaldwin Active Member

    Messages:
    215
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    95
    #5
    yes, that is right, if that user was a free member.
    Do that under all your select statements, to make sure they are all working right. You will need to change the user upgrade to make sure the select statement you want is being processed due to your if statement, this will check your if statement as well.

    you can also add this to get your mysql errors

    
    echo 'something'.mysqli_error($cxn);
    
    Code (markup):
    Change the something to a description, so you know where the error happened in your script, and place the code under all your

    
    $row=mysqli_fetch_row($result);
    
    Code (markup):
    You will need to change
    {CODE]
    $result=mysqli_query ($cxn, $sql) or die ("couldn't execute query.");
    [/CODE]
    to
    {CODE]
    $result=mysqli_query ($cxn, $sql);
    [/CODE]

    That should help narrow down the problem.

    Michael
     
    mbaldwin, Jan 17, 2010 IP
  6. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The problem is the mysqli_fetch_assoc will not work with $result passed as a parameter, it needs to be accessed like an Object.

    Also mysqli_error won't explain what's going wrong here, as it's not an SQL error it's a PHP function error, it was already reporting what's wrong when it said the first parameter needed to be an mysqli_result.

    Regards,
    Steve
     
    Steve136, Jan 17, 2010 IP
  7. WhitneyM

    WhitneyM Guest

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Ok, so I am really confused. Sorry, I'm really new to this and the answer is most likely really obvious. So I changed the code to this:

    $today=date("Y-m-d");

    $sql="SELECT * FROM upgrades";
    $result=mysqli_query($cxn, $sql);
    while ($row = $result->fetch_assoc())
    {
    extract($row);
    $sql="SELECT * FROM bank WHERE user='$username'";
    $result=mysqli_query ($cxn, $sql);
    $row=mysqli_fetch_assoc($result);
    extract($row);
    If($upgradetype=="free")
    {
    $newbalance=$balance+1000;
    echo"$username $newbalance";
    $transaction=1000;
    }
    Elseif ($upgradetype=="bronze")
    {
    $newbalance=$balance+2000;
    echo"$username $newbalance";
    $transaction=2000;
    }
    Elseif ($upgradetype=="silver")
    {
    $newbalance=$balance+4000;
    echo"$username $newbalance";
    $transaction=4000;
    }
    Elseif ($upgradetype=="gold")
    {
    $newbalance=$balance+6000;
    echo"$username $newbalance";
    $transaction=6000;
    }
    $query="UPDATE bank set balance='$newbalance' where user='$username'";
    $result=mysqli_query($cxn, $query) or die("Couldn't execute query to change balance.");
    $query="INSERT INTO transactions (transactiondate, transactionmemo, transactiontype,

    transactionamount, username, othername) values ('$today', 'Salary', 'Deposit', '$transaction',

    '$username', 'IE Staff')";
    $result=mysqli_query($cxn, $query) or die("Couldn't execute query to insert into transactions.");
    }
    ?>

    It logs into the server earlier, but anyways this is the result I get:

    test6 200973
    Fatal error: Call to a member function fetch_assoc() on a non-object in C:\AppServ\www\updatesalary.php on line 12

    It will update only the first member it pulls up (I erased some of the other members because of other testing I was doing so now test6 is the first member). It will execute all of the queries for that member, but not anything else. I tried changing everything like you guys said, but I couldn't figure out the 'something'.mysqli_error($cxn); I didn't quite understand that. Sorry I'm not helping more thanks for all the help so far.
     
    WhitneyM, Jan 23, 2010 IP