Hello, there is a normal loop, displays the name and id

$ res = mysql_query ("SELECT id, name FROM Manufacturer WHERE Checked = 1 ORDER BY name");

while ($row = mysql_fetch_array($res)) { printf ("<li><a href='/manufacturer.php?id=%s'>%s</a>",$row['id'],$row['name']); } 

How to make a check that would output those manufacturers where there are goods, and within the sql cycle did a request for the existence of goods? Already a very large load on the site turned out. Is there a more effective solution?

  • And where and how do you keep information about the availability of goods? - DL_
  • Everything is stored in the mysql database, the name of the tables is manufacturer and tovar, the id fields are dogmar

1 answer 1

 SELECT DISTINCT m.id, m.name FROM Manufacturer M JOIN Product P ON P.manufacturer = M.id WHERE M.Checked=1 ORDER BY m.name 

the names of the table of goods and its columns are fictional

  • Strangely, everything worked on Denver, laid out on the hosting and gives an error Warning: mysql_fetch_array (): I’m not a valid MySQL result resource
  • sql error shows Unknown column 'm.id' in 'field list' - dogmar
  • I can only advise you to check whether the database on the host actually contains these tables and columns. Check the character encoding in the request - the alias M in the from clause can be written in Russian. The request itself, in view of its primitiveness, cannot be mistaken. - renegator
  • Well, on Denver, everything works)) tables and columns completely coincide with mine, the only product changed to Tovar - dogmar
  • try to put SELECT * what will happen? - renegator