co.ador Posted May 21, 2009 Share Posted May 21, 2009 I am having some trouble pathing images from Mysql database o the browser... Help how can I properly path an image in a field table? Quote Link to comment https://forums.phpfreaks.com/topic/159174-need-help-dont-know-how-to-stores-images-in-a-database-table-field/ Share on other sites More sharing options...
roopurt18 Posted May 21, 2009 Share Posted May 21, 2009 You create a script that takes a get parameter, such as: image.php?id=47 <?php // image.php $id = db_sanitize( $_GET['id'] ); $stmt = "select img_blob from image_table where id={$id}"; // execute and fetch such that $row is an array of the database fields header( 'Content-Type: image/jpg' ); // or image/gif, image/jpeg, image/png, etc header( 'Content-Size: ' . strlen( $row->img_blob ) ); // maybe set some other headers echo $row->img_blob; // AND MAKE SURE THERE IS NO LEADING OR TRAILING WHITE SPACE IN THIS FILE ?> Quote Link to comment https://forums.phpfreaks.com/topic/159174-need-help-dont-know-how-to-stores-images-in-a-database-table-field/#findComment-839441 Share on other sites More sharing options...
co.ador Posted May 21, 2009 Author Share Posted May 21, 2009 You gave me this You create a script that takes a get parameter, such as: image.php?id=47 <?php // image.php $id = db_sanitize( $_GET['id'] ); $stmt = "select img_blob from image_table where id={$id}"; // execute and fetch such that $row is an array of the database fields header( 'Content-Type: image/jpg' ); // or image/gif, image/jpeg, image/png, etc header( 'Content-Size: ' . strlen( $row->img_blob ) ); // maybe set some other headers echo $row->img_blob; // AND MAKE SURE THERE IS NO LEADING OR TRAILING WHITE SPACE IN THIS FILE ?> To embed it around the script I have, but when I embed it around it shows the following error Fatal error: Call to undefined function db_sanitize() in C:\wamp\www\store\shoes\menu2.php on line 48 My code goes as follow <?php $sql = 'SELECT id,Subject,image FROM menusubjects;'; $result = mysql_query($sql); $subjects = array(); if($result && mysql_num_rows($result)!=0) { echo '<ul id="nav-categories">'; while($row = mysql_fetch_assoc($result)) { $subjects[] = $row; $uri = 'example1.php?subject='.urlencode($row['id']); $class = !is_null($cat) && $cat==$row['id']?' class="selected"':''; echo "\t",'<li',$class,'><a href="',$uri,'">',$row['Subject'].'</a>'; if($submenu==false && !is_null($cat) && $cat == $row['id']) { // line 58 $sql2 = 'SELECT id,shoename FROM regularmenu WHERE menusubject_id = '.mysql_real_escape_string($cat).';'; $result2 = mysql_query($sql2); if($result2 && mysql_num_rows($result2)!=0) { echo "\n\t\t",'<ul class="submenu">',"\n"; while($row2 = mysql_fetch_assoc($result2)) { $uri2 = $uri.'&menu='.urlencode($row2['id']); $class2 = !is_null($prod) && $prod==$row2['id']?' class="selected"':''; echo "\t\t\t",'<li',$class,'><a href="',$uri2,'">',$row2['shoename'],'</a></li>',"\n"; } echo "\t\t",'</ul> <!-- end of ul.submenu -->',"\n"; } $submenu = true; } echo '</li>',"\n"; } echo '</ul> <!-- end of ul#nav-categories -->',"\n "; } // drop in thumbnail menu if(!empty($subjects)) { echo '<ul id="nav-subjects-thumbs">',"\n"; foreach($subjects as $sub) { $uri = 'example1.php?subject='.urlencode($sub['id']); $src = $sub['image']; $width = 40; $height = 40; $alt = $sub['Subject']; echo "\t",'<li><a href="',$uri,'"><img scr="',$src,'" width="',$width,'" height="',$height,'" alt="',$alt,'" /></a></li>',"\n"; } echo '</ul>',"\n"; } ?> This script has two queries on top and below a foreach loop at the end where the image value is inside of a array variable called $sub, Notice that the foreach loops uses the first query on top of the script to obtain the values of the array. How can I embed your suggestion script around the script I have so it can show up the pictures? Quote Link to comment https://forums.phpfreaks.com/topic/159174-need-help-dont-know-how-to-stores-images-in-a-database-table-field/#findComment-839485 Share on other sites More sharing options...
roopurt18 Posted May 21, 2009 Share Posted May 21, 2009 There is no PHP function called db_sanitize(), which is why you receive the error. I included that function as a reminder to you that you have to sanitize user input (stuff from $_POST, $_GET, etc) before you stick it in your database. How you sanitize data will depend on the database back end you are using. image.php is a separate script you have to write, you can not wrap your existing script inside of it. You can not embed images into the HTML page. Images in HTML are created with an img-tag that has a src-attribute that points at a URL. The URL it points at should be image.php, which pulls the image data from the database and dumps it out. Quote Link to comment https://forums.phpfreaks.com/topic/159174-need-help-dont-know-how-to-stores-images-in-a-database-table-field/#findComment-839492 Share on other sites More sharing options...
co.ador Posted May 21, 2009 Author Share Posted May 21, 2009 i have create the script, now do i have specify the images I want to show up in the array? how will my script will now which images to pull up? Quote Link to comment https://forums.phpfreaks.com/topic/159174-need-help-dont-know-how-to-stores-images-in-a-database-table-field/#findComment-839500 Share on other sites More sharing options...
paparts Posted May 22, 2009 Share Posted May 22, 2009 When I use images i just put all the images on a folder and on the database I just store the directory and the file name. One example in php is $db_path = 'images/' . $filename; <img src="$db_path" /> Quote Link to comment https://forums.phpfreaks.com/topic/159174-need-help-dont-know-how-to-stores-images-in-a-database-table-field/#findComment-839735 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.