0

I've done some research on JOINs and UNIONs but haven't achieved what I want to do since something I do must be terribly wrong. I have the following SQL-statement:

SELECT posts.*, channels.channel
FROM posts
INNER JOIN channels
ON posts.channel_id = channels.id
ORDER BY posts.created DESC

What I want to do is to get all the columns from the "posts" table and the "channel" column from the "channels" table where posts.channel_id is the same as channels.id. Am I approaching this wrong? Should I skip JOINs and try a more advanced SELECT statement? Help me out here will you? :)

2
Contributors
9
Replies
10
Views
6 Years
Discussion Span
Last Post by debasisdas
0

I guess that somethings wrong while performing the query since the result is nonexistent:

Fatal error: Call to a member function fetch_assoc() on a non-object in C:\xampp\htdocs\HTSIBWORT\gw_sql_lib.php on line 70

But I don't get what could go wrong since this is how I execute it:

//$this->con is the MySQLi-object and &unprp_stmt is the statement
$stmt = $this->con->real_escape_string($unprp_stmt);
$result = $this->con->query($stmt);
0

Ok, I've solved it, the problem was in that the statement was divided into multiple lines so when I ran mysqli::real_escape_string() line breaks "\n" was added into the script. But I've never had any trouble running multi-lined statements before... strange...

0

Thanks! I'm an amateur but I try :)

Edited by gunnarflax: n/a

0

Now I have another problem... When I'm trying to prepare this statement it won't return a mysqli_stmt handle:

SELECT posts.*, channels.channel FROM posts WHERE id=? OR id=? INNER JOIN channels ON posts.channel_id = channels.id ORDER BY posts.published DESC

It works if I don't use the where clause but this time I need it. Do you use WHERE differently when working with JOINs?

1

try this

SELECT posts.*, channels.channel 
FROM posts 
INNER JOIN channels 
ON posts.channel_id = channels.id 
WHERE  (posts.id=? OR posts.id=? )   ----better to use IN clause here.
ORDER BY posts.published DESC
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.