PHP MySQL dont get last selected

633

i have a mysql Table with some rows.

The Select gets one (LIMIT 1) row. After a counter it reloads and get random another row. But sometimes he get the same (cause random).

How can i setup that he dont get the last row? The problem is, i cannot do that in mysql. I must do that in the SESSION or only on that site.

I tried that:

    $_SESSION['ads'] .= ','.$adid;

put that adid in "ads" SESSION and read it before SELECT here

session_start();
$ads = substr($_SESSION['ads'], 1);
$ads = str_replace(",", "','", $_SESSION['ads']);

In the SELECT is that

AND id NOT IN ('".$ads."')

But sometimes, i dont know why, he save two items or something... i dont find out why, cause the SELECT is LIMIT 1

Any ideas how to do that or is there a mysql function?

For any reason, the script is load two times the SELECT or something This is the code:

$query = "SELECT * FROM ads ORDER BY RAND() LIMIT 1;";
while($row = $result->fetch_assoc()) {
    $adid = $row["id"];
    $_SESSION['ads'][] = $adid;
}
echo 'adid: '. $adid;
print_r($_SESSION['ads']);

There he print like that:

Array
(
    [0] => 6
    [1] => 3
)

I dont know, why he puts 2 in there. Cause the echo of the $adid shows only one!

689

Answer

Solution:

Try something like this maybe :

1/ Create a new session and create an empty array :

session_start();

if (empty($_SESSION['ads']))
    $_SESSION['ads'] = array();

2/ Do you select and add the id in this array :

if (empty($_SESSION['ads']) {
    // 1. Do your select with no where condition 

    // 2. Fetch the result
    $data = /* result of the select */;

    // 3. Add the selected id in your session array
    $_SESSION['ads'][] = $data['id'];

} else {
    // 1. Do your select but with a WHERE condition
    $query = "SELECT...
              WHERE id NOT IN ( " . implode( "', '" , $_SESSION['ads'] ) . " )";

    // 2. Fetch the result
    $data = /* result of the select */;

    // 3. Add the selected id in your session array
    $_SESSION['ads'][] = $data['id'];

}

Is it what you are looking for?

People are also looking for solutions to the problem: wordpress - Why the php files weren't executed by cron job?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.