#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    21
    Rep Power
    0

    cant get all data I query from MySQL to web page by using PHP


    Help, I cant get all data I query from MySQL to my web page by using PHP

    MySQL Database created like this.

    CREATE TABLE hw ( id tinyint(4) DEFAULT '0' NOT NULL AUTO_INCREMENT, mfr varchar(20), product varchar(20), description varchar(255), price varchar(10), PRIMARY KEY (id), UNIQUE id (id));

    // 1 ST Record
    INSERT INTO hw VALUES (1,'Maxtor','Harddrive','20GB 5400RPM','68');

    //2 ND RECORD
    INSERT INTO hw VALUES (2,'Samsung','Harddrive','20GB 5400RPM','68');

    //3 RD RECORD
    INSERT INTO hw VALUES (3,'WesternDigital','Harddrive','20GB 5400RPM','68');

    The product field is where I want to be able to embed a value in the url like this mywebsite.com/hardware.php?product=harddrives or mywebsite.com/hardware.php?product=cpus
    No product name will have a space.

    What I did didnít work and need help
    - First I opened my template I used to create my web page in notepad
    - Second I saved the file naming it hardware.php
    - Third I made a hyperlink from my harddrives image map to point to mywebsite.com/hardware.php?product=harddrives so when you click it, it places it in the url
    - Fourth- he is the php code I used to get to mysql database and display the above information
    <?php



    $db = mysql_connect("localhost", "root", "mypassword");
    $conn = mysql_select_db("hw",$db);

    $result = mysql_query("SELECT * FROM hw",$db);

    while ($row = mysql_fetch_array($result))
    {
    ?>
    Manufacturer: <br><? echo $row["mfr"]; ?><br>
    Product: <br><? echo $row["product"]; ?><br>
    Description: <br><? echo $row["description]; ?><br>
    Price: <br><? echo $row['price']; ?><br><br>
    <?
    }
    ?>

    What I get is a blank page. Please Help, I understand mysql but am not good with php

    So I tried another way, When I use the code below, it only returns the 1 ST RECORD
    <?php
    $db = mysql_connect("localhost", "root", "mypassword");

    mysql_select_db("hw",$db);

    $result = mysql_query("SELECT * FROM hw",$db);

    printf("Manufacturer: %s<br>\n", mysql_result($result,0,"mfr"));

    printf("Product: %s<br>\n", mysql_result($result,0,"product"));

    printf("Description: %s<br>\n", mysql_result($result,0,"description"));

    printf("Price: %s<br>\n", mysql_result($result,0,"price"));

    ?>

    Please Help, thanks in advance
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Posts
    85
    Rep Power
    18

    A couple of ideas


    Hi,
    I didn't look at everything but a few important things...I noticed

    1) ALWAYS test your result or result set from mysql_query to be certain there wasn't an error.

    So you might try something like:

    $result = mysql_query($sql_statement, $db_link);
    if($result){ // or if($result > 0){
    //now put your while statement in here
    while ($row = mysql_fetch_array($result)){
    echo ..... your code here
    }
    }else{
    echo "There was an error with the query";
    }


    2) I might suggesting using your web hostings php mySQL Admin software to view what's happening in the database. You can be certain there is data in the tables

    Hope that helps
    ~Just Say When ~
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    21
    Rep Power
    0

    Hmmm


    Thanks a bunch ,but I used the following code below and it worked

    <?
    $db = mysql_connect("localhost", "root", "mypassword");
    mysql_select_db("hw",$db);

    $result = mysql_query("SELECT * FROM hw",$db);
    while ($row = mysql_fetch_array($result)){
    ?>
    Manufacturer: <br><?=$row['mfr']?><br>
    Product: <br><?=$row['product']?><br>
    Description: <br><?=$row['description']?><br>
    Price: <br><?=$row['price']?><br><br>
    <?
    }
    ?>

IMN logo majestic logo threadwatch logo seochat tools logo