user2242044
user2242044

Reputation: 9213

querying data from mysql db with PHP

I have a MySQL database on my local machine and am trying to query it with PHP.

I've written out the following HTML/PHP code. When I open this HTML file in Chrome, this is what is displayed. Why does the browser treat this code as text?

ID Project Code'; while ($row = mysqli_fetch_array($response)){ echo '' . $row[Id] . '' . $row[Project_Code] . ''; echo ''; } echo ''; } else { echo "couldn't connect do it"; echo mysqli_error($dbc); } mysqli_close($dbc); ?>

code:

<HTML>
<body>
<?php
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'root');
DEFINE ('DB_HOST', 'my_local_ip');
DEFINE ('DB_NAME', 'my_db');

$dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die("couldn't connect");
$query = "SELECT Id, Project_Code FROM database.projects";
$response = @mysqli_query($dbc, $query);

if ($response){
    echo '<table>
    <td>ID</TD>
    <td>Project Code</TD></table>';

    while ($row = mysqli_fetch_array($response)){
        echo '<tr><td>' . 
        $row[Id] . '</td></td>' .
        $row[Project_Code] . '</td></td>';

        echo '</tr>';

    }

    echo '</table>';
} else {
    echo "couldn't connect do it";
    echo mysqli_error($dbc);
}
mysqli_close($dbc);
?>
</BODY>
</HTML>

Upvotes: 1

Views: 125

Answers (3)

Murtaza Bhurgri
Murtaza Bhurgri

Reputation: 398

It is Because you are not running your code with apache/php/mysql.

If you are using windows package like wamp put it in www directory and access it through localhost, not directly open it in the browser, otherwise browser will treat it like plain html.

i've opened it as you were opening in chrome:

enter image description here

You should access your script using localhost and make sure your wamp (or whatever server package your are using) should be running.

Upvotes: 2

user4037517
user4037517

Reputation:

After you installed Apache,MySQL and PHP
(You can try XAMPP or WAMP as mentioned in other answers.)
you can test this code:
(For XAMPP put your code in xampp\htdocs folder ,
name like something.php
to run. browse to localhost/something.php)

<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<?php
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'database');

$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die("couldn't connect");
$query = "SELECT Id, Project_Code FROM database.projects";
$response = mysqli_query($dbc, $query);
?>
<?php
if ($response){
    echo '<table>
    <td>ID</td>
    <td>Project Code</td>';
    while ($row = mysqli_fetch_array($response)){
        echo '<tr>'.'<td>'.$row['Id'].'</td><td>'.$row['Project_Code'].'</td></tr>';
    }
    echo '</table>';
} else {
    echo "couldn't connect do it";
    echo mysqli_error($dbc);
}
mysqli_close($dbc);
?>
</BODY>
</HTML>

Upvotes: 0

chris85
chris85

Reputation: 23892

PHP unlike javascript/HTML is a server side language. It runs on the server and outputs the generated content to the browser.

So for example

<?php 
if(empty($a)) {
echo ':)';
} else { 
echo ':(';
}
?>

The only part that makes it to the browser would be :).

The issues you are running into are

a) your file is .html. PHP won't run as such unless your server configuration is modified to do so.
b) your files are being executed on your local machine which doesn't have PHP installed.

Solutions:

a) Install PHP on your local machine; http://php.net/manual/en/install.php
b) Move to a hosted server (this might be easier since they will have support for configuration issues)

(lettering unrelated to issues lettering)

To be more specific your whole PHP code is outputted to the browser but the

<?php
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'root');
DEFINE ('DB_HOST', 'my_local_ip');
DEFINE ('DB_NAME', 'my_db');

$dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die("couldn't connect");
$query = "SELECT Id, Project_Code FROM database.projects";
$response = @mysqli_query($dbc, $query);

if ($response){
    echo '<table>

Is all read as one element <?php until the > of the <table>.

Also while learning try to never use the @. That suppresses errors and those might (95% of the time, will) be useful to you.

Upvotes: 1

Related Questions