Forum Moderators: coopster

Message Too Old, No Replies

Mix 2 tables data together

2 sql tables in one query

         

andrewds

2:16 am on Nov 12, 2006 (gmt 0)

10+ Year Member



Hey

I'm new here, but I'm wondering if you could help me please.

Basically I am create a forum in PHP and would like to have a search feature.

However

I am having some problems

my code is:

$getuser = mysql_query("SELECT * from $pretopics where author LIKE '$_GET[membername]'");
while ($results = mysql_fetch_array($getuser))
{
echo("
<tr>
<td width=\"20%\">
$results[topic_name]
</td>

<td width=\"10\">
$results[author]
</td>

<td width=\"10%\">
$results[forum_id]
</td>

<td width=\"50%\">
</td>
</tr>");
}

however that works fine ... but I need it so it always gets data from the table: $preposts

Your help in assisting me would be gratefully appreciated.

Thanks
Andy

willybfriendly

4:19 am on Nov 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$getuser = mysql_query("SELECT * from $pretopics where author LIKE '$_GET[membername]'");

Off topic. The above line is an invitation to security problems.

NEVER use unsanitized user generated data in a query.

Imagine $_GET[membername] = hi'OR 1=1

We have just completed the first step towards compromising your entire application.

Do a search on sql injection.

About your original question, I am not sure I understand it. On the surface, it appears that substituting $preposts for $pretopics in your query would solve the problem, but I suspect you are asking something different than that.

WBF

andrewds

2:07 pm on Nov 12, 2006 (gmt 0)

10+ Year Member



why is this a problem?

also they do is enter a username they want to search for...

anyway, not all the posts are stored in $preposts and in $pretopics it hold the 1st post in the topic...

Andy

coopster

2:21 pm on Nov 12, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, andrewds.

Sounds like you need to either use a JOIN query statement or run another query to retrieve your posted data from the other table. I just posted some JOIN links the other day for somebody else, here they are ...

[webmasterworld.com...]

And here is a starter on that SQL Injection [php.net] mentioned. The PHP Forum Library [webmasterworld.com] also has some security tips in regards to the topic.

andrewds

12:00 am on Nov 23, 2006 (gmt 0)

10+ Year Member



hey


Column 'author' in where clause is ambiguous

I am getting that with the join the code:


$getuser = mysql_query("SELECT $preposts.*, $pretopics.* FROM $preposts LEFT JOIN $pretopics ON ($preposts.author = $pretopics.author) WHERE author = '$_GET[membername]'") or die(mysql_error());

Probably all wrong though :O

Can you help please?

Thanks

spikey

3:57 am on Nov 23, 2006 (gmt 0)

10+ Year Member



You need to specify which "author" you mean in the where clause (even though both are the same by the join):
SELECT $preposts.*, $pretopics.* FROM $preposts LEFT JOIN $pretopics ON ($preposts.author = $pretopics.author) WHERE $preposts.author = '$_GET[membername]'

And pay attention to the comment on sanitizing user input!
Here's some info:
[php.net...]