0

I'm learning PHP & MySQL. While trying to folllow a tutorial I am not getting the erxpected result which is simply to retrieve a little data out of a table with a query.
Here is my PHP code:

<?php
//1. Create a database connection
$connection = mysql_connect("localhost", "kingdomc_mtech", "busine$$");
if (!$connection)
{
die("Database connection failed: " . mysql_error());
}

//2. Select a database to use
$db_select = mysql_select_db("kingdomc_learning", $connection);
if (!$db_select)
{
die ("Database selection failed: " . mysql_error());
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SQL lesson1</title>
</head>

<body>
<?php
	//3. Perform database query
	$result = mysql_query("SELECT * FROM subjects", $connection);
	if(!$result)
	{
	die ("Database query failed: " . mysql_error());
	}
	//4. Use returned data
	while ($row = mysql_fetch_array($result));
	{
	echo $row[0]." ".$row[2]."<br/>";
	}
	?>
</body>
</html>
<?php
//5. Close connection
mysql_close($connection);
?>

and i have attached a screenshot of my very simple table. Thanks for the time and help.

Edited by Ezzaral: Changed code tags for clarity.

3
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by almostbob
0

What's the problem? Any error messages?

No error message. I'm not getting any data returned. When I load and even refresh my page its just blank.

0
<?php
//1. Create a database connection
$connection = mysql_connect("localhost", "kingdomc_mtech", "busine$$");
if (!$connection)
{
die("Database connection failed: " . mysql_error());
}

//2. Select a database to use
$db_select = mysql_select_db("kingdomc_learning", $connection);
if (!$db_select)
{
die ("Database selection failed: " . mysql_error());
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SQL lesson1</title>
</head>

<body>
<?php
	//3. Perform database query
	$result = mysql_query("SELECT * FROM subjects", $connection);
	if(!$result)
	{
	die ("Database query failed: " . mysql_error());
	}
	//4. Use returned data
	while ($row = mysql_fetch_array($result));
	{
	echo $row[0]." ".$row[2]."<br/>";
	}
	?>
</body>
</html>
<?php
//5. Close connection
mysql_close($connection);
?>

line 27, try

$result = mysql_query("SELECT * FROM subjects" );
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.