Jump to content

Selecting from 2 tables not working


harkly

Recommended Posts

I am trying to pull data from 2 tables and have it out put to a form. I am getting a syntax error. Can someone look to see if my Select statement is correct?

 

tables:

about_me

pets

 

("SELECT pets.dog, pets.cat, pets.fish, pets.other, pets.other_type, about_me.headline,
  about_me.description, about_me.attribute_1, about_me.attribute_2, about_me.attribute_3,
  about_me.wls, about_me.wls_other, about_me.wls_month, about_me.wls_day, about_me.wls_year,
  about_me.wls_date, about_me.wls_goal_met, about_me.status, about_me.kids_have, about_me.kids_want,
  about_me.smoking, about_me.smoke_mate, about_me.alcohol, about_me.tattoos, about_me.pets, about_me.education,
  about_me.profession

FROM about_me, pets
WHERE about_me.userID AND pets.userID = 'test'");

 

this is the following code and I am getting and being told that the error is right after my while statement and I am not sure what I need to do to call them - $about_me.headline=$r["headline"]; ??

 

  while ($r=mysql_fetch_array($result))
   {

   $userID=$r["userID"];
   $headline=$r["headline"];
   $description=$r["description"];
   $attribute_1=$r["attribute_1"];
   $attribute_2=$r["attribute_2"];
   $attribute_3=$r["attribute_3"];
   $wls=$r["wls"];
   $wls_month=$r["wls_month"];
   $wls_day=$r["wls_day"];
   $wls_year=$r["wls_year"];
   $wls_goal_met=$r["wls_goal_met"];
   $status=$r["status"];
   $kids_have=$r["kids_have"];
   $kids_want=$r["kids_want"];
   $smoking=$r["smoking"];
   $smoke_mate=$r["smoke_mate"];
   $alcohol=$r["alcohol"];
   $tattoos=$r["tattoos"];
   $pets=$r["pets"];
   $education=$r["education"];
   $profession=$r["profession"];
}

 

 

Link to comment
https://forums.phpfreaks.com/topic/184462-selecting-from-2-tables-not-working/
Share on other sites

 WHERE about_me.userID = 'test' AND pets.userID = 'test'

 

where clause was wrong

 

That shouldn't cause a syntax error. That will just evaluate to true if the about_me.userID is not null and is above 0.

 

If your query was failing then the while loop wouldn't even get executed. I think your syntax error is in the PHP, not the SQL. Can you post the code above and below the line where it says the syntax error is?

 WHERE about_me.userID = 'test' AND pets.userID = 'test'

 

where clause was wrong

 

That shouldn't cause a syntax error. That will just evaluate to true if the about_me.userID is not null and is above 0.

 

If your query was failing then the while loop wouldn't even get executed. I think your syntax error is in the PHP, not the SQL. Can you post the code above and below the line where it says the syntax error is?

 

True, but are you assuming that he is not checking for them to both be the same value?  I didn't state that I was solving his syntax error (like you said, more code is needed for that) I was just using some deductive reasoning on the where clause :)

This is more code that i am using, I am not sure where to go after the select statement. The userID in both tables will be the same.

 

<?php
  mysql_select_db('testDB'); 
  $search=$_GET['userID'];

$result = mysql_query("SELECT pets.dog, pets.cat, pets.fish, pets.other, pets.other_type, about_me.headline,  about_me.description, about_me.attribute_1, about_me.attribute_2, about_me.attribute_3,  about_me.wls, about_me.wls_other, about_me.wls_month, about_me.wls_day, about_me.wls_year,  about_me.wls_date, about_me.wls_goal_met, about_me.status, about_me.kids_have, about_me.kids_want,  about_me.smoking, about_me.smoke_mate, about_me.alcohol, about_me.tattoos, about_me.pets, about_me.education,  about_me.profession FROM about_me, pets WHERE about_me.userID = 'test' AND pets.userID = 'test'");

  while ($r=mysql_fetch_array($result))
   {

   $userID=$r["userID"];
   $headline=$r["headline"];
   $description=$r["description"];
   $attribute_1=$r["attribute_1"];
   $attribute_2=$r["attribute_2"];
   $attribute_3=$r["attribute_3"];
   $wls=$r["wls"];
   $wls_month=$r["wls_month"];
   $wls_day=$r["wls_day"];
   $wls_year=$r["wls_year"];
   $wls_goal_met=$r["wls_goal_met"];
   $status=$r["status"];
   $kids_have=$r["kids_have"];
   $kids_want=$r["kids_want"];
   $smoking=$r["smoking"];
   $smoke_mate=$r["smoke_mate"];
   $alcohol=$r["alcohol"];
   $tattoos=$r["tattoos"];
   $pets=$r["pets"];
   $education=$r["education"];
   $profession=$r["profession"];
}

   echo "
      <form method='post' action='updateAboutMe.php' name='test' id='test'>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.