Hi everyone,
I am having some trouble with a mysql select query not working correctly.

The query I am trying to run is

"SELECT * FROM cards2 WHERE cardid NOT LIKE '".$_SESSION['clubsId'][0]."' "

I have also tried

mysql_query("
SELECT * FROM cards2 WHERE NOT (cardid =".$_SESSION['clubsId'][0]." || ".$_SESSION['clubsId'][1]."

Would appreciate some help as I am not sure how to get this working...
Thanks in advance.

Dani AI

Generated

A few focused points that tie the thread together and show a reliable fix path.

and were right to push session debugging: the most common causes here are (a) session not started on the exact script doing the query, or (b) session state lost between requests. Confirm the session is active (and started before any output), compare session IDs across pages, and log the full session structure for the problem page so the stored arrays are visible and identical to what is echoed elsewhere:

if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}
error_log('SID: '.session_id().' | SESSION: '.var_export($_SESSION, true));

SQL-wise, NOT LIKE without wildcards behaves like a negated equality test but is the wrong tool for excluding multiple IDs. If the intention is “exclude these cardid values,” build a NOT IN clause and use parameterized queries (avoid mysql_*). Example with PDO:

$exclude = $_SESSION['clubsId'] ?? [];
if ($exclude) {
    $placeholders = implode(',', array_fill(0, count($exclude), '?'));
    $sql = "SELECT * FROM cards2 WHERE cardid NOT IN ($placeholders)";
    $stmt = $pdo->prepare($sql);
    $stmt->execute($exclude);
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
}

Also check the session-building code for simple bugs that commonly slip by: uninitialized session arrays before using array_push, an undefined $query variable (the snippet showing $result = mysql_query($query)), and suspicious WHERE clauses like id='1' that may not reflect the intended filter (e.g., selecting by suit). Finally, migrate away from deprecated mysql_* calls to mysqli or PDO, sanitize/cast session values (e.g., array_map('intval', ...) for numeric IDs), and remove ad-hoc debugging once the issue is resolved.

Recommended Answers

All 10 Replies

What is the error message? Does $_SESSION['clubsId'][0] have any value? Have you tried to display the query and test it in phpmyadmin:

die("SELECT * FROM cards2 WHERE cardid NOT LIKE '{$_SESSION['clubsId'][0]}'");

It is usually a good practice to test for variable values before using them in queries:

if(isset($_SESSION['clubsId'][0])) {
    mysql_query("SELECT * FROM cards2 WHERE cardid NOT LIKE '{$_SESSION['clubsId'][0]}'");
} else {
    die('Value does not exist!');
}

Hi Thanks for you reply Broj1 -
The only error message I get is the die error -

I will give your second suggestion a run as I know the session var is set as I can see it on the screen,

When I run your script, I am getting the error value does not exist,
But I can clearly see the value on the page...

Can you put this code before the code above:

die(print_r($_POST, 1));

It will display the contents of the $_POST array. Please post it here.

Hi, Sorry for the late reply,

I have added this to my page, but all I get is an empty array ()

    die(print_r($_POST, 1));
    if(isset($_SESSION['clubsId'][0])) {
    mysql_query("SELECT * FROM cards2 WHERE cardid NOT LIKE '{$_SESSION['clubsId'][0]}'");
    } else {
    die('Value does not exist!');
    }

I think he meant to ask you to print_R $_SESSION to see what's in your session, the post data has nothing to do with your session data.

die(print_r($_SESSION, 1));

That will print out the structure of your session array so we can see how it's formed

I have added this to my page, but all I get is an empty array ()

This means that the $_SESSION array is empty. Do you have the session_start() function on top of your script? What is the logic to set up the session (populate the $_SESSION array)?

Hi Broj1... I have session start(); at the top of my page,
When I echo out

<?php
        echo $_SESSION['clubsId'][0]." ";   
        echo $_SESSION['clubsId'][1]." ";   
        echo $_SESSION['clubsId'][2]." ";
        echo $_SESSION['diamondsId'][0]." ";    
        echo $_SESSION['diamondsId'][1]." ";    
        echo $_SESSION['diamondsId'][2]." ";
        echo $_SESSION['heartsId'][0]." ";  
        echo $_SESSION['heartsId'][1]." ";  
        echo $_SESSION['heartsId'][2]." ";
        echo $_SESSION['spadesId'][0]." ";  
        echo $_SESSION['spadesId'][1]." ";  
        echo $_SESSION['spadesId'][2]." ";
        ?>

This gives me the cardid number of the playing card & displays the number of cards on page.

I am running a query on my playing card database to select three random playing card from each suit, clubs, diamonds, hearts & spades.

I am using array_push to populate the different sessions

            <?php 
                    ////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    // Select Three Clubs
                    $sth = mysql_query("SELECT id, card, suit, value, cardid, cardimg FROM cards2 where id='1' ORDER BY RAND() LIMIT 3");
                    ////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    // result
                    $result = mysql_query($query) or die ("no query for clubs");                    
                    ////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    // array
                    $clubsRemoved = array();

                    while($clubsRemoved = mysql_fetch_array($sth)) {

                        //foreach($rows as $key => $value)
                        foreach($clubsRemoved as $key => $value){ 

                            }
            //echo $clubsRemoved[0]." ".$clubsRemoved[1]." ".$clubsRemoved[2]." ".$clubsRemoved[3]." ".$clubsRemoved[4]." ".$clubsRemoved[5]."<br />";
            array_push($_SESSION['cardsRemoved'], $clubsRemoved['0'], $clubsRemoved['1'], $clubsRemoved['2']);
            array_push($_SESSION['clubsRemoved'], $clubsRemoved['0'], $clubsRemoved['1'], $clubsRemoved['2']);
            array_push($_SESSION['clubsValue'], $clubsRemoved['3']);
            array_push($_SESSION['clubsId'], $clubsRemoved['4']);
            array_push($_SESSION['clubsShow'], $clubsRemoved['5']);
                        }
                    ////////////////////////////////////////////////////////////////////////////////////////////////////////////            

            ?>

OK, but do you have session_start() on each and every script that uses the session (including the script that is making troubles)? You also have to make sure there is no HTML sent before the session_start() statement otherwise session will not get initialized.

When I run your script, I am getting the error value does not exist,

This means that $_SESSION['clubsId'][0] is not set (does not exist) so you can not use it in your query.

Thanks Broj1...

I will look back over what I have, as there is something going a miss somewhere

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.