mark103 Posted August 7, 2011 Share Posted August 7, 2011 Hi all, I am working on the php script as I am using this for reading the images from each rows on mysql database. There are two rows that I have store in mysql already, so there is a little problem as I want you to take a look at this current code: <?php session_start(); define('DB_HOST', 'localhost'); define('DB_USER', 'mydbusername'); define('DB_PASSWORD', 'mydbpassword'); define('DB_DATABASE', 'mydbusername'); $username = (int)$_GET['username']; $errmsg_arr = array(); $errflag = false; $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } function clean($var) { return mysql_real_escape_string(strip_tags($var)); } $qrytable1="SELECT images FROM images_list WHERE username=$username"; $result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error()); while ($row = mysql_fetch_array($result1)) { $details = getimagesize($row['images']); header ('Content-Type: ' . image_type_to_mime_type($details[2])); echo file_get_contents($row['images']); } ?> There are two hotlinks images that I have stored in each row. When I entered the id at the end of the url, it will search the id in the database while it will look for the images through with the column name, the id and the username. The first image did printed on my php page when I entered the id, but the random images did not print out when I enter with different id. Do you know where the trouble is and what I need to make some changes? Quote Link to comment https://forums.phpfreaks.com/topic/244144-print-each-image-when-enter-id/ Share on other sites More sharing options...
AyKay47 Posted August 7, 2011 Share Posted August 7, 2011 1. why are you casting a username into an integer? is it a number string? 2. This will grab whatever images you have with the username of $username, seems that you are going about it correctly, I don't really understand the issue here and what you mean by "random images not showing"..? Quote Link to comment https://forums.phpfreaks.com/topic/244144-print-each-image-when-enter-id/#findComment-1253825 Share on other sites More sharing options...
mark103 Posted August 7, 2011 Author Share Posted August 7, 2011 well I have a mysql database where I am casing the username into an integer, so it will find the images whatever i stored in mysql. However, I do realised that I wouldn't need to as long the id and the images coumn name need to be necessary. Sorry, I meant that the image did not changes when I enter with different id at the end of the url. Any idea what I need to do to make some changes in order to get the images displaying with the correct id? Quote Link to comment https://forums.phpfreaks.com/topic/244144-print-each-image-when-enter-id/#findComment-1253828 Share on other sites More sharing options...
AyKay47 Posted August 7, 2011 Share Posted August 7, 2011 do you mean that the query does not change if you enter another username? are your usernames actual strings like "username" or string numbers like "1"? Quote Link to comment https://forums.phpfreaks.com/topic/244144-print-each-image-when-enter-id/#findComment-1253832 Share on other sites More sharing options...
mark103 Posted August 7, 2011 Author Share Posted August 7, 2011 yes, the query does not changes when I enter with another string numbers like "2". Do you know how to fix this? Quote Link to comment https://forums.phpfreaks.com/topic/244144-print-each-image-when-enter-id/#findComment-1253834 Share on other sites More sharing options...
AyKay47 Posted August 7, 2011 Share Posted August 7, 2011 if you do not receive any mysql errors, i would try outputting your sql to see what its doing.. $qrytable1="SELECT images FROM images_list WHERE username=$username"; print $qrytable1; Quote Link to comment https://forums.phpfreaks.com/topic/244144-print-each-image-when-enter-id/#findComment-1253836 Share on other sites More sharing options...
mark103 Posted August 7, 2011 Author Share Posted August 7, 2011 here's the output that it have printed on my php page: SELECT id, images FROM images_list WHERE username=0 Any idea? Quote Link to comment https://forums.phpfreaks.com/topic/244144-print-each-image-when-enter-id/#findComment-1253865 Share on other sites More sharing options...
Pikachu2000 Posted August 7, 2011 Share Posted August 7, 2011 The username is supposed to be an integer? Quote Link to comment https://forums.phpfreaks.com/topic/244144-print-each-image-when-enter-id/#findComment-1253866 Share on other sites More sharing options...
mark103 Posted August 7, 2011 Author Share Posted August 7, 2011 yes it supposed to be so please can you help? Quote Link to comment https://forums.phpfreaks.com/topic/244144-print-each-image-when-enter-id/#findComment-1253894 Share on other sites More sharing options...
Pikachu2000 Posted August 7, 2011 Share Posted August 7, 2011 Have you tried pasting the query string into phpMyAdmin to see if there are any results returned? Quote Link to comment https://forums.phpfreaks.com/topic/244144-print-each-image-when-enter-id/#findComment-1253898 Share on other sites More sharing options...
mark103 Posted August 8, 2011 Author Share Posted August 8, 2011 I have already stored the query strings into mysql which I did get the results returned, so I don't know what to do and how to correct the situation as I am getting. This is what I am trying to do: enter the id at the end of the url something like this: http:www.mysite.com/images.php?id=123 each id i enter at the end of the url, it will enter to mysql database find the id in mysql and find the header called images print out each random image each time i enter the different id on the end of the url do you know how to do this using with this code? <?php session_start(); define('DB_HOST', 'localhost'); define('DB_USER', 'mydbusername'); define('DB_PASSWORD', 'mydbpassword'); define('DB_DATABASE', 'mydbusername'); $username = (int)$_GET['username']; $errmsg_arr = array(); $errflag = false; $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } function clean($var) { return mysql_real_escape_string(strip_tags($var)); } $qrytable1="SELECT images FROM images_list WHERE username=$username"; $result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error()); while ($row = mysql_fetch_array($result1)) { $details = getimagesize($row['images']); header ('Content-Type: ' . image_type_to_mime_type($details[2])); echo file_get_contents($row['images']); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/244144-print-each-image-when-enter-id/#findComment-1254623 Share on other sites More sharing options...
AyKay47 Posted August 8, 2011 Share Posted August 8, 2011 I believe something like this is what you will want... if(isset($username)){ $sql = "SELECT images FROM images_list WHERE username = '$username'"; $result = mysql_query($sql) or die("Invalid query: " . mysqli_error()); header("Content-type: image/jpeg"); $im = imagecreatefromstring(mysql_result($result, 0)); imagejpeg($im); imagedestroy($im); } this is assuming that the image is a jpeg, but this is to give you the idea... Quote Link to comment https://forums.phpfreaks.com/topic/244144-print-each-image-when-enter-id/#findComment-1254630 Share on other sites More sharing options...
mark103 Posted August 9, 2011 Author Share Posted August 9, 2011 thanks, i have a bit of trouble with the code. I cannot be able to get the output images as it will output the url link on my php page. <?php session_start(); define('DB_HOST', 'localhost'); define('DB_USER', 'mydbusername'); define('DB_PASSWORD', 'password'); define('DB_DATABASE', 'mydbname'); $username = (int)$_GET['username']; $errmsg_arr = array(); $errflag = false; $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } function clean($var) { return mysql_real_escape_string(strip_tags($var)); } $qrytable1="SELECT images FROM user_channel_list WHERE username=$username"; $result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error()); if(isset($username)){ $sql = "SELECT id, images FROM images_list WHERE username = '$username'"; $result = mysql_query($sql) or die("Invalid query: " . mysqli_error()); header("Content-type: image/jpeg"); $im = imagecreatefromstring(mysql_result($result, 0)); imagejpeg($im); imagedestroy($im); } ?> any idea how to fix this? Quote Link to comment https://forums.phpfreaks.com/topic/244144-print-each-image-when-enter-id/#findComment-1254636 Share on other sites More sharing options...
AyKay47 Posted August 9, 2011 Share Posted August 9, 2011 how are you storing you images...? Quote Link to comment https://forums.phpfreaks.com/topic/244144-print-each-image-when-enter-id/#findComment-1254641 Share on other sites More sharing options...
mark103 Posted August 9, 2011 Author Share Posted August 9, 2011 im just hotlinking them. Quote Link to comment https://forums.phpfreaks.com/topic/244144-print-each-image-when-enter-id/#findComment-1254643 Share on other sites More sharing options...
AyKay47 Posted August 9, 2011 Share Posted August 9, 2011 while ($row = mysql_fetch_array($result1)) { $image = $row['images'] $details = getimagesize($image); header ('Content-Type: ' . image_type_to_mime_type($details[2])); readfile($image); //if fopen_wrappers is enabled } } you will want to put the output php code into a separate php file...like "test.php", then your html will look like <img src='test.php' /> untested.. Quote Link to comment https://forums.phpfreaks.com/topic/244144-print-each-image-when-enter-id/#findComment-1254647 Share on other sites More sharing options...
mark103 Posted August 10, 2011 Author Share Posted August 10, 2011 thanks, but there is a problem. i have got an error: Parse error: syntax error, unexpected T_VARIABLE in /home/myusername/public_html/images.php on line 32 here is the line 32: $details = getimagesize($image); any idea how to correct the error? Quote Link to comment https://forums.phpfreaks.com/topic/244144-print-each-image-when-enter-id/#findComment-1255621 Share on other sites More sharing options...
AyKay47 Posted August 10, 2011 Share Posted August 10, 2011 yeah i forgot to end the line before that one with a semi colon " ; " $image = $row['images']; //add semicolon Quote Link to comment https://forums.phpfreaks.com/topic/244144-print-each-image-when-enter-id/#findComment-1255624 Share on other sites More sharing options...
mark103 Posted August 10, 2011 Author Share Posted August 10, 2011 that is okay. i have added semi colon on the image = $row, however i am getting another error: Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 here's the current code: <?php session_start(); define('DB_HOST', 'localhost'); define('DB_USER', 'mydbusername'); define('DB_PASSWORD', 'mypassword'); define('DB_DATABASE', 'mydbname'); $errmsg_arr = array(); $errflag = false; $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } function clean($var) { return mysql_real_escape_string(strip_tags($var)); } $qrytable1="SELECT images FROM image_list WHERE username=$username"; $result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error()); while ($row = mysql_fetch_array($result1)) { $image = $row['images']; $details = getimagesize($image); header ('Content-Type: ' . image_type_to_mime_type($details[2])); readfile($image); } ?> what's wrong? Quote Link to comment https://forums.phpfreaks.com/topic/244144-print-each-image-when-enter-id/#findComment-1255626 Share on other sites More sharing options...
AyKay47 Posted August 10, 2011 Share Posted August 10, 2011 you dont define $username before using it in your query.. Quote Link to comment https://forums.phpfreaks.com/topic/244144-print-each-image-when-enter-id/#findComment-1255627 Share on other sites More sharing options...
mark103 Posted August 10, 2011 Author Share Posted August 10, 2011 oh sorry, i have forgot to define the $username. however, when i input the url address with random id at the end something like this: http://www.mysite.com/myscript.php?id=0 http://www.mysite.com/myscript.php?id=1 ...etc i have got the same output images, do you know why and how to fix this? Quote Link to comment https://forums.phpfreaks.com/topic/244144-print-each-image-when-enter-id/#findComment-1255630 Share on other sites More sharing options...
AyKay47 Posted August 11, 2011 Share Posted August 11, 2011 should produce a different image if the images are dependent on the integer, what does a "view source" show in your borwser? Quote Link to comment https://forums.phpfreaks.com/topic/244144-print-each-image-when-enter-id/#findComment-1255640 Share on other sites More sharing options...
mark103 Posted August 11, 2011 Author Share Posted August 11, 2011 i think i have found the solution. I have changed the id define instead of using the username define. every image have changes when i input each different id. however, this is the last thing i need before this will get solve. Can you please help me how to set the html tag for this line? echo "<p id='images'>", readfile($image); I am getting the url output on my php page. So once we set the html tag, this will be solve. Quote Link to comment https://forums.phpfreaks.com/topic/244144-print-each-image-when-enter-id/#findComment-1255641 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.