Search code examples
phpmysqlsqlmysql-error-1064

SQL-JOIN error => same column names in multiple tables


I have some trouble trying to join a couple of tables for a time-booking system.

The database look like this:

Database tables:

tbl_events

- int_eventID (INT)
- int_serviceID (INT)
- date_eventDueDate (DATETIME)
- date_eventCreationDate (DATETIME)
- int_userID (INT)
- int_customerID (INT)
- int_eventOnlineBooked (INT)

tbl_customers

- int_customerID (INT)
>>> - int_userID (INT) <= this one gives me headache << 
- str_customerFirstName (VARCHAR)
- str_customerLastName (VARCHAR)
- str_customerEmail (VARCHAR)
- str_customerPassword (VARCHAR)
- str_customerCellPhone (VARCHAR)
- str_customerHomePhone (VARCHAR)
- str_customerAddress (VARCHAR)

tbl_services

int_serviceID (INT)
str_serviceName (VARCHAR)
str_serviceDescription (VARCHAR)
int_servicePrice (INT)
int_serviceTimescale (TIME)

tbl_users

int_userID   (INT)
str_userFirstName (VARCHAR)
str_userLastName (VARCHAR)
str_userEmail (VARCHAR)
str_userPassword (VARCHAR)
str_userCellPhone (VARCHAR)

I've got everything to work as expected by the SQL-query (see below). It gives me all events for a specific "user" during a specific week.

SQL query:

SELECT  int_customerID as customerID,
    int_serviceID as serviceID,
    int_eventID as eventID,
    date_eventDueDate as eventDueDate,
    date_eventCreationDate as eventCreationDate,
    int_eventOnlineBooked as eventOnlineBooked,
    str_serviceName as serviceName,
    int_serviceTimescale as serviceTimescale,
    str_customerFirstName as customerFirstName,
    str_customerLastName as customerLastName,
    str_customerCellPhone as customerCellPhone,
    str_customerHomePhone as customerHomePhone
FROM tbl_events
JOIN tbl_services USING (int_serviceID)
JOIN tbl_customers USING (int_customerID)
WHERE
int_userID = 1 AND
YEARWEEK(date_eventDueDate,1) = 201219

The problem is, I didn't had a column in the tbl_customers table that specified which user that customer belongs to. When I added "int_userID" to tbl_customers the SQL-stopped working and gave me the error message:

<b>Warning</b>:  mysql_num_rows() expects parameter 1 to be resource, boolean given in     <b>/Library/WebServer/Documents/calendar/api/calender_getWeekGetEvents.php</b> on line <b>46</b><br />

Line 46:

if(mysql_num_rows($result)) {
    while($event = mysql_fetch_assoc($result)) {
        $events[] = $event;
    }
}

Any ideas? :)

Thanks / L


Solution

  • If column names are colliding try to specify which table you try to use that from.

    ...
    WHERE
    tbl_Events.int_userID = 1 AND
    ...