0

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>
2
  • 1
    change php.ini and set display_errors to On and error_reporting to E_ALL. read the error. Commented Nov 19, 2014 at 14:49
  • 1
    $records mysql_query(sql); - basic PHP syntax errors, and you're probably running with all debug options turned off. That is NOT a good idea, especially when you're first starting out. Turn on display_errors and error_reporting in your php.ini, then try again. Commented Nov 19, 2014 at 14:54

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

1 Comment

This helped more than I needed, thank you! Will be sure to use error reporting from now on.
1

Try setting error_reporting(E_ALL); to on just after to open'ed PHP. If not, make sure that error reporting is turned on in your php.ini

http://php.net/manual/en/errorfunc.configuration.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.