1

So here's my situation. I have a books table and authors table. An author can have many books... In my authors page view, the user (logged in) can click an author in a tabled row and be directed to a page displaying the author's books (collected like this URI format: viewauthorbooks.php?author_id=23), very straight forward... However, in my query, I need to display the books for the author only, and not all books stored in the books table (as i currently have!) As I am a complete novice, I used the most simple query of:

SELECT * FROM books 

This returns the books for me, but returns every single value (book) in the database, and not ones associated with the selected author. And when I click a different author the same books are displayed for them...I think everyone gets what I'm trying to achieve, I just don't know how to perform the query. I'm guessing that I need to start using more advanced query clauses like INNER JOIN etc. Anyone care to help me out :)

Jess
  • 191
  • 1
  • 1
  • 7

2 Answers2

0

The basic syntax is SELECT * FROM table WHERE column=value;

But you really need to study more SQL, I'd suggest going through sqlzoo tutorial. http://sqlzoo.net/

MindStalker
  • 14,235
  • 3
  • 23
  • 19
0

Enters the WHERE clause:

The WHERE clause is used to extract only those records that fulfill a specific criteria. In your case, all you need to do is:

SELECT * FROM tasks_tb WHERE author_id = '23';

You will obviously need to change the '23' with the value passed in the URL querystring, so that each page lists the books of each relevant author.

Since it is never too early to start reading about best practices, note that for public websites it is really dangerous to include any un-sanitized input into an SQL query. You may want to read further on this topic from the following Stack Overflow posts:

Community
  • 1
  • 1
Daniel Vassallo
  • 312,534
  • 70
  • 486
  • 432
  • Thanks for your help...I'm not bothering with security issues right now as the application will be used internally, will this still be safe? Also, I have been using traditional mysql code like 'mysql_query' etc. changing this to 'mysqli' caused me parameter errors. I have dozens of queries in this structure, will it take me a long time to make it suitably hack-proof? – Jess Mar 16 '10 at 23:34
  • Yes, it does take some time to make the queries hack-proof, but it is definitely worth it, for a public web site. You may want to check Google or Stack Overflow for "parameterized queries". – Daniel Vassallo Mar 16 '10 at 23:52