nickivz Posted January 10, 2017 Share Posted January 10, 2017 (edited) 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 January 12, 2017 by Zane Quote Link to comment Share on other sites More sharing options...
Barand Posted January 10, 2017 Share Posted January 10, 2017 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. Quote Link to comment Share on other sites More sharing options...
Mlaaa Posted January 10, 2017 Share Posted January 10, 2017 (edited) 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 January 10, 2017 by Mlaaa Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.