0

I'm trying to get this piece of code to display the variable $variable as a number which is calling from a database.

There are categories that contain sub categories, these categories are displayed with the number of sub categories next to it (the dynamically generated number)

e.g. Category: Gaming (4)

The bit in the brackets is the dynamically generated number.

Here is the code, I can only get it to display like this:

Windows Doors Conservatories (Array)

<?php 
 mysql_connect("", "", "") or die(mysql_error()); 
 mysql_select_db("testing") or die(mysql_error()); 
 $data = mysql_query("SELECT * FROM categories2");
 $subno = mysql_query("SELECT COUNT(*) FROM subcategories WHERE keyword = 'Windows'")
  or die(mysql_error()); 

 Print "<table>"; 

 while($info = mysql_fetch_array( $data ))
 { 
    Print "<tr>"; 
    Print "<td>".$info['username'] . "</td> ";
 } 
 while($variable = mysql_fetch_array( $subno ))
 { 
    Print "<td>(".$variable . ") </td></tr>"; 
 }
 Print "</table>"; 
 ?> 

Thanks in advance!

Jagan Akash
  • 559
  • 5
  • 26
Josh
  • 3
  • 1
  • Inb4 all the "mysql_ functions are deprecated" comments: http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php/14110189#14110189 – BLaZuRE Jul 22 '13 at 09:48
  • I've added this in, the result gives me the answer 2, there are 4 entries in the database. Also it only displays for the last category? – Josh Jul 22 '13 at 09:51
  • What only displays for the last category? Your `COUNT(*)` will return a single row, containing the number of rows returned by your query (which is 2). Note, it is case sensitive as well as whitespace sensitive. – BLaZuRE Jul 22 '13 at 09:54
  • the number returned should be 4, there are 4 entries in the database with the keyword Windows, This is the number I am trying to display in the brackets. Thanks for the quick replies. – Josh Jul 22 '13 at 09:56
  • To clarify, your `subcategories` table contains 4 entries. Each of these entries has `Windows`, exactly in that case, with no other words or whitespace. Also, what is the result of your query if you just select all and adjust your PHP so that it prints the id of each row (can you spot any pattern between the 2 rows returned?). – BLaZuRE Jul 22 '13 at 10:01
  • In total there are 12 entries, 4 of these entries have the exact match Windows on the keyword column with no white space and caps sensitive. – Josh Jul 22 '13 at 10:05

1 Answers1

0

use synonymous. :

SELECT COUNT(*) AS `cnt` ...

echo $variable['cnt'];
  • You don't even need to do that, you can do `$variable['COUNT(*)']`. For some reason, I just assumed `$variable` was some variable name he didn't want to display and not the actual variable he was using. – BLaZuRE Jul 22 '13 at 10:05
  • thanks guys this has worked, only for the last category though, it displays like this: Windows Doors Conservatories (4) Whereas I want it to do Windows (4) Doors (4) Conservatories (4) Obviously the number will be different for each category depending on the amount in the database – Josh Jul 22 '13 at 10:10
  • this depends on your query, and loops combination. in your case you firstly display a row with categories. and after you display counts. but your Count query is failed, cause it returns only one value. you should group by names.. – Павел Иванов Jul 22 '13 at 10:44