Search code examples
phphtmlmysqlsqlmamp

PHP not displaying anything in MAMP


Im very new to PHP and was trying to get record information to display on a php page using the MAMP server environment.

The attributes are productID, productName, productDescription and productPrice.

I've been able to get really basic PHP to run fine but every time I open this php file, nothing displays, I was wandering if it might be to do with the location I placed the php file. It is currently in htdocs. would appreciate any help.

Thanks

<?php
    //connection to db
    mysql_connect('localhost', 'root', 'root');

    //choosing db
    mysql_select_db(primrose);

    $sql= "SELECT * FROM products";

    $records mysql_query(sql);
?>

<html>
<head>
    <title>Product Data</title>
</head>
<body>
    <table width="600" border="1" cellpadding="1" cellspacing="1">  
        <tr>
            <th>Name</th>
            <th>Description</th>
        </tr>

    <?php
    //loops over each record and for each a new record is set into the variable products
    while(products=mysql_fetch_assoc($records)){
        echo "<tr>";
        echo "<td>".$products['productName']."</td>";
        echo "<td>".$products['productDescription']."</td>";
        echo "</tr>";

    } //end while
    ?>

    </table>
</body>
</html>

Solution

  • This is because (I think) this line is bad:

    mysql_select_db(primrose);
    

    Add quotes around the name of db:

    mysql_select_db("primrose");
    

    Also this line:

    $records mysql_query(sql);
    

    change to

    $records = mysql_query($sql);
    

    and this:

    while(products=mysql_fetch_assoc($records)){
    

    to

    while($products=mysql_fetch_assoc($records)){
    

    NOTE 1:

    • Do not use mysql functions since, they are deprecatid. Use mysqli or PDO instead.

    NOTE 2:

    Let's turn on your error reporting with these two rows in the top of your PHP file:

    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    

    You have a lot of syntax errors. Let's use an IDE to identify them.

    So your final code like this:

    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
     //connection to db
    mysql_connect('localhost', 'root', 'root');
    //choosing db
    mysql_select_db("primrose");
    $sql= "SELECT * FROM products";
    $records = mysql_query($sql);
    ?>
    <html>
    <head>
        <title>Product Data</title>
    </head>
    <body>
        <table width="600" border="1" cellpadding="1" cellspacing="1">
        <tr>
            <th>Name</th>
            <th>Description</th>
        </tr>
        <?php
        //loops over each record and for each a new record is set into the variable products
        while($products=mysql_fetch_assoc($records)){
            echo "<tr>";
            echo "<td>".$products['productName']."</td>";
            echo "<td>".$products['productDescription']."</td>";
            echo "</tr>";
        } //end while
        ?>
        </table>
    </body>
    </html>