SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SQLSRV_FETCH_ARRAY how to store the arrays in a variable

    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. I tried some many things, but its still nt working.

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    $resulmat = sqlsrv_query($conn,$sql);
    print_r( sqlsrv_errors());

    while($row = sqlsrv_fetch_array($resultmat)){

    Typo

  3. #3
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    for some reason. the green part now is not echoing anything. thanks for the help so far, i cant believe i made such a silly error with the spelling but yeah now i have this new prob. will be grateful if you could help

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    // use the . to concatenate the expanded array element correctly into the sql string
    $sql="SELECT user_email FROM Inventory WHERE item = '" $row_loo[item]. "'";

    // add a line of debug here - this is how you prove whether the sql statement is 
    // properly constructed - copy the output, paste it into your db, does it get results?
    // does it show errors?  RM it when when case is proven
    echo $sql;


    $resultmat sqlsrv_query($conn,$sql);
    if(
    sqlsrv_has_rows($resultmat)){
    echo 
    'success';

    HTH

  6. #6
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face

    Quote Originally Posted by Cups View Post
    PHP Code:
    // use the . to concatenate the expanded array element correctly into the sql string
    $sql="SELECT user_email FROM Inventory WHERE item = '" $row_loo[item]. "'";

    // add a line of debug here - this is how you prove whether the sql statement is 
    // properly constructed - copy the output, paste it into your db, does it get results?
    // does it show errors?  RM it when when case is proven
    echo $sql;


    $resultmat sqlsrv_query($conn,$sql);
    if(
    sqlsrv_has_rows($resultmat)){
    echo 
    'success';

    HTH
    SO I FOUND A SOLUTION ACTUALLY. SO WHAT I DID WAS:

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


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

    }

    So this basically stores the values from my query as $loo_email1,$loo_email2,$loo_email3 ........ This is bascially what i wanted. Anyways thanks guys.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •