0

As usual thanks in advance. I've searched and searched for an answer to this but come up with a blank.

I know that the Variable $userId is fine as when echoing it spits out the number 8. However when trying to use it within my Mysql command the array just comes up with a blank.

I took that into account when searching for a NULL entry into the array with the echo "Not"; but even that doesn't seem to echo out. Very strange. Is it simply that I need to change the way the $userId variable is put into the mysql statement?

if($_SESSION['logged_in'] === "logged")
{
    if(isset($_SESSION['id']))
    {
    $userId = $_SESSION['id'];
    echo 'The User ID = ' . $userId . '<br>';
    $musicidselect = mysql_query("SELECT * FROM myMusic WHERE user_id = '.$userId.'") or die(mysql_error());

        while($data = mysql_fetch_array($musicidselect)){
            if($data){
                print_r($data);
            } else{
                echo "Not";
            }
        } 

    }
}

Am I having issues here simply because this file is loaded via jquery into another page?

<script type="text/javascript"> 
    jQuery(document).ready(function($){ 
        $("#performsearch").click(function(){ 
             $("#displayresults").empty(); 
             $( "#displayresults" )
                  .html('<center><img src="http://www.#######.com/wp-content/themes/bigformat/images/ajax-loader.gif">‌​</center>')
                  .load( '/wp-content/themes/bigformat/template-home-search.php'); 
             return false; 
        }); 
     }); 
</script>
Barbara Laird
  • 12,599
  • 2
  • 44
  • 57
bowser404
  • 73
  • 1
  • 7
  • What does your data look like? Is user_id an int or a string? Do you get any errors, or just no rows returned? – Barbara Laird Feb 12 '14 at 16:49
  • Just to add further detail if I trim out the variable and simply enter the number 8 it works, thus why I'm almost 100% the entry of the variable is the problem. – bowser404 Feb 12 '14 at 16:50
  • Hi Barbara user_id is an int. No errors, just no rows returned. Many thanks – bowser404 Feb 12 '14 at 16:52

3 Answers3

3

This is not correct

"SELECT * FROM myMusic WHERE user_id = '.$userId.'"

should be as

"SELECT * FROM myMusic WHERE user_id = '".$userId.'"
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
3

Wrong:

("SELECT * FROM myMusic WHERE user_id = '.$userId.'")

Correct:

("SELECT * FROM myMusic WHERE user_id = " . $userId)
Daniel W.
  • 31,164
  • 13
  • 93
  • 151
1
 $musicidselect = mysql_query("SELECT * FROM myMusic WHERE user_id = $userId") or die(mysql_error());

Since user_id is a number

FYI, variables with " "'s are evaluated, within ' ' are not. See What is the difference between single-quoted and double-quoted strings in PHP? for a nice description.

Community
  • 1
  • 1
Barbara Laird
  • 12,599
  • 2
  • 44
  • 57
  • Thanks for the input, but still nothing echo's. very very strange! – bowser404 Feb 12 '14 at 16:58
  • What does your data look like? – Barbara Laird Feb 12 '14 at 16:59
  • It's generally a better idea to keep variables out of quotations and concat them with a dot. – Daniel W. Feb 12 '14 at 17:09
  • @DanFromGermany - I disagree. I think it makes much more readable code and the performance is similar. Why do you prefer to concat? – Barbara Laird Feb 12 '14 at 17:17
  • @Barbara In the most common code style guides (PECL, Zend, ..), all suggest to concat or at least use `{$var}`. Putting variables in the quotes makes code less readable, IMO. Even the code highlighting on SO (see my answer below) shows this makes way more sense. Many newbies on SO start putting arrays and objects in quoted strings: `$test = "string $var['index'] string $object->object";` which actually throws errors / does not work. Sticking to the same rules in every case makes a way better code style. – Daniel W. Feb 12 '14 at 17:38
  • Am I having issues here simply because this file is loaded via jquery into another page? `` – bowser404 Feb 12 '14 at 17:49
  • @user3279268 - I don't think so, but have you tried going to the page directly? What exactly does your sql statement that works look like? And, what exactly does the variable $userId contain? – Barbara Laird Feb 12 '14 at 17:56
  • 1
    @DanFromGermany - lol I just saw answered a question where someone tried to use an array in "s. I still disagree with using the lowest common denominator to determine best practices, but I see your point. – Barbara Laird Feb 12 '14 at 18:23
  • Maybe I'm doing something really stupid here. It's getting late and my eyes are barely open. To clarify the session contains the id of '8' whilst the myMusic database DOES NOT it only has '34'. Therefore the page should echo I cant find the ID! BUT for some reason it always seems to echo from the if statement above saying 'Ive found the ID'???? – bowser404 Feb 12 '14 at 18:44
  • `if($_SESSION['logged_in'] === "logged") { if(isset($_SESSION['id'])) { $userId = $_SESSION['id']; echo 'The User ID = ' . $userId . '
    '; $musicidselect = mysql_query("SELECT * FROM myMusic WHERE user_id =" .$userId); if (!empty($musicidselect)) { //If ID value in table != Null then //$strSQL = "UPDATE users SET house=$houseaddition WHERE id='$userId'"; echo 'Ive found the ID!'; } else{ echo 'Ive cant find the ID!'; } } }`
    – bowser404 Feb 12 '14 at 18:48
  • 1
    I've finally solved it with the following!!! `// Is user logged in if($_SESSION['logged_in'] === "logged") { if(isset($_SESSION['id'])) { $userId = $_SESSION['id']; echo 'The User ID = ' . $userId . '
    '; $musicidselect = mysql_query("SELECT * FROM myMusic WHERE user_id =" .$userId); if (empty($musicidselect)) { // This ID does not exist. Insert new Row into Table with ID. } else{ // This ID exists. Make adjustments to current data } } }`
    – bowser404 Feb 12 '14 at 19:09