Question

i am trying to select information from my mysql database with this statement after making a successful connection to the database:

$query = "SELECT * FROM `users`, `markers`,`imagemarkers` 
    WHERE username LIKE '%$s%' 
    OR email LIKE '%$s%' 
    OR location LIKE '%$s%' 
    OR author LIKE '%$s%' 
    OR bike LIKE '%$s%' 
    OR id LIKE '%$s%' 
    OR title LIKE '%$s%'";

and i am getting this error: Column 'author' in where clause is ambiguous. Which i understand to be because i have multiple tables with the same field name.

I want to extract the information from these tables, and from these fields:

 -markers:
    author
    title
    bike
    id
    date

 -imagemarkers:
   -author
   -title
   -id
   -date

 -users:
   -loction
   -email
   -username
   -id

I have searched for the solution and have so far come to the conclusion that each table field should be refered to as something like:

  markers.title
  imagemarkers.title

  markers.author
  imagemarkers.author

  markers.date
  imagemarkers.date

  markers.id
  imagemarkers.id
  users.id

And that the statement might look something like:

  SELECT markers.author
    FROM markers JOIN imagemarkers ON markers.author = imagemarkers.author

But i'm not sure how to make this work with the amount of information I need to retrieve.

The whole code i have at the moment looks like this:

        if (isset($_POST['submit'])) {
    if ($_POST['search'] !="") {
    require("connection.php");

    $s = mysql_real_escape_string($_POST['search']);
    $query = "SELECT * FROM `users`, `markers`,`imagemarkers` WHERE username LIKE '%$s%' OR email LIKE '%$s%' OR location LIKE '%$s%' OR author LIKE '%$s%' OR bike LIKE '%$s%' OR id LIKE '%$s%' OR title LIKE '%$s%'";
    $result = mysql_query($query, $connect)
        or die(mysql_error());

    $num = mysql_num_rows($result);

    echo "<h2>you searched for: " . $s . "..</h2>";
    echo "<h5>there are " . $num . " results</h4>";

while ($row = mysql_fetch_assoc($result)) {
    echo "<p>username: " . $row['username'] . "<br />";
    echo "location: " . $row['location'] . "</p>";
    echo "author: " . $row['author'] . "</p>";//error: Column 'author' in where clause is ambiguous, same with date. 
    echo "date: " . $row['date'] . "</p>";
    echo "id: " . $row['id'] . "</p>";
    echo "title: " . $row['title'] . "</p>"; 
    echo "<hr />";

    }
} else {
        echo "<h3> you must type something in the box</h3>";
}  }

Can anyone offer me any help?

Thanks very much.

Was it helpful?

Solution

quick and dirty

$query = "SELECT * FROM `users` u
INNER JOIN `markers` m ON (m.author = u.id)
INNER JOIN``imagemarkers` im ON (im.author = u.id) 
 WHERE u.username LIKE '%$s%'
  OR u.email LIKE '%$s%'
  OR u.location LIKE '%$s%'
  OR m.author LIKE '%$s%'
  OR m.bike LIKE '%$s%'
  OR m.title LIKE '%$s%'"; 

This will link the results together, so that only markers and imagemarkers related to users are shown.

Note that I don't know if markers.author is an integer field linking to users.id or a char field linking to users.name this query assumes the former, please adjust the join condition accordingly is this does not match your tables.

If you leave out the join conditions, you will end up with a cross join, which is 99% likely not what you want.
See: http://en.wikipedia.org/wiki/Join_(SQL)

Listing the columns explicitly
If you want to limit the columns you'll have to name them explicitly (recommended).

$query = "SELECT u.location, u.email, u.username as author, u.id as user_id
   ,m.title as marker_title, m.bike, m.date as marker_date, m.id as marker_id
   ,im.title as imagemarker_title, im.date as imagemarker_date, im.id as imagemarker_id  
FROM `users` u
INNER JOIN `markers` m ON (m.author = u.id)
INNER JOIN``imagemarkers` im ON (im.author = u.id) 
 WHERE u.username LIKE '%$s%'
  OR u.email LIKE '%$s%'
  OR u.location LIKE '%$s%'
  OR m.author LIKE '%$s%'
  OR m.bike LIKE '%$s%'
  OR m.title LIKE '%$s%'"; 

Note that as everything is linked on author, you need to only list that field once, because otherwise you'd get 3 authorfields with exactly the same content, which would be silly.

OTHER TIPS

thats because fields are named the same way

try:

$query = "SELECT * FROM `users` u, `markers` m,`imagemarkers` im
    WHERE u.username LIKE '%$s%' 
    OR u.email LIKE '%$s%' 
    OR u.location LIKE '%$s%' 
    OR m.author LIKE '%$s%' 
    OR m.bike LIKE '%$s%' 
    OR m.title LIKE '%$s%'";

and so on

That's a lot of code for a simple question.. Ambiguous means that an identifier in either your select case or where case is present in more than one table.. If you're saying "Select authors", you need to specifiy from which table.. so markers.authors. If you're doing "where authors.." you need "where markers.authors"

I would suggestion UNIONing your tables.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top