Jump to content

How do I show data from sql in php


Zillathegod

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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");
    ?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.