Reputation: 15
I am developing event website, after create the event, the site give the event url to user like this
www.event.com/?h=6997287a21a94875a1d827f9e2790a6c
6997287a21a94875a1d827f9e2790a6c
this parameter is record as id(primary key) in eventTable. What my problem is i create a comment box, all of the comment is record at commentTable and it has e_id column, the value is same as id from eventTable. how to get all of the comment from commentTable when event id is same as e_id from commentTable;
Upvotes: 0
Views: 128
Reputation: 1691
selects all rows from both tables as long as there is a match between the columns and check for event id which you receive from $_GET
$eventId = $_GET['h'];
select e.id, e.title, e.date, c.name, c.comment from eventTable as e, commentTable as c where e.id = c.e_id and e.id = $eventId;
Upvotes: 0
Reputation: 527
then do the following code,
$event_id=$_GET['h'];
$sql="SELECT * FROM commentTable WHERE e_id ='$event_id'";
$result=mysql_query($sql) or die("Query Failed".mysql_error());
$row =mysql_fetch_array($result);
print_r($row);
Upvotes: 0
Reputation: 1340
The event id is visible in url. That means , You are using GET method.You have bind event id to url as "h". So we can load the comments for the relevant event as follows.
SELECT comments FROM commentTable WHERE e_id=$_GET['h'];
Upvotes: 1
Reputation: 1695
SQL provides JOIN clause to combine rows from two or more tables, based on a common field between them. here you can use below query:
SELECT * FROM eventTable join commentTable on commentTable.e_id=eventTable.id where commentTable.e_id=6997287a21a94875a1d827f9e2790a6c6997287a21a94875a1d827f9e2790a6c
find more about SQL JOIN.
Upvotes: 1
Reputation: 438
it is very simple just a where clause in select statement.
select name, comment from commentTable where e_id= '6997287a21a94875a1d827f9e2790a6c
6997287a21a94875a1d827f9e2790a6c'
Upvotes: 0
Reputation: 1816
This is a simple query with a WHERE
clause.
SELECT name, comment FROM commentTable WHERE e_id = 6997287a21a94875a1d827f9e2790a6c
6997287a21a94875a1d827f9e2790a6c
Upvotes: 0
Reputation: 1887
SELECT * FROM commentTable WHERE e_id = event_id
event_id
is event id parameter, in your example 6997287a21a94875a1d827f9e2790a6c
Upvotes: 0
Reputation: 596
by a join query like this
select * from commentTable inner join eventTable on commentTable.e_id=eventTable.id where commentTable.e_id=111111111
Upvotes: 0