I am trying to write a conditional statement which echo's the following messages :
if {the query executed bring results from the database
Process the query
echo 'Success' }
else {
If no results/ 0 results have been retrieved then
echo 'No results obtained'}
I am using PostgreSQL as my database. I am new to using PostgreSQL with PHP but so far I have managed to make my queries work. I am just confused on how I can do this and which part of the code this logic should be in. Is it before the query has been executed.
<?php
// Connecting, selecting database
$dbconn = pg_connect("host=localhost port=5432 dbname=sser user=postgres password=password")
or die('Could not connect: ' . pg_last_error());
$name = pg_escape_string($_POST['name']);
$name2 = pg_escape_string($_POST['name2']);
$query = " SELECT y.name, y.time, z.name, z.time
FROM
(SELECT * FROM departure_times WHERE name ='$name') as y,
(SELECT * FROM departure_times WHERE name ='$name2') as z
WHERE y.tram_id = z.tram_id ";
$result = pg_query($query) or die('Query failed: ' . pg_last_error());
// Printing results in HTML
echo "<table>
";
echo "These are the following trams from '$name' to '$name2' ";
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
echo "\t<tr>
";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>
";
}
echo "\t</tr>
";
}
echo "</table>
";
// Free resultset
pg_free_result($result);
// Closing connection
pg_close($dbconn);
?>