Why won't this script work? :( Easy bits of code damn it!

Discussion in 'PHP' started by mynameisdi, Mar 1, 2009.

  1. #1
    Hey DP PHP experts!

    Any idea on why doesn't this code work?

    Obviously it's because of the <?=$id?> bit.

    But in the rest of the document the <?=$id?> function works well. It's the session of the users ID.

    How do I make it work within the <? ?> php parameters?

    Thanks in advance!
     
    mynameisdi, Mar 1, 2009 IP
  2. qualityfirst

    qualityfirst Peon

    Messages:
    147
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    0
    #2
    You already have PHP on. Can't turn it twice.

    Try this:

    
    ?
    
    
    $data = mysql_query("SELECT * FROM gg_event WHERE id='{$id}' ORDER BY id DESC LIMIT 20");
    while($row = mysql_fetch_array($data))
    {
    
    echo "
    
    
    ".$row[client_name]."
    
    
    
    
    
    ";
    
    }
    
    
    ?> 
    
    PHP:
     
    qualityfirst, Mar 1, 2009 IP
  3. Scripten

    Scripten Peon

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $data = mysql_query("SELECT * FROM gg_event WHERE id=" . $id . " ORDER BY id DESC LIMIT 20");

    This should do the trick :)
     
    Scripten, Mar 1, 2009 IP
  4. mynameisdi

    mynameisdi Banned

    Messages:
    977
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Did you miss the < before ? on purpose ?

    Anyways, it doesn't seem to work.

    I understand that you changed to '{$id}'

    That doesn't cause the page to crash which is progress

    But for some reason it doesn't call out the right results.

    It should call out results that are associated with the user whom that page belongs to.

    Hhmmm
     
    mynameisdi, Mar 1, 2009 IP
  5. mynameisdi

    mynameisdi Banned

    Messages:
    977
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok, that does the same as with the other code (first answer in this thread)

    Hmm... for some reason the $id doesn't associate the query with the user whom the page belongs to.

    It's supposed to give out all of the events that happened to the user.

    <a href="#/gallery/user/<?=$id?>

    - that is how the code for his gallery URL (the users) is generated within that same php document.

    Odd, huh?
     
    mynameisdi, Mar 1, 2009 IP
  6. qualityfirst

    qualityfirst Peon

    Messages:
    147
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    0
    #6
    Ok. Let's begin with a bit of theory.

    PHP is different from html. While regular html can be executed in a .php file, php code can not be executed in a normal .html file. It is possible, but not recommended.

    Any code that you type into a .php page that is not within <?php and ?> will be executed as html, and not as php. Once those two tags are open, everything will be executed as php, even if your code is html and not inside an echo or print function. So you don't need <?=$id?> if it's going between <?php and ?>. You'll need it if it's not going between those two.

    As for the above, I did not mean to miss the <, and it should have been there.
     
    qualityfirst, Mar 1, 2009 IP
  7. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #7
    Assuming the $id value is numeric use

    Or possibly

    but not " inside a query string that is alread enclosed in " "

    and right before that query line insert the following line to make sure the variable is valid to start with:

     
    Colbyt, Mar 1, 2009 IP
    qualityfirst likes this.