Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    newbie needs help with isset $_GET

    Hello all,

    I'm new with PHP and looking for some help... can someone tell me what am I doing wrong here why can't I make this work, I know I'm doing something wrong,

    Thank you in advance..


    <?php

    //Retrieves data from MySQL

    if(isset($_GET['id']) && $id !=''){
    $id = mysql_real_escape_string($id);
    $sql = "SELECT * FROM bloggers WHERE id = '$id'";

    }else{
    $sql = "SELECT * FROM bloggers ORDER BY id DESC";

    }

    $data = mysql_query($sql) or die(mysql_error());
    //Puts it into an array
    if(mysql_num_rows($data) != 0){
    while($info = mysql_fetch_assoc( $data ))
    {

    //Outputs the image and other data
    Echo '<h1><a href=?id=' . $row['id'] . '>' . $row['title'] .$info['title'] . '</a></h1>';
    Echo "<h4>".$info['timestamp'] . " </h4>";
    Echo "<p> ".$info['body'] . " </p>";

    Echo '<img src=http://www.example.com'.$info['image'] .'> <br/><br/><div class="line"></div>';
    }
    }
    ?>

  • #2
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    Quote Originally Posted by mastertooley View Post
    Hello all,

    I'm new with PHP and looking for some help... can someone tell me what am I doing wrong here why can't I make this work, I know I'm doing something wrong,

    Thank you in advance..


    PHP Code:
    <?php 
            
            
    //Retrieves data from MySQL 
            
            
    if(isset($_GET['id']) && $id !=''){
                
    $id mysql_real_escape_string($id);
                
    $sql "SELECT * FROM bloggers WHERE id = '$id'";
                
            }else{
                
    $sql "SELECT * FROM bloggers ORDER BY id DESC";
                
            }
            
    $data mysql_query($sql) or die(mysql_error());
    //Puts it into an array 
    if(mysql_num_rows($data) != 0){
        while(
    $info mysql_fetch_assoc$data )) 
        { 

            
    //Outputs the image and other data
            
    Echo '<h1><a href=?id=' $row['id'] . '>' $row['title'] .$info['title'] . '</a></h1>';
            Echo 
    "<h4>".$info['timestamp'] . " </h4>";
            Echo 
    "<p> ".$info['body'] . " </p>";

            Echo 
    '<img src=http://www.example.com'.$info['image'] .'> <br/><br/><div class="line"></div>'
        }
    }
    ?>
    can you please put php code in php bbcode
    thanks.. now let's see...

  • #3
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    oh, here

    PHP Code:
    if(isset($_GET['id']) && $id !=''){
                
    $id mysql_real_escape_string($id);
                
    $sql "SELECT * FROM bloggers WHERE id = '$id'";
                
            }else{
                
    $sql "SELECT * FROM bloggers ORDER BY id DESC";
                
            } 
    you need to declare $id variable like this
    PHP Code:
    if(isset($_GET['id']) && $id !=''){
                
    $id $_GET['id'];
                
    $id mysql_real_escape_string($id);
                
    $sql "SELECT * FROM bloggers WHERE id = '$id'";
                
            }else{
                
    $sql "SELECT * FROM bloggers ORDER BY id DESC";
                
            } 
    dunno about other mistakes yet

  • #4
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi funnymoney,

    Thank you for your quick answer, sorry about the ' php bbcode' but I don't know what you mean with that, tried your answer to define the var but still no luck.

    Thank you for your help.

    regards

  • #5
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    How can you check the variable $id if it's not created yet?

    That's like doing this

    PHP Code:
    <?php
    if ($variable == 1)
    {
        
    $variable 1;
        echo 
    'This code doesn\'nt make sense :)';
    }
    ?>
    Create the variable BEFORE checking it.
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  • #6
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    i missed this what Zangeel says.. you can't check $id before you set id

    so this has no sense

    PHP Code:
    if(isset($_GET['id']) && $id !=''){ 
    because you didn't set $id value yet

    this is correct

    PHP Code:
    if(isset($_GET['id'])){ 
    to check weather $id is empty, you need to do that inside isset control, and then output results

  • #7
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    And of course you can just add the empty check, using the $_GET variable.

    PHP Code:
    if ( isset ( $_GET['id'] ) && !empty ( $_GET['id'] ) ) { ... 
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  • #8
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you all for your suggestions and for pointing out my newbie mistakes!! I'm afraid that still no luck I still getting ' index.php?id= ' and I know its a stupid and silly mistake but when one does not have much experience thats what happens.

    Thank you guys for your support and help

    regards

  • #9
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    Here's how I do it.

    PHP Code:
    <?php
    if (isset($_GET['id'])) {

        
    $id = (!empty($_GET['id'])) ? mysql_real_escape_string($_GET['id']) : null;
        
    $sql = ($id === null) ? 'SELECT * FROM `bloggers` ORDER BY `id` DESC' :
            
    'SELECT * FROM `bloggers` WHERE `id` = \'' $id ' ''';

        
    $data mysql_query($sql) or die(mysql_error());
        if (
    mysql_num_rows($data) != 0) {
            while (
    $info mysql_fetch_assoc($data)) {

                echo 
    '<h1><a href=?id=' $info['id'] . '>' $info['title'] . $info['title'] .
                    
    '</a></h1>';
                echo 
    "<h4>" $info['timestamp'] . " </h4>";
                echo 
    "<p> " $info['body'] . " </p>";

                echo 
    '<img src=http://www.example.com' $info['image'] .
                    
    '> <br/><br/><div class="line"></div>';
            }
        }
    }
    ?>
    Optimized the codes a bit, added backsticks. in mysql there are reserved words, so you need to add backsticks to rows and table names.

    EDIT: you used a $row variable instead of $info in a few places, fixed my above code.
    Last edited by Zangeel; 09-07-2009 at 05:12 AM.
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  • #10
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi Zangeel,

    Thanks for your time and your help, unfortunately the code you gave me gives me the following errors,

    Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in

    'SELECT * FROM `bloggers` WHERE `id` = \'' . $id . ' ''';

    if I change to this code:

    'SELECT * FROM `bloggers` WHERE `id` = \'' .$id . ' ';

    gives me a page without any posts.

    Thank you for the help

    regards

  • #11
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    Sorry, forgot to escape the single quote, try this:

    PHP Code:
    <?php
    if (isset($_GET['id'])) {

        
    $id = (!empty($_GET['id'])) ? mysql_real_escape_string($_GET['id']) : null;
        
    $sql = ($id === null) ? 'SELECT * FROM `bloggers` ORDER BY `id` DESC' :
            
    'SELECT * FROM `bloggers` WHERE `id` = \'' $id '\'';

        
    $data mysql_query($sql) or die(mysql_error());
        if (
    mysql_num_rows($data) != 0) {
            while (
    $info mysql_fetch_assoc($data)) {

                echo 
    '<h1><a href=?id=' $info['id'] . '>' $info['title'] . $info['title'] .
                    
    '</a></h1>';
                echo 
    "<h4>" $info['timestamp'] . " </h4>";
                echo 
    "<p> " $info['body'] . " </p>";

                echo 
    '<img src=http://www.example.com' $info['image'] .
                    
    '> <br/><br/><div class="line"></div>';
            }
        }
    }
    ?>
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  • #12
    New to the CF scene
    Join Date
    Aug 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have also same trouble, but thanks for giving the solution.

  • #13
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi Zangeel,

    again Thank you , the code is working now but does not fetch any posts, it gives me a blank index page.... hmmm can't think why??

    Any ideas??

    Thnak you very very much for your help realy appreciated !

    regards

  • #14
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    You're accessing the page like this right

    Code:
    http://site.com?file.php?id=1
    or something similar right?
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  • Users who have thanked Zangeel for this post:

    mastertooley (09-07-2009)

  • #15
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Zangeel,

    I'm on the index page, and it comes up the index page just with the HTML and CSS display but no data .

    I hope I didn't confused you, the code is for the index page, and when i click on the link i want the data to display on its own on the page does this make sense??

    Thank you


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •