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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Variables in MYSQL query in imagestring

    Hey,

    I am making a signature service for a group of peers where they will put

    [img]www.mydomain.com/sig.php?id=1[img] in their signature. The id=1 is supposed to fetch the information for the row where id=1, displaying the fields associated.

    Here's the code:

    Code:
    <?
    $db_host = "localhost";
    $db_user = "signatures";
    $db_pass = "******";
    $db_name = "signatures";
    $db = mysql_connect($db_host,$db_user,$db_pass);
    mysql_select_db ($db_name) or die ("Cannot Connect To Database");
    
    $id = $_GET['id'];
    
    
    $query = 'SELECT `name` FROM `information` WHERE id = '$id''
    $result = mysql_query($query); 
    
    
    header("Content-type: image/gif");
    $im = imagecreatefromgif("sig.gif");
    $color = imagecolorallocate($im, 0, 0, 0);
    imagestring($im, 5, 200, 1, $result, $color);
    imagegif($im);
    imagedestroy($im);
    ?>
    The issue seems to be here, but ofcourse I am not totally sure.

    Code:
    $query = 'SELECT `name` FROM `information` WHERE id = '$id''
    $result = mysql_query($query);
    I've tried like 10 different queries, no ', slashes and whatnot. No result, different weird messages printed on my picture ("Resource ID #2" and "Array"). They all output junk I can't use. What I need is for it to output the "name" field associated to id 1 in the database if I type www.yourdomain.com/sig.php?id=1

    If you wish to try out the code, try using this image for optimal testing since that's my planned size:



    Oh btw, rendering the ID number received by the GET is properly onto the picture when I put the variable in the imagestring.
    Last edited by ItsNotRudy; 11-02-2009 at 01:32 PM.

  • #2
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    Code:
    $query = 'SELECT `name` FROM `information` WHERE id = '$id''
    $result = mysql_query($query);
    mysql_query returns a Resource identifier, as evidenced by your Resource ID#2 'junk'

    You need to use this to retrieve the records you request. ie:
    PHP Code:
    $query 'SELECT `name` FROM `information` WHERE id = '$id''
    $result mysql_query($query);

    if (
    $result)
    {
        
    $row mysql_fetch_assoc($result);
        echo 
    $row['name'];   // echo it or put it in a variable for later use

    John

  • #3
    New to the CF scene
    Join Date
    Nov 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, I resolved it later that night though, this is what I ended up with

    Code:
    <?php
    $db_host = "localhost";
    $db_user = "signatures";
    $db_pass = "******";
    $db_name = "signatures";
    $db = mysql_connect($db_host,$db_user,$db_pass);
    mysql_select_db ($db_name) or die ("Cannot Connect To Database");
    
    $id = $_GET['id'];
    $id = stripslashes($id);
    $id = mysql_real_escape_string($id);
    
    
    
    if (is_numeric($id)) {
       $query = "SELECT `name` FROM `information` WHERE id = $id";
       $result = mysql_query($query);
    
       if (mysql_num_rows($result) > 0) {
          $row = mysql_fetch_array($result);
       
          header("Content-type: image/gif");
          $im = imagecreatefromgif("sig.gif");
          $color = imagecolorallocate($im, 0, 0, 0);
          imagestring($im, 5, 200, 1, $row['name'], $color);
          imagegif($im);
          imagedestroy($im);
       }
    }
    ?>


  •  

    Posting Permissions

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