Jump to content

Unable to retrieve data from database


nickivz

Recommended Posts

I have used the example (and some others) program but for some reason i don't seem to be able to read data from my database. Can someone help me to find out what is wrong. 

 

The database is located at www(.)canary(.)co(.)za, 

database name = canaryco_codes
table = towns
password = Lechwe01
fields = town-id, town, province, street, box, area

If you run www(.)canary(.)co(.)za/codes3 the result is:

"; echo "
town-id " "
town " "
province " "
street"; while(list($town-id,$town,$province,$street)= mysql_fetch_array($val)) { echo ""; echo "".$town-id.""; echo "".$town.""; echo "".$province.""; echo "".$street.""; echo ""; } echo ""; echo "
Done
n"; ?>

This is the sample program:

<!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>Database</title>
</head>
<body>
<?php
//connect database 
$con=mysql_connect("localhost","**********","********") or die(mysql_error());
// select database 
mysql_select_db("canaryco_codes",$con);
//select values from towns table
$data="SELECT * FROM towns";
$val=mysql_query($data); 
echo "<table border='1'>";
echo "  <p>town-id " "  <p>town " "  <p>province " "  <p>street";
while(list($town-id,$town,$province,$street)= mysql_fetch_array($val))
{
 echo "<tr>"; 
 echo "<td>".$town-id."</td>";
 echo "<td>".$town."</td>";
 echo "<td>".$province."</td>";
 echo "<td>".$street."</td>";
 echo "</tr>";
}
echo "</table>";
  echo "<p>Done </p>n";
?>
</body>
</html>
 
Thanks a lot.
 
Edited by Zane
Link to comment
Share on other sites

Your posted code won't even execute without throwing syntax errors.

 

Turn on error reporting in your php.ini file and set the error level to E_ALL.

 

When you execute a query, check the return value. If false then it failed and you should check the content of mysql_error().

 

mysql_ functions have been obsolete for years and now removed from PHP. Change to mysqli_ functions or PDO.

 

When you've done this, check the errors yourself and if you still cannot fix them come back.

Link to comment
Share on other sites

Try this

<?php
error_reporting(E_ALL);

// connection to database
try {
    $dbh = new PDO('mysql:host=your_host;dbname=database_name', 'username', 'password');
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

?>

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

<?php

//select values from towns table
$stmt = $dbh->prepare ("SELECT * FROM towns");
$stmt->execute();
$data = $stmt->fetchAll();

echo '<table width="100%">
		<tr>
			<th>Town ID</th>
			<th>Town</th>
			<th>Province</th>
			<th>Street</th>
		</tr>';

// display all values in table
foreach ($data as $key)
{
	echo '<tr>
			<td>'.$key['town-id'].'</td>
			<td>'.$key['town'].'</td>
			<td>'.$key['province'].'</td>
			<td>'.$key['street'].'</td>
		</tr>';
}

echo '</table>';

?>

	</body>
</html>
Edited by Mlaaa
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.