Passing variables using php and querying mysql db

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
newphper
Forum Commoner
Posts: 26
Joined: Mon Mar 21, 2011 1:48 am
Location: Bridgeman Downs, Qld, Australia

Passing variables using php and querying mysql db

Post by newphper »

I am new at this and going ok, but...can anyone tell me what i am doing wrong in this code please ?
My database fields names seem to be correct and I fail to see any syntax error atm.....

Code: Select all

<?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 customer WHERE postcode = '".$_POST['postcode']."'");
while ($row = mysql_fetch_array($res))
{
// Commented out 30-05-2011 by Reg
//echo("$row[area_id]");
$value_aid="$row[area_id]";
}

////////////////// Gather and Display postcodes relating to area_id ////////////////////////

$res3=mysql_query("SELECT trade_id FROM customer WHERE trade_id = '".$_POST['trade_type']."'");
while ($row3 = mysql_fetch_array($res3))
{
// Commented out 30-05-2011 by Reg
//echo("\n$row3[trade_id]");
$value_tradeid="$row3[trade_id]";
}

/**************************************** Gather the query information ********************************************/

$res2=mysql_query("SELECT first_name, last_name, phone_mobile, postcode, trade_type FROM customer WHERE area_id = '$value_aid' && trade_type = '$value_tradeid'");

/**************************************** 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><br><br>No one is advertising for this area just yet, sorry.<br>We will have tradesmen advertising here very soon.</strong>");
}

//include ("db_close.php");

?>
The error message i am getting is
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /Users/regellis/Sites/tradetest/view_results3.php on line 70

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /Users/regellis/Sites/tradetest/view_results3.php on line 81
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Passing variables using php and querying mysql db

Post by Celauran »

In one query you have a column called trade_id, in the next query (on the same table) you have a column called trade_type. Seems unlikely they'd both be right. If you're trying to reference a column that doesn't exist, your query will return FALSE.
newphper
Forum Commoner
Posts: 26
Joined: Mon Mar 21, 2011 1:48 am
Location: Bridgeman Downs, Qld, Australia

Re: Passing variables using php and querying mysql db

Post by newphper »

Celauran,

You were on the right track, but I do have a trade_id assigned to a trade_type. Thank you.

My problem was when I started out I had multiple phone fields...phone_home, phone_work, phone_mobile you get the idea.

I cut it back to just mobile and it could not find it as the field was phone_mobile...or the other way around....

One minute we are smart...the next we are so dumb, thats how I fell anyway :-)

Problem solved and is working... not the most robust code but working.

www.tradesman4u.com.au
Post Reply