slanden
slanden

Reputation: 1357

SQL Statement Inside A PHP Echo, Inside HTML Tag

I'm using mySQL, and I have a SQL statement assigned to a variable in PHP. Later, I have an HTML <p> element where I want to call the SQL statement and insert the result into the string. So, if there's one item in AppsToBeApproved, the resulting <p> content should read "You have 1 new applications."

"appID" is the auto-incrementing primary key for each item

<?php
$newAppsCount = "COUNT(appID) FROM AppsToBeApproved";
?>

<p><?php echo "You have ".$newAppsCount." new applications."?></p>

The result of this code is this:

"You have SELECT * FROM AppsToBeApproved new applications."

Why does this not work?

Upvotes: 1

Views: 2392

Answers (3)

Abdul Rahman
Abdul Rahman

Reputation: 1705

You have to use obect oriented approach by calling

  <?php echo ($db->dbQuery ("select some column from Table")); ?>

After that your query will show you results. Here in your caee, you are just displaying your variable assigned string. Hope it helps.

Upvotes: 1

Mutasim Fuad
Mutasim Fuad

Reputation: 606

You have to first connect to database and execute the sql command.

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "name_of_database";
$conn = new mysqli($servername, $username, $password, $dbname);

execute the command

$newAppsCount = "COUNT(appID) FROM AppsToBeApproved";
$result = $conn->query($newAppsCount);

And print the result then.

<p><?php echo "You have ".$result." new applications."?></p>

Make sure the sql command is correct.

Upvotes: 5

Bruno F. Fontes
Bruno F. Fontes

Reputation: 1

You have to execute the SQL commands and show just the results. In your code, you are just showing the variable content.

I am not on PC now, but try to search about mysqli or PDO to learn how to use SQL commands thru PHP. You will have to connect the DB first, and then query your SQL Statement to be able to show the results.

Hope this could help you.

Upvotes: 0

Related Questions