Cable Forum

Cable Forum (https://www.cableforum.uk/board/index.php)
-   General IT Discussion (https://www.cableforum.uk/board/forumdisplay.php?f=19)
-   -   PHP - Trying to retrieve a value from Mysql db (https://www.cableforum.uk/board/showthread.php?t=33647782)

funkyCable 26-03-2009 09:06

PHP - Trying to retrieve a value from Mysql db
 
Hi

I have a table in mysql called tbl_picture_link which stores a url string to some pictures not I would like to retieve the string and place <img> around it.

This is the function I have
PHP Code:

function get_image($image_id){
    
//finds and displays the selected image 
        
global $connection
        
$query "SELECT     * 
                  FROM tbl_picture_link
                  where picture_link_id = 
{$image_id}  
                  LIMIT 1"

        
$picture_set mysql_query($query$connection); 
        
confirm_query($picture_set); 
        echo 
"<img scr=\"" $picture_set['picture_link_href'] . "\">" $query."<br />"
    } 

I'm not getting an errors with connection to my db all that its echoed is an img place holder the $query value which is a value sql statement because I copied the output and ran it in phpmyadmin and it returned some the result I was expecting.

Any ideas what could be wrong.

punky 26-03-2009 09:25

Re: PHP - Trying to retrieve a value from Mysql db
 
You're missing out a step. Even though you have one row, you still need to fetch the result, not just link to the query. So something like this:

PHP Code:

function get_image($image_id){
    
//finds and displays the selected image 
        
global $connection
        
$query "SELECT     * 
                  FROM tbl_picture_link
                  where picture_link_id = 
{$image_id}  
                  LIMIT 1"
;

        
//get query result 
        
$query_result mysql_query($query$connection);
        
//fetch result as an array
        
$picture_set mysql_fetch_array($query_result);
 
        
confirm_query($picture_set); 
        echo 
"<img scr=\"" $picture_set['picture_link_href'] . "\">" $query."<br />"
    } 

Note as you only have one row returned, you only call the mysql_fetch_array once. If you wanted to do more than one row, you need to do a while loop or something to keep calling mysql_fetch_array and get the new rows.

bmxbandit 26-03-2009 09:26

Re: PHP - Trying to retrieve a value from Mysql db
 
This won't help...
Quote:

<img scr
I'm a little confused as to the problem...does $picture_set contain what you expect it to?

edit: heh, ok. what he said ^^^ :D

funkyCable 26-03-2009 10:10

Re: PHP - Trying to retrieve a value from Mysql db
 
oh yes now I see. Thanks its working just one more question how can I use a relative url in a img tag?

This is my web folders are constructed my root is htdocs i then have a websites folder which has another our_website folder which has all my php files under it. I then have /images/pix folder under our_website my images are displaying if I use <img src"/websites/our_website/images/familypix/image.jpeg"/> Is there a way I could shorten this to something like /images/familypix/image.jpeg

So if I ever move/rename the our_website folder it will not affect it.

punky 26-03-2009 10:18

Re: PHP - Trying to retrieve a value from Mysql db
 
You can, just drop the first /

like:

images/familypix/image.jpeg

funkyCable 26-03-2009 10:24

Re: PHP - Trying to retrieve a value from Mysql db
 
thanks working 100%, a shiney is on its way.


All times are GMT. The time now is 01:40.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
All Posts and Content are © Cable Forum