0

hi I am trying to execute the following code on cloud 9 :

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Test | Products</title>
    </head>

    <body>
        <?php
            $con=mysqli_connect(0.0.0.0,"ritikasahay","","trydb");

            if (mysqli_connect_errno()) {
                echo "Failed to connect to MySQL: " . mysqli_connect_error();
            }

            $result = mysqli_query($con,"SELECT * FROM veg");

            echo "<table border='1'>
            <tr>
                <th>Name</th>
                <th>Price</th>
                <th>Image</th>
            </tr>";

            while($row = mysqli_fetch_array($result)) {
                echo "<tr>";
                echo "<td>" . $row['name'] . "</td>";
                echo "<td>" . $row['price'] . "</td>";
                echo "<td>" . $row['image'] . "</td>";
                echo "</tr>";
            }

            echo "</table>";

            mysqli_close($con);
            ?>
    </body>
</html>

when i am executing this code, i am getting just the following php code as result instead of the table contents:

Name Price Image "; while($row = mysqli_fetch_array($result)) { echo ""; echo "" . $row['name'] . ""; echo "" . $row['price'] . ""; echo "" . $row['image'] . ""; echo ""; } echo ""; mysqli_close($con); ?>

can someone tell me whre i am going wrong?

Simpsons
  • 476
  • 1
  • 7
  • 17
ritika
  • 61
  • 1
  • 10

2 Answers2

0

chang this command:

$con=mysqli_connect(0.0.0.0,"ritikasahay","","trydb");

To like this:

$con=mysqli_connect("0.0.0.0","ritikasahay","","trydb");
  • yes, after changing to your syntax, i am getting the following resuly:Failed to connect to MySQL: Access denied for user 'ritikasahay'@'localhost' (using password: NO) Name Price Image – ritika Sep 23 '14 at 07:44
0

Table display once instead of multiple times

<!DOCTYPE html>
<html lang="en">
    <head>

        <title>Test | Products</title>

    </head>
    <body>
        <?php
$con=mysqli_connect("0.0.0.0","ritikasahay","","trydb");

if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM veg");

//create table
$table = "<table border='1'>
<tr>
<th>Name</th>
<th>Price</th>
<th>Image</th>
</tr>";

while($row = mysqli_fetch_array($result)) {
  //bind rows
  $table .= "<tr>";
  $table .= "<td>" . $row['name'] . "</td>";
  $table .= "<td>" . $row['price'] . "</td>";
  $table .= "<td>" . $row['image'] . "</td>";
  $table .= "</tr>";
}

//close table
$table .= "</table>";

//display table
echo $table;

mysqli_close($con);
?>
    </body>
</html>
Sundar
  • 4,580
  • 6
  • 35
  • 61
  • still not working, getting the following error:Failed to connect to MySQL: Access denied for user 'ritikasahay'@'localhost' (using password: NO) – ritika Sep 23 '14 at 07:51
  • what is your password. Update proper mysql connection details then this should work " $con=mysqli_connect("localhost","ritikasahay","","trydb"); " update proper username and password – Sundar Sep 23 '14 at 07:55
  • i am sorry, i am pretty new to php, so am active naive. my db doesnot have a password and i tried my cloud9 password too but it doesnt work either – ritika Sep 23 '14 at 07:57
  • try this " $con=mysqli_connect("localhost","root","","trydb"); " – Sundar Sep 23 '14 at 08:00
  • getting this error: Failed to connect to MySQL: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) ....:( – ritika Sep 23 '14 at 08:24
  • http://stackoverflow.com/questions/11990708/error-cant-connect-to-local-mysql-server-through-socket-var-run-mysqld-mysq – Sundar Sep 23 '14 at 08:49