Reputation: 5
I need to grab the result of a query, with php from a mysql database. I have searched around and I think what I have should work, but it doesn't. I'm running it in the correct folder. If there is anything else anyone can tell me, it would be greatly appreciated.
Thanks!
Here is my code.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<?php
// define DB connection variables
$host = "localhost";
$user = "root";
$password = "";
// connect to the MySQL server
$db_connection = mysql_connect($host,$user,$password);
// If the connection failed:
if(!$db_connection){
echo "Error connecting to the MySQL server: " . mysql_error();
exit;
}
mysql_select_db("hockey");
// Execute an SQL SELECT query to pull back rows where Kessel scores
$query = "SELECT COUNT(*) FROM goals WHERE player_id = 4";
$response = mysql_query($query) or die(mysql_error());
?>
<?php
while($row= mysql_fetch_array($response));{
echo $row['COUNT(*)'];
// get the next row's details and loop to the top
}
?>
</body>
</html>
Upvotes: 0
Views: 2368
Reputation: 22152
You have an error here:
while($row= mysql_fetch_array($response));{
Infact you have the semicolon after the while and before the {
(it should not be there).
Anyway, since you're retrieving just one field, the while is useless at all. Get rid of it and do something like this:
$query = "SELECT COUNT(*) as Count FROM goals WHERE player_id = 4";
$response = mysql_query($query) or die(mysql_error());
$row= mysql_fetch_array($response);
echo $row['Count'];
Upvotes: 3