Zillathegod Posted March 18, 2012 Share Posted March 18, 2012 i am doing a little project but I get stuck on a problem. How do I show the content of my SQL database in PHP or HTML. I want it to show the title at the top, the text in the middle and maybe a price at the bottom. How do I do that? I can't seem to find a tutorial which covers only that, and I don't have the time to search each tutorial for some usefull information right now. And a second thing I want is that it will show the database in one list on a specific webpage. So people can look trough the list and find the thing they want and than click on it so they awill get directed to that item from the database. Quote Link to comment https://forums.phpfreaks.com/topic/259204-how-do-i-show-data-from-sql-in-php/ Share on other sites More sharing options...
cpd Posted March 18, 2012 Share Posted March 18, 2012 We're here to provide help and support on programming. We're not here to write pages upon pages of code for you. What your asking would require a long essay because you haven't even grasped the basics of PHP and MySQL. If you truly want someone to do it for you, I suggest you post in the "Freelancer" forum as this is essentially what your asking. Quote Link to comment https://forums.phpfreaks.com/topic/259204-how-do-i-show-data-from-sql-in-php/#findComment-1328758 Share on other sites More sharing options...
dark k58 Posted March 18, 2012 Share Posted March 18, 2012 This is a code from a site i did: <?php // This block grabs the whole list for viewing $product_list = ""; $sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC"); $productCount = mysql_num_rows($sql); // count the output amount if ($productCount > 0) { while($row = mysql_fetch_array($sql)){ //here you will list the columns $id = $row["id"]; $product_name = $row["product_name"]; $price = $row["price"]; $date_added = strftime("%b %d, %Y", strtotime($row["date_added"])); //this is going to be how the data is viewed $product_list .= "Product ID: $id - <strong>$product_name</strong> - $$price - <em>Added $date_added</em><br />"; } } else { $product_list = "You have no products"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <body> <div> <!-- this here will view the sql data on your site --> <?php echo $product_list; ?> </div> </body> Quote Link to comment https://forums.phpfreaks.com/topic/259204-how-do-i-show-data-from-sql-in-php/#findComment-1328770 Share on other sites More sharing options...
Zillathegod Posted March 18, 2012 Author Share Posted March 18, 2012 THanks for the script. I did get it to work somewhat but it shows that I have no products. I can't figure out what variables are for the database, my database is called "hdmikabels". What should I change in the script so it will work with my database? Quote Link to comment https://forums.phpfreaks.com/topic/259204-how-do-i-show-data-from-sql-in-php/#findComment-1328806 Share on other sites More sharing options...
dark k58 Posted March 18, 2012 Share Posted March 18, 2012 ok to make it easy I will show you the list of pages you need 1s page would be the one that views the list of products (assuming there are products in your database) I've added a line of code to connect the database you just have to change it to your own database. make sure you read the notes i put on the code <?php //this will include the second page so that you can connect to the mysql database, change it to what ever you going to call the second page include "the second page here.php"; // This block grabs the whole list for viewing, here you have to change the table name, I've already changed it to the one you gave me , don't change the "$product_list" $product_list = ""; $sql = mysql_query("SELECT * FROM hdmikabels ORDER BY date_added DESC"); $productCount = mysql_num_rows($sql); // count the output amount if ($productCount > 0) { while($row = mysql_fetch_array($sql)){ //here you will list the columns (make sure you change them to the same column names in your database table) $id = $row["id"]; $product_name = $row["product_name"]; $price = $row["price"]; $date_added = strftime("%b %d, %Y", strtotime($row["date_added"])); //this is going to be how the data is viewed, don't change the "$product_list" $product_list .= "Product ID: $id - <strong>$product_name</strong> - $$price - <em>Added $date_added</em><br />"; } } else { $product_list = "You have no products"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <body> <div> <!-- this here will view the sql data on your site --> <?php echo $product_list; ?> </div> </body> this is going to be the second page so you can connect to the database <?php /* 1: "die()" will exit the script and show an error statement if something goes wrong with the "connect" or "select" functions. 2: A "mysql_connect()" error usually means your username/password are wrong 3: A "mysql_select_db()" error usually means the database does not exist. */ // Place db host name. Sometimes "localhost" but // sometimes looks like this: >> ???mysql??.aserver.net $db_host = "your.host.com"; // Place the username for the MySQL database here $db_username = "database_username"; // Place the password for the MySQL database here $db_pass = "password"; // Place the name for the MySQL database here $db_name = "the name of the database here"; // Run the actual connection here, don't change this mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql"); mysql_select_db("$db_name") or die ("no database"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/259204-how-do-i-show-data-from-sql-in-php/#findComment-1328809 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.