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.

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.