Schlo_50 Posted February 5, 2007 Share Posted February 5, 2007 I had a webpage that had code to display some items from a database, i then changed the layout and then copied and pasted the working code into the new layout and now the script won't work.. Im sure it's exactly the same code, but now when i run the script all i get is my layout load up with none of the items in my database displayed! <quote> <? $host = 'correct' ; $user = 'correct' ; $pass = 'correct' ; $db = 'correct' ; $table = 'correct' ; $show_all = 'SELECT * FROM stock_tbl' ; mysql_connect ($host,$user,$pass) or die ( mysql_error ()); mysql_select_db ($db)or die ( mysql_error ()); $result = mysql_query ($show_all) or die ( mysql_error ()); while ($row = mysql_fetch_array ($result)) { echo $row['st_id']. ' - <img src="'. $row['image'] . '" />' . $row['descr'] . ' - ' . $row['price'] . "<br />\n"; } ?> </quote> Anything painfully wrong here? Thanks guys! Quote Link to comment Share on other sites More sharing options...
richardw Posted February 5, 2007 Share Posted February 5, 2007 Dont forget to connect to the DB before executing the select statement. Also I always use "mysql_free_result($result); " after the while loop $show_all = 'SELECT * FROM stock_tbl' ; $result = mysql_query ($show_all) or die ( mysql_error ()); while ($row = mysql_fetch_assoc($result)) { echo $row['st_id']. ' - <img src="'. $row['image'] . '" />' . $row['descr'] . ' - ' . $row['price'] . " \n"; } mysql_free_result($result); ?> Hope this helps, Quote Link to comment Share on other sites More sharing options...
phat_hip_prog Posted February 5, 2007 Share Posted February 5, 2007 Some times if you don't use '<?php' to open it can trip. I don't think your getting an error but I catch em this way.. (uncomment if you want the error) function do_mysql_query($s, $conn) { try { if ( !@ ($res = mysql_query($s, $conn)) ) throw new Exception (mysql_error()); } catch (Exception $e) { //$sret .= 'Could not create table because: ' . $e->getMessage(); return array(-1, $res); } return array(0, $res); } Is php working here at all, if so try outputting intermediaries, or other data from populated tables, etc... Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted February 5, 2007 Author Share Posted February 5, 2007 Still, nothing is being displayed from my database..Im using the code on a separate page with no formatting at all and all that shows is a plain white bg. ??? <?php $host = 'mysql2.freehostia.com' ; $user = 'correct' ; $pass = 'correct' ; $db = 'correct' ; $table = 'stock_tbl' ; $show_all = 'SELECT * FROM stock_tbl' ; $result = mysql_query ($show_all) or die ( mysql_error ()); while ($row = mysql_fetch_assoc($result)) { echo $row['st_id']. ' - <img src="'. $row['image'] . '" />' . $row['descr'] . ' - ' . $row['price'] . " \n"; } mysql_free_result($result); ?> Quote Link to comment Share on other sites More sharing options...
richardw Posted February 5, 2007 Share Posted February 5, 2007 This connection string may be helpful in finding the error, <?php $conn = mysql_connect("correcthost", "correctuser", "correctpassword"); if (!$conn) { echo "Unable to connect to DB: " . mysql_error(); exit; } if (!mysql_select_db("correct")) { echo "Unable to select Database: " . mysql_error(); exit; } $show_all = 'SELECT * FROM stock_tbl' ; $result = mysql_query($show_all ); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo There are no rows found"; exit; } $num_results = mysql_num_rows($result); // echo above $num_results to check rows returned while ($row = mysql_fetch_assoc($result)) { echo $row['st_id']. ' - <img src="'. $row['image'] . '" />' . $row['descr'] . ' - ' . $row['price'] .,"<br>"; } mysql_free_result($result); ?> Keep going! If this does not work, could you post your table structure... Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted February 5, 2007 Author Share Posted February 5, 2007 Ok i pasted the code you used and changed the host, username and password etc to the correct details and now all that appears is the script..lol I wish i wasn't a n00b! (I have enclosed a pic) <url>http://img154.imageshack.us/img154/4446/untitledqx4.jpg</url> Here is what i think you mean by table structures: <url>http://img154.imageshack.us/img154/3129/untitledmf0.jpg</url> Thanks. Quote Link to comment Share on other sites More sharing options...
richardw Posted February 5, 2007 Share Posted February 5, 2007 I am going to try and replicate your table and test it, but how are you storing your images, or is this just a link to your image. Also, have you worked with this server before? I have never seen the code reappear in that manner unless it was pasted into a WYSIWIG screen. What are you using for an editor? Quote Link to comment Share on other sites More sharing options...
richardw Posted February 5, 2007 Share Posted February 5, 2007 Look at the following page: http://66.223.105.215/pictures.php <?php $conn= mysql_connect("localhost" ,"uname" ,"password" ) ; if(!$conn) { echo "Unable to connect to DB: " . mysql_error(); exit; } if (!mysql_select_db("gallery")) { echo " Unable to select gallery Database: " . mysql_error() ; exit; } $sql = "SELECT * FROM stock_tbl"; $result = mysql_query( $sql); if ( mysql_num_rows($result) == 0) { echo "No Documents Available at this time"; } else { $num_results = mysql_num_rows($result) ; echo "There are currently", $num_results, " images available<br>"; while ( $row = mysql_fetch_assoc( $result)) { echo $row["st_id"], " - <img src=", $row["image"], " /><br>", $row["descr"], " - ", $row["price"] ,"<br>"; ?> <?php echo "<br>"; } } mysql_free_result($result); ?> It was created from the following schema: # # Table structure for table `stock_tbl` # CREATE TABLE `stock_tbl` ( `st_id` int(11) NOT NULL auto_increment, `name` varchar(15) NOT NULL default '', `image` varchar(255) NOT NULL default '', `descr` text NOT NULL, `stock` enum('No','Yes') NOT NULL default 'No', `dateadd` date NOT NULL default '0000-00-00', `price` tinytext NOT NULL, KEY `st_id` (`st_id`) ) TYPE=MyISAM AUTO_INCREMENT=2 ; # # Dumping data for table `stock_tbl` # INSERT INTO `stock_tbl` VALUES (1, 'Sloop', 'DSC_2691.JPG', 'Picture from my sailing trip', 'No', '2007-02-05', 'N/A'); Quote Link to comment 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.