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.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jan 2005
    Posts
    62
    Thanks
    1
    Thanked 0 Times in 0 Posts

    passing var for mysql query

    Hello everyone, I have a quick PHP/MySQL syntax question.

    PHP Code:
    <?php                                
        
    if(isset($_GET['id'])){
                    
            
    $id $_GET['id'];
                        
            echo 
    "ID: ".$id;
                    
            
    $result mysql_query("SELECT * FROM classifieds WHERE adid=$id");        
            while (
    $row mysql_fetch_assoc($result)) {
                echo 
    "<img src='images/classifieds/$row[pictureid].jpg'>";
            }
                        
            
    mysql_close($connect);
                        
        } else {
                    
            echo 
    "<br><br><span class='bold'>We apologize, but you have reached this page in error.<br><br>Please use the navigation menu on the left to choose another main page.</span>";
                        
        }
    ?>
    I've confirmed that I am receiving the id variable (i.e., I'm able to echo the correctly passed id). However, the MySQL query is not working. I'm guessing I've missed something small, but any assistance would be appreciated.

    Thanks,

    Ryan

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Depending on your mysql version, you probably just need to wrap your id in quotes:
    PHP Code:
    $result mysql_query("SELECT `pictureID` FROM classifieds WHERE adid='" $id "'"); 
    Since you only need your pictureID, don't bother with selecting all of your fields as well, it will save you some processing time.
    Try that, see if the solution is as simple as that. If not, post back.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    Jan 2005
    Posts
    62
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Nope, not quite that simple I'm afraid. Also, I need to select all variables from the database because they will be used when I further develop this code.

    Thanks,

    Ryan

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    echo '<img src="images/classifieds/'.$row['pictureid'].'.jpg">';

    The variable there is not a constant, so remember to add in the quotes around pictureid.

    Even if you're needing all the fields, do not use *, call the fields singally by their name

  • #5
    New Coder
    Join Date
    Jan 2005
    Posts
    62
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Well, I've tried your suggestion and still had no luck. Also, because I'm still learning the nuances of php/mysql, could you please explain why it's more appropriate to call specific fields?

    Thanks,

    Ryan

  • #6
    New Coder
    Join Date
    Jan 2005
    Posts
    62
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok, I've figured out the syntax problem. Rather, it was a mistake in the particular database entry I was using. Thanks for the assistance, and I'd still like to hear your logic as to why individual fields should be called.

    Thanks,

    Ryan

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by Nightfire View Post
    echo '<img src="images/classifieds/'.$row['pictureid'].'.jpg">';

    The variable there is not a constant, so remember to add in the quotes around pictureid.

    Even if you're needing all the fields, do not use *, call the fields singally by their name
    Good call m8, I didn't notice that one o.O

    Individual field calls reduce the load on the memory that needs to be allocated . Not a big deal in small time applications, but if you only make use of a couple of field entries on a table containing 1000000 records, it is less intensive to store just the fields required instead of all fields for all records. Granted that when the database table loads, it loads all data into memory, but will discard any that is not in use.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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