0

I want to send the result of an SQL query which is a PHP variable to the index file. Here's from the PHP I want to send from:

$result = mysqli_query($conn, $sql);
$remaining = array();
while($row = mysqli_fetch_array($result)){
array_push($remaining, $row['_id']);
}
echo json_encode($remaining);

?>
<script>
      $.ajax({
        url: "index.php", 
        data: {result: result}, 
        dataType: "json",
        success: function(result){
}});
</script>

Here is where I want to use the code:

$var_value = $_GET['remaining'];
$values = implode(", ", $var_value);

$sql = "SELECT *
        FROM `species` 
        WHERE `_id` IN (".$valus.") ";

$results = mysqli_query($conn, $sql);

I have tried many methods, running a second ajax call within one already running since the $remaining variable is sent back to a javascript using ajax. $_cookies doesn't work for my webserver. So this seems the best solution. Can anyone see what is going wrong?

5
  • your question is not clear. What exactly you want to do? Commented May 3, 2015 at 8:55
  • Is there a reason you need to to use two transactions to the database? Could you refactor by making the SELECT * FROM art WHERE _id IN ( [$sql from first example] )? Commented May 3, 2015 at 9:00
  • @doublesidedstickytape I still have to send it to another document, since I use the PHP document only for the first query. Commented May 3, 2015 at 9:08
  • ok, bottomline: on the server, you get values from the database, renderthat in to a javascript and send that to the client. The client then sends back tha just generated values to the server to another page, where they are picked up as query parameters by php again? Sorry, that's like printing an email and sending it by fax to the next recipient. Consider to store the values in the session and skip that AJAX stuff. Commented May 3, 2015 at 9:10
  • @AxelAmthor How do I store the values from the query and use it in another mySQL query on the same page? Commented May 3, 2015 at 9:26

1 Answer 1

1

As reply to your comment: easy:

session_start(); // important!!

if ( !isset($_SESSION['remaining']) )
{
    $result = mysqli_query($conn, $sql);
    $remaining = array();
    while($row = mysqli_fetch_array($result)){
        array_push($remaining, $row['_id']);
    }
    $_SESSION['remaining'] = $remaining;
}
else
{
    $values = implode(",", $_SESSION['remaining']);
    /// $_SESSION['remaining'] = null - depends on logic.
    $sql = "SELECT *
        FROM `species` 
        WHERE `_id` IN (".$valus.") ";

    $results = mysqli_query($conn, $sql);
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the reply. This seems like a great solution, but I have issues accessing the server in the session even after setting save path, I get the permission denied (13) error. Got any ideas?
please explain " issues accessing the server in the session"
got it: the user which is running your web server has no access rights on the file path to store the session in.
Yeah, it seems so. I am currently trying to fix it and sending for help :). Thanks for the help though.
Is there another way to do it by $_POST or something? @Axel Amthor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.