#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    12
    Rep Power
    0

    Storing sqlsrv_fetch_array problem!!!


    Hi, can someone tell me how to store the arrays retrieved frm a database using sqlsrv_fetch_array to a variable? say for example this is my script:

    echo $email;
    $sql_int="SELECT item FROM Inventory WHERE user_email = '$email'";
    $resultint = sqlsrv_query($conn,$sql_int);
    $sql_loo="SELECT item FROM Looking WHERE user_email = '$email'";
    $resultloo = sqlsrv_query($conn,$sql_loo);
    print_r( sqlsrv_errors());
    while($row_int = sqlsrv_fetch_array($resultint)){
    echo $row_int['item'];
    print_r( sqlsrv_errors());
    }

    $row_loo = array();
    while($row_loo = sqlsrv_fetch_array($resultloo)){
    echo $row_loo['item'];
    print_r( sqlsrv_errors());

    }

    $row_loo = sqlsrv_fetch_array($resultloo);
    echo $row_loo['item'];


    $sql="SELECT user_email FROM Inventory WHERE item = '$row_loo'";
    $resulmat = sqlsrv_query($conn,$sql);
    print_r( sqlsrv_errors());

    while($row = sqlsrv_fetch_array($resultmat)){
    echo $row['user_email'];
    print_r( sqlsrv_errors());


    i am getting an error that says the underlined script, has null resource. I knw the problem is in the bold line. $row_loo doesnt have a value. The value is supposed to come frm the blue coloured lines. For some reason, it does echo the values, but outside of the loop the variable is empty. so i added another line ( the red one)outside the loop to fetch array values again, but its still not working. can someone help? I tried echoing some of the values to test and see if they are working by the ways.Thanks in advance. Still a newbie.
    Oh by the way, the code is working fine up until the end of the blue lines. The problem with the script starts from the red lines to the bottom.
  2. #2
  3. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
    - In the blue lines you will grab every single row from $resultloo that there is. That means after the loop there won't be any more rows, and thus the red sqlsrv_fetch_array() won't return anything. Rethink what you're doing.

    - $row_loo will be an array, so in the bold line you should get an "Array to string conversion" warning (you don't because $row_loo is null). Just like you used $row_loo['item'] in the blue, you need to do the same in the bold.

    - The underlined line uses $resultmat but a couple lines up you typoed the variable name as "$resulmat".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    12
    Rep Power
    0
    Originally Posted by requinix
    - In the blue lines you will grab every single row from $resultloo that there is. That means after the loop there won't be any more rows, and thus the red sqlsrv_fetch_array() won't return anything. Rethink what you're doing.

    - $row_loo will be an array, so in the bold line you should get an "Array to string conversion" warning (you don't because $row_loo is null). Just like you used $row_loo['item'] in the blue, you need to do the same in the bold.

    - The underlined line uses $resultmat but a couple lines up you typoed the variable name as "$resulmat".
    Requinix thanks a million!!!! Prob solved but only one thing though, the while loop for resultmat is not displaying anything. $resultmat is no longer null, i echoed it and it displayed something like resource id8# but the only thing is the while loop thtas supposed to show the resultmat data is not working for some reason. any ideas? i tried checking the table and database, it has entries and data in them, so it shud definitely work by right.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    12
    Rep Power
    0
    Originally Posted by kumsan
    Requinix thanks a million!!!! Prob solved but only one thing though, the while loop for resultmat is not displaying anything. $resultmat is no longer null, i echoed it and it displayed something like resource id8# but the only thing is the while loop thtas supposed to show the resultmat data is not working for some reason. any ideas? i tried checking the table and database, it has entries and data in them, so it shud definitely work by right.

    SO to check if the query actually worked, i added
    if(sqlsrv_has_rows($resultmat)){
    echo '1';
    }
    and guess what it didnt display echo 1.
  8. #5
  9. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    12
    Rep Power
    0
    Originally Posted by requinix
    So what's your revised code now?
    echo $email;
    $sql_int="SELECT item FROM Inventory WHERE user_email = '$email'";
    $resultint = sqlsrv_query($conn,$sql_int);
    $sql_loo="SELECT item FROM Looking WHERE user_email = '$email'";
    $resultloo = sqlsrv_query($conn,$sql_loo);
    print_r( sqlsrv_errors());

    while($row_int = sqlsrv_fetch_array($resultint)){
    echo $row_int['item'];
    print_r( sqlsrv_errors());
    }


    while($row_loo = sqlsrv_fetch_array($resultloo)){
    echo $row_loo['item'];
    print_r( sqlsrv_errors());
    }

    echo'will';
    $sql="SELECT user_email FROM Inventory WHERE item = '$row_loo[item]'";
    $resultmat = sqlsrv_query($conn,$sql);
    if(sqlsrv_has_rows($resultmat)){
    echo 'success';
    }

    while($row = sqlsrv_fetch_array($resultmat)){
    echo $row['user_email'];
    echo 'willer';
    print_r( sqlsrv_errors());
    }


    ?>

    so the dark red part does not echo success. and the green line echoes nothing, not even the willer. by teh way, when i echoed resultmat, it says smtg like resource id8#. so i am guessing that the resultmat does return a values as i am no longer getting any error that its null.
    I check my databse many many times and i am very sure that there are datas that it shud match and display.
  12. #7
  13. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
    It goes back to what I said before about the while loop. During the loop $row_loo will be an array with values, but eventually sqlsrv_fetch_array() will return null and the loop will exit.

    After the loop $row_loo is always null.

    Find your php.ini and set
    Code:
    display_errors = on
    error_reporting = -1
    Then restart the web server and try your script. You will see warning messages.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    12
    Rep Power
    0
    Originally Posted by requinix
    It goes back to what I said before about the while loop. During the loop $row_loo will be an array with values, but eventually sqlsrv_fetch_array() will return null and the loop will exit.

    After the loop $row_loo is always null.

    Find your php.ini and set
    Code:
    display_errors = on
    error_reporting = -1
    Then restart the web server and try your script. You will see warning messages.
    Do you have any suggestion how to overcome this. i understand now why i am getting this error. so lets say i want to compare row_loo with values from another database. can i just remove the first while loop and just have a statement that says: $row_loo=sqlsrv_fetch_array($resultmat);
    after that i just use row_loo to retrieve data from another table. this means i will nt echo the row_loo values using the while loop?
  16. #9
  17. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    12
    Rep Power
    0

    managed to solve it.:)


    $c=1;
    while($row=sqlsrv_fetch_array($resultmat,SQLSRV_FETCH_ARRAY)){


    $variablename = 'loo_email' . $c;
    $$variablename = $resultloo_row['user_email'];
    $c++;

    }


    thats what i did. so it saves the values from the query as $loo_email1,$loo_email2,$loo_email3....... This is basically what i wanted. Thanks guys.

IMN logo majestic logo threadwatch logo seochat tools logo