-3

Hello i have a database table with lot of names I'm stuck on getting a SELECT to work and show results Here is the code i have :

$sql = 'SELECT Father, count(*) AS NumChildren
  FROM people
  GROUP BY Father
  HAVING NumChildren > 5
  ORDER BY NumChildren DESC';  

This will hopefully get the Names of Fathers that have more than 5 children I cannot find a way to diplay the results into a Table.

I want the results to appear like the following eample :

Name of Father
Matthew
Marc
Martin

Number of Children
17
15
10

but in a table side by side etc, etc.

Thanks for the replies.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • 1
    Does this answer your question? [display data from SQL database into php/ html table](https://stackoverflow.com/questions/15251095/display-data-from-sql-database-into-php-html-table) – Adrian Kokot Sep 15 '21 at 08:04
  • `I cannot find a way`...well what have you tried? Displaying data from SQL into a HTML table using PHP is the subject of (or an incidental part of) many, many tutorials, questions, examples etc available online already. There's very little point in us repeating that kind of general example again. It's hard to see what problem you're having really. Show your effort and explain what goes wrong. P.S. See also the [tour] and [ask] for more guidance on asking a good question on stackoverflow. – ADyson Sep 15 '21 at 08:08
  • 1
    Which dbms are you using? (I'd expect an error here.) – jarlh Sep 15 '21 at 08:08

1 Answers1

0
$sql = 'SELECT ....';

Assuming you already have a db connection established

$fathers = mysqli_query($connection, $sql);
foreach($fathers as $father){
   echo 'Children: '.$father['numChildren'];
}

//** output above will show all fathers children!

All that would be needed fo you, is to wrap it in html tags

Legaci
  • 54
  • 7
  • This is the code i tried common.php is the file needed to connect to my database table. Result is just a blank page, it doesn't seem to work – Bobby30 Sep 15 '21 at 08:21
  • Maybe, edit your question. show us the common.php file, and the code you are querying. We can then see a larger picture, and can help you more directly. ***Edit out your credentials*** – Legaci Sep 15 '21 at 08:32
  • 1
    + if you are using phpMyAdmin you can run sql queries in the database! it is a great way to check you are pulling the correct data, and check it works. after that you can safley say your sql is not the error. – Legaci Sep 15 '21 at 08:34
  • Thanks I cannot do queries in phpMyAdmin because i don't have access to it. I will try and show contents of the common.php file later. And because I didn't write the original scripts, I'm just trying to get more out of them,. Not sûre if a solution can be found – Bobby30 Sep 15 '21 at 08:58
  • @Bobby30 maybe you don't have phpMyAdmin but you must have _some_ way to access mysql directly to run queries? Otherwise how did you set up your database? And how do you test anything? If someone is denying you access to it, then complain to them that you can't do your work properly because of that – ADyson Sep 15 '21 at 09:00
  • ADyon, Thanks for that reply it's a long story. Basically I have a program on my computer that adds etc to the database the database used is run by the person that made that program. So I have no direct access to the database. That database is then used on my website for all the information needed. I have permission to modify the PHP files used which I have done to get more information back from the database – Bobby30 Sep 15 '21 at 09:09
  • @adyson 100%, failing that you can get some very cheap hosted servers for as little as $1pm which comes with phpMyAdmin! great little package, for small projects. – Legaci Sep 15 '21 at 09:10
  • @Legaci I know that. Maybe you wanted to tell the OP that? – ADyson Sep 15 '21 at 09:24
  • @Bobby30 `I have no direct access to the database` well...technically if your program can access the database, then you should be able to as well, using the same credentials - you could maybe use mysql workbench, or the command-line, to access it. – ADyson Sep 15 '21 at 09:39
  • @Bobby30 `Result is just a blank page`...this could indicate an error. have you got PHP error reporting switched on? Have you got mysqli errors switched on? See the following for guidance if you need help setting those up, it doesn't take long: https://stackify.com/php-error-logs-guide/ (php error logging/reporting) https://stackoverflow.com/a/22662582/5947043 (mysqli error reporting) – ADyson Sep 15 '21 at 09:40
  • @ADyson, Thanks for all your replies iv'e tried to get into the Database and can't for some reason. iv'e just set up my own database with the same tables etc, everything works except what i'm trying to do here. I still get a blank page so the problem is obviously coming from the PHP files somewhere. I will try and contact the person that made the software and PHP scripts to see if they except to help. – Bobby30 Sep 15 '21 at 10:32
  • @Bobby30 as I mentioned just above, a blank page is likely to mean a PHP error...did you turn on the reporting or logging as I said? See those links I provided. The other reason it could be is if there are simply no rows returned from the query (and if the PHP script doesn't have any other output as part of it) ...that's why you need to test the query directly against the database - it helps you to check whether it's the query or the PHP which is at fault. – ADyson Sep 15 '21 at 10:35
  • $sql = "select count(*) FROM people WHERE Father IN ('$_name')"; $nRows = $Connection->RunScalar($sql); echo "Number of Children : $nRows"; – Bobby30 Sep 16 '21 at 12:30