tags:

views:

169

answers:

5

I'm trying to join three tables from a database in-order to display a users selected categories but I get the following error.

Warning: mysqli_error() expects exactly 1 parameter, 0 given in

I think I'm doing something wrong when I query my database.

Here is the code below.

// Query member data from the database and ready it for display
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT users.*, categories.*, users_categories.* FROM users_categories INNER JOIN users_categories ON users_categories.user_id = users.user_id JOIN categories ON users_categories.user_id = users.user_id WHERE users_categories.user_id=3");

if (!$dbc) {
 // There was an error...do something about it here...
 print mysqli_error();
} 

//Users entered category loop
while ($row = mysqli_fetch_assoc($dbc)) {
  if (! empty($row['category'])) {
    echo '<div class="skill-info">';
    echo '<p>' , htmlspecialchars($row['category']) , '</p>';
   }

 }

Here is my MySQL tables structure.

CREATE TABLE users (
user_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
user_name VARCHAR(255) NOT NULL,
PRIMARY KEY (user_id)
);

CREATE TABLE categories ( 
id INT UNSIGNED NOT NULL AUTO_INCREMENT, 
parent_id INT UNSIGNED NOT NULL DEFAULT 0, 
category VARCHAR(255) NOT NULL, 
url VARCHAR(255) NOT NULL, 
PRIMARY KEY (id), 
INDEX parent (parent_id)
);

CREATE TABLE users_categories (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT UNSIGNED NOT NULL, 
category_id INT UNSIGNED NOT NULL, 
PRIMARY KEY (id)
);

Now I get the following error

Not unique table/alias: 'users_categories'

How do I fix this?

Thanks everyone for the help. Is there a way i can reward every one for there help here on stackoverflow instead of one person?

A: 

mysqli_error requires a link, try:

print mysqli_error($mysqli);
Dominic Rodger
+3  A: 

You forgot to pass a required argument to mysqli_error(). Change

print mysqli_error();

to

print mysqli_error($mysqli);

Once you make the correction, you'll be able to see the error message and debug further.

FYI: It looks like you're oddly mixing the OO and procedural style usage of mysqli. You should stick to either one or the other.

Asaph
+1  A: 

You should call mysqli_error with parameter:

if (!$dbc) {
    print mysqli_error($mysqli);
}

EDITED:

You join two table with the same names. I think in FROM you made typo. Shouldn't it be FROM users? Table users appears in column list but you do not have it anywhere in FROM or JOINs.

SELECT users.*, categories.*, users_categories.* 
FROM users
INNER JOIN users_categories ON users_categories.user_id = users.user_id 
JOIN categories ON users_categories.user_id = users.user_id 
WHERE users_categories.user_id=3
Lukasz Lysik
@Lukasz Lysik: +1 Nice catch on the SQL table name.
Asaph
Your edit helped but know everything is displayed six times. Is there a way I can limit it to display only once.
123abc
Check the @R. Bemrose suggestions. If it is displayed six times the problem is with your JOINs (I assume that you don't duplicate data in your tables).
Lukasz Lysik
A: 

The query is the problem. Test the query first in PhpMyAdmin or whatever mySQL client you have at your disposal, and you'll see it doesn't work.

SerpicoLugNut
+1  A: 

There are lots of comments on how to fix the mysqli_error line, but there are two problems with the SQL itself:

  1. You are joining the users_categories table twice and not joining the users table at all.
  2. The second ON is identical to the first, so it references the wrong columns in the wrong tables.

This should work better:

SELECT users.*, categories.*, users_categories.*
FROM users_categories
INNER JOIN users ON users_categories.user_id = users.user_id 
INNER JOIN categories ON users_categories.category_id = categories.id
WHERE users_categories.user_id=3
R. Bemrose
This fixed every thing thanks.
123abc
@123abc - I see that you are new at SO. If it answer helped you and you think that it was the best one, please accept it (tick on the left side).
Lukasz Lysik