RE: Simple Problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



<snip>
I can't figure out what I am doing wrong,
this sql string seems to filter out the information I want but it
duplicates the all info, as 'num_rows' is total of rows in the table and
not the correct value of the filtered information?

$sql="SELECT products.productID, products.title,
products.number_per_box, products.stock_level, products.image,
users.username, users.email, users.userID FROM users, products  WHERE
products.userID = $userID"; $mysql_result=mysql_query($sql,$connection);
$num_rows=mysql_num_rows($mysql_result);


this is the old sql statement which works fine - 
$sql="SELECT productID, title, number_per_box, stock_level, image,
userID FROM products WHERE userID = '$userID'";
$mysql_result=mysql_query($sql,$connection);
$num_rows=mysql_num_rows($mysql_result);

what I am I doing wrong.
</snip>

In the old query you're jut pulling records from one table.  In the
second, you're joining two tables, but you're still using the same WHERE
clause, products.userID = $userID.

This is causing multiple records to be returned, since there is nothing
specifying how you are joining the users and products table so it
correctly returns a match for every row in the user table.

Change the where clause to:

WHERE products.userID = $userID
AND users.userID = $userID

and this will force the query to limit results to those records in
products that have a matching record in the users table.

HTH.

Pablo

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux