The problem is that you'll always get a result back if you use mysqli_query. You need to check to see how many rows were returned to determine whether or not to display the table. Also, please use code tags around your code:
Ok, I have attacked it another way and this time it works,probably not the best code as I am new.
If anyone can suggest a better way to code this snippet, please assist
<?php
/******************** This needs to be put into an inc file located elswhere for security reasons ******************/
$db=mysql_connect ("127.0.0.1", "root", "mypassword") or die ('Could not connect to the database: ' . mysql_error());
/************************** If you cant connect to localhost, send message to screen *******************************/
if (!$db)
{
die('Could not connect: ' . mysql_error());
}
/******************************* If you do connect nominate the database you want to use **************************/
mysql_select_db ("mytable", $db);
/**************************************** gather the query information ********************************************/
$res=mysql_query("SELECT customer_id, first_name, last_name, phone_mobile, postcode, trade_type FROM customer WHERE postcode = '".$_POST['postcode'] . "' && trade_type = '".$_POST['trade_type'] . "'");
/**************************************** DISPLAY QUERY RESULTS HERE *********************************************/
while ( $row = mysql_fetch_array( $res ) )
{
echo("<TABLE align='center' border = '1' bgcolor = 'A7E3F6'><TH><strong>SEARCH RESULTS<strong></TH>");
echo("<TR><TD><strong>Name :<strong>\n$row[first_name]\n$row[last_name]</TD></TR>");
echo("<TR><TD><strong>Phone :<strong>\n$row[phone_mobile]</TD></TR>");
echo("<TR><TD><strong>Postcode :<strong>\n$row[postcode]</TD></TR>");
echo("<TR><TD><strong>Trade Type :<strong>\n$row[trade_type]</TD></TR></TABLE>");
}
/*********************** If no matching records in my table...DISPLAY MESSAGE HERE ******************************/
if (mysql_num_rows($res) == 0) {
echo ("<strong>No Records to show, sorry.</strong>");
}
mysql_free_result($res);
mysql_close($db);
?>
<?php
/******************** This needs to be put into an inc file located elswhere for security reasons ******************/
$db=mysql_connect ("127.0.0.1", "root", "mypassword") or die ('Could not connect to the database: ' . mysql_error());
/************************** If you cant connect to localhost, send message to screen *******************************/
if (!$db)
{
die('Could not connect: ' . mysql_error());
}
/******************************* If you do connect nominate the database you want to use **************************/
mysql_select_db ("mytable", $db);
/**************************************** gather the query information ********************************************/
$res=mysql_query("SELECT customer_id, first_name, last_name, phone_mobile, postcode, trade_type FROM customer WHERE postcode = '".$_POST['postcode'] . "' && trade_type = '".$_POST['trade_type'] . "'");
$total = mysql_num_rows(mysql_query("SELECT customer_id FROM customer"));
/**************************************** DISPLAY QUERY RESULTS HERE *********************************************/
while ( $row = mysql_fetch_array( $res ) )
{
echo("<TABLE align='center' border = '1' bgcolor = 'A7E3F6'><TH><strong>SEARCH RESULTS<strong></TH>");
echo("<TR><TD><strong>Name :<strong>\n$row[first_name]\n$row[last_name]</TD></TR>");
echo("<TR><TD><strong>Phone :<strong>\n$row[phone_mobile]</TD></TR>");
echo("<TR><TD><strong>Postcode :<strong>\n$row[postcode]</TD></TR>");
echo("<TR><TD><strong>Trade Type :<strong>\n$row[trade_type]</TD></TR></TABLE>");
}
/*********************** If no matching records in my table...DISPLAY MESSAGE HERE ******************************/
echo "<strong>" . mysql_num_rows($res) . " of " . number_format($total) . " records found.";
mysql_free_result($res);
mysql_close($db);
?>
I actually have achieved that part, just wanted to display a message if no records in the postcode area. I have managed to do that part.
Now I am stuck again...In my postcode table I may have 20 postcodes(zipcodes) with the same area_id.
My pc_test table has the fields "postcodes", "area_name", "area_id".
I now want to be able to display all postcodes linked to the same area_id.
<?php
//error_reporting(E_ALL);
///////////////////////Connect to the database and close the connection when finished///////////////////////////////
include ("dbconnect.php");
///////////////////////////////// Gather and Display area_id ///////////////////////////////
$res=mysql_query("SELECT area_id FROM pc_test WHERE postcodes = '".$_POST['postcode']."'");
while ($row = mysql_fetch_array($res))
{
echo("$row[area_id]");
}
//88//88//88//88//88//This is where I need to query and display all postcodes that belong to an area_id ////////////////////
///////////// Would anyone have the time and/or inclination to have a go at this piece of code ??? ////////////////////////
/////////////My pc_test table has an "area_id" and there may be 20 postcodes with that area_id number //////////////////////
////////////////////How does one query that and then pass it and then display the result ?? ////////////////////////////////
//////////////////// My guess is to put them into an array and then echo them out ?? //////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**************************************** Gather the query information ********************************************/
$res2=mysql_query("SELECT customer_id, first_name, last_name, phone_mobile, postcode, trade_type FROM customer WHERE postcode = '". $_POST['postcode'] . "' && trade_type = '".$_POST['trade_type'] . "'");
/**************************************** DISPLAY QUERY RESULTS HERE *********************************************/
while ( $row2 = mysql_fetch_array( $res2 ) )
{
echo("<TABLE align='center' border = '1' bgcolor = 'A7E3F6'><TH><strong>SEARCH RESULTS<strong></TH>");
echo("<TR><TD><strong>Name :<strong>\n$row2[first_name]\n$row2[last_name]</TD></TR>");
echo("<TR><TD><strong>Phone :<strong>\n$row2[phone_mobile]</TD></TR>");
echo("<TR><TD><strong>Postcode :<strong>\n$row2[postcode]</TD></TR>");
echo("<TR><TD><strong>Trade Type :<strong>\n$row2[trade_type]</TD></TR></TABLE>");
}
/*********************** If no matching records in my table...DISPLAY MESSAGE HERE ******************************/
if (mysql_num_rows($res2) == 0) {
echo ("<strong>No Result, sorry</strong>");
}
//include ("db_close.php");
?>
$res = mysql_query("SELECT postcode FROM pc_test WHERE area_id = '{$_POST['area_id']}'")
or die ("cannot select postcodes from area {$_POST['area_id']}: " . mysql_error());
////////////////// Gather and Display postcodes relating to area_id ////////////////////////
$res3 = mysql_query("SELECT postcodes FROM pc_test WHERE area_id = '".$_POST['postcode']."'")
or die ("cannot select postcodes from area {$_POST['area_id']}: " . mysql_error());
while ($row3 = mysql_fetch_array($res3))
{
echo("$row3[postcodes]");
}
$res3=mysql_query("SELECT postcodes FROM pc_test WHERE area_id = '".$_POST['postcode']."'")
or die ("cannot select postcodes from area {$_POST['area_id']}: ". mysql_error());
while ($row3 = mysql_fetch_array($res3))
{
echo("$row3[postcodes]");
}
if (mysql_num_rows($res3) == 0) {
echo ("<strong>No postcodes match area_id.</strong>");
}
///////////////////////////////// Gather and Display area_id //////////////////////////////
$res=mysql_query("SELECT area_id FROM pc_test WHERE postcodes = '".$_POST['postcode']."'");
while ($row = mysql_fetch_array($res))
{
echo("$row[area_id]");
$value="$row[area_id]";
}
////////////////// Gather and Display postcodes relating to area_id ////////////////////////
$res3=mysql_query("SELECT postcodes FROM pc_test WHERE area_id = '$value'")
or die ("cannot select postcodes from area {$_POST['postcode']}: ". mysql_error());
while ($row3 = mysql_fetch_array($res3))
{
echo("\n$row3[postcodes]");
}
if (mysql_num_rows($res3) == 0) {
echo ("<strong>No postcodes match area_id.</strong>");
}