Selecting from two tables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
franknu
Forum Contributor
Posts: 146
Joined: Sun May 28, 2006 9:29 am

Selecting from two tables

Post by franknu »

Ok, i have tow diffrent tables and i want to display the values for table named message that
match the value for colum BusinessName on table named business_info and message

i have two query the first one works but the second one is not dispalying values

here is my code

Code: Select all

<?php
session_start(); 
$_SESSION['User_Name'];
$_SESSION['Password']; 
$_SESSION['BusinessName'];

?>
<html>
<head>
<body>
<?

if ( isset($_SESSION['User_Name']) && isset ($_SESSION['Password']) )
{

$query = "SELECT * FROM business_info where User_Name='".$_SESSION['User_Name']."' AND Password='".$_SESSION['Password']."'" ;
$result=mysql_query($query);

}

  if ($result = mysql_query($query)) 
{
  if (mysql_num_rows($result))
{
      $row = mysql_fetch_assoc($result);

      $BusinessName= $row['BusinessName'];
      $Keyword = $row['Keyword'];
      $Picture1 =  $row['Picture1'];
      $Headline = $row['Headline'];
      $Slogan =$row['Slogan'];
      $Slogan2 = $row['Slogan2'];
      $Description1 =$row['Description1'];
      $Description2 = $row['Description2'];
      $Description3= $row['Description3'];
      $Contact2 =  $row['Contact2'];
      $Picture2 = $row['Picture2'];
      $Picture3 = $row['Picture3'];
      $Categories=$row['Categories'];
      $Business_Address= $row['Business_Address'];
      $make=$row['make'];
      $type=$row['type'];
      $Tel= $row['Tel'];
      $Website=$row['Website'];
   } 
else 
{
      echo "<p><b>username and/or password not found. Try
again?

</b></p>";
exit;
    }
  } 
else 
{
  echo "Query failed<br />$query<br />". mysql_error();
exit;
  }
 


?>



<table width="504" border="1" cellpadding="0" cellspacing="0" bordercolor="#CCCCCC">
  <tr>
    <td width="494">

<table width="497" border="0" bgcolor="#FFFFFF">
      <tr>
        <td width="487">Banner</td>
      </tr>
      <tr>
	  


	   
	<td background="fondo2.jpg">
<? echo " Welcome  <b>$BusinessName </b>"; ?>

<?

// This is the query that is not displaying any values any idea how i should fix this
if($BusinessName=$_SESSION['BusinessName'])
{
$sql= "SELECT * FROM messages WHERE $BusinessName='".$_SESSION['BusinessName']."' ";

$result=mysql_query($sql);
}

if ($result = mysql_query($query)) 
{
  if (mysql_num_rows($result))
{
      $row = mysql_fetch_assoc($result);
      $BusinessName= $row['BusinessName'];
      $date = $row['date'];
      $from =  $row['from'];
      $subject = $row['subject'];
      $message =$row['message'];
}
}

?>
   <tr>
                        <td><input type="checkbox" name="checkbox" value="checkbox"></td>
                        <td>&nbsp;</td>
                        <td><? echo"$date"; ?></td>
                        <td>gg</td>
                        <td><? echo"$subject"; ?></td>
                      </tr>
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

What are you trying to achieve with the last three lines of
franknu wrote:<?php
session_start();
$_SESSION['User_Name'];
$_SESSION['Password'];
$_SESSION['BusinessName'];
?
franknu
Forum Contributor
Posts: 146
Joined: Sun May 28, 2006 9:29 am

Post by franknu »

I am starting a session there which i initially started at another page that is like administration page.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

session_start() is starting the session mechanism, all right.
But what are the three line after session_start() for?
franknu
Forum Contributor
Posts: 146
Joined: Sun May 28, 2006 9:29 am

Post by franknu »

oh, this is how i know what i am calling, just to remind myself i can eliminate them , it doesnt make any diffrents
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Selecting from two tables

Post by superdezign »

franknu wrote:

Code: Select all

...
    $result=mysql_query($sql);
}

if ($result = mysql_query($query)) 
{
...
You call your new query, then put the old query back into $result.
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

You can open the MySQL and read something about LEFT JOIN or INNER JOIN.
And...

Code: Select all

...
// This is the query that is not displaying any values any idea how i should fix this
if($BusinessName=$_SESSION['BusinessName'])
{
$sql= "SELECT * FROM messages WHERE $BusinessName='".$_SESSION['BusinessName']."' "; 
...
Be attentive with conditions :)
User avatar
alimadzi
Forum Newbie
Posts: 9
Joined: Fri Jun 08, 2007 12:57 pm
Location: Boise, Idaho, USA

Post by alimadzi »

Gente wrote:You can open the MySQL and read something about LEFT JOIN or INNER JOIN.
And...

Code: Select all

...
// This is the query that is not displaying any values any idea how i should fix this
if($BusinessName=$_SESSION['BusinessName'])
{
$sql= "SELECT * FROM messages WHERE $BusinessName='".$_SESSION['BusinessName']."' "; 
...
Be attentive with conditions :)
In case you don't catch what he's talking about, you need to replace the "=" with "==" in your if test. A single equal sign is an assignment operator while a double equal sign is a test for equality.
franknu
Forum Contributor
Posts: 146
Joined: Sun May 28, 2006 9:29 am

Post by franknu »

well, i made some changes but still not displaying :


here is my new code:

Code: Select all

if( isset($BusinessName))
{
$sql=" SELECT  business_info.BusinessName, messages.BusinessName FROM business_info, messages
WHERE business_info.BusinessName=messages.BusinessName";

mysql_query($sql) or die(mysql_error());
$result=mysql_query($sql);
}

if ($result = mysql_query($sql)) 
{
  if (mysql_num_rows($result))
{
      $row = mysql_fetch_assoc($result);
      $BusinessName= $row['BusinessName'];
      $date = $row['date'];
      $from =  $row['from'];
      $subject = $row['subject'];
      $message =$row['message'];
	  }
else 
{
      echo "Query failed<br />$sql<br />". mysql_error();

exit;

}
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The query may be executed three times. Why?

$BusinessName isn't used.
franknu
Forum Contributor
Posts: 146
Joined: Sun May 28, 2006 9:29 am

Post by franknu »

well, i am selecting from two diffrent tables one called business_info and messages
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

franknu wrote:well, i am selecting from two diffrent tables one called business_info and messages
That doesn't explain three query executions. Was it supposed to?
franknu
Forum Contributor
Posts: 146
Joined: Sun May 28, 2006 9:29 am

Post by franknu »

can you explain better because i dont see what i am doing wrong, i reads good to me. maybe the way , it is set up is not the best for me
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You have three mysql_query() back-to-back without any modification of the call. You only need one.
Post Reply