CATEGORII DOCUMENTE |
Asp | Autocad | C | Dot net | Excel | Fox pro | Html | Java |
Linux | Mathcad | Photoshop | Php | Sql | Visual studio | Windows | Xml |
Tutorial - Checking your data for errors
The first one that we could come across is a problem connecting to the database.
$db = pg_connect('dbname=friends');
$query = 'SELECT * FROM friends';
$result = pg_exec($db, $query);
if (!$result)
We've seen this part of the script before. The main lines that we're going to look at are these ones -
if (!$result)
We've already seen that this means that, if after doing the query, if there is NO result - (!$result) - print an error message
and exit the script so it doesn't do anything else. The pg_errormessage($db) will tell you why the script failed. If a result is
found, it will continue onto the next step. We can make this error say anything, from 'ERROR' to pointing to another
webpage altogether, and probably as far as e-mailing you automatically!
Another error we can get is if we have a problem with there being no data in the database. How do we check this? With this line.
if (!$numrows)
If nothing is assigned to $numrows, then there's nothing in the database, so print 'No data', and exit the script so
nothing else is done. You can obviously change this message to whatever you want, see above.
So our whole script will look like this when we try and display all of the data in the database -
<html>
<body>
<?php
$db = pg_connect('dbname=friends');
$query = 'SELECT * FROM friends';
$result = pg_exec($db, $query);
if (!$result)
$numrows = pg_numrows($result);
if (!$numrows)
$row=0;
printf ('<table border=1>');
printf ('<tr><td>ID</td><td>First Name</td><td>Surname</td></tr>');
do
while ($row < $numrows);
printf ('</table>');
?>
</body>
</html>
We only had to add a few lines! Pretty easy isn't it?
If we didn't put this line in, our output would display like this if there wasn't anything in the database -
'Warning: Unable to jump to row 0 on PostgresSQL result index 2 in tutorial.php on line 14'
Which means nothing to the user, and besides that, it doesn't look very nice.
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 757
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved