-2

I am trying to write a script/html page that displays live company information internally. I want to have a widget that counts the amount of live projects from our database.

The widget will need to look on the table called 'projects' then look in the column called 'live' and count how many times 'LIVE' is displayed, but the last problem I have is that we operate two companies so it will need to first check that column 'company' has 'E' there and then count the 'live' column.

I cannot think how to approach this as my php skills are less than novice... more like n00b!

Dan
  • 45
  • 1
  • 9
  • I think you need to go do a bunch more research before asking things on here :) Stack Overflow users will be happy to help out with *specific* problems you are having, but its not really for general, vague, 'how do I do this?' type questions. There are lots of tutorials available online for that. For a place to start, look at basic queries for your database type, and look at writing basic php pages, and connecting to a database in php. – Eilidh Nov 04 '14 at 17:28
  • If you're new to PHP try and find a [development framework](http://codegeekz.com/best-php-frameworks-for-developers/) like [Laravel](http://laravel.com/) that fits your programming style and needs. Most of these have a database layer that does exactly what you want there with a few lines of code, so just follow their examples. – tadman Nov 04 '14 at 17:33
  • thanks guys, I've tried using tutorials but found this website to be more useful - I will endeavor to see if there are tutorials before posting in future. Thanks – Dan Nov 05 '14 at 13:20

1 Answers1

1

Try a simple select count() mysql query similar to:

SELECT count(*) AS count FROM `projects` WHERE `live` = 'LIVE' AND `company` = 'E'

To return the value back inside the php file and output it as html you need to fetch the results of the query as in:

$result = mysql_query("SELECT count(*) AS count FROM projects` WHERE live = 'LIVE' AND company = 'E'");
$row = mysql_fetch_array($result);

echo $row['count'];

CAUTION

You should not be using any of the mysql_*, they are deprecated and are making your application really vulnerable to attacks. Feel free to use them as learning tools (many tutorials) but once you feel comfortable connecting to databases and fetching results, make sure to move to mysqli or pdo accordingly. Good reads:

PHP Mysql Query

How can I prevent SQL injection in PHP?

Community
  • 1
  • 1
Kypros
  • 2,997
  • 5
  • 21
  • 27
  • Hi Kypros, I've created the following code but it returned no value or text. I know there is at least one project in the database that matches both values, any idea what could be wrong? `` – Dan Nov 05 '14 at 13:22
  • if you run that query directly in `mysql` or `phpmyadmin` does it return those rows you expect? – Kypros Nov 05 '14 at 13:41
  • Hi Kypros, I've ran it in phpmyadmin and get the following returned; count 1 – Dan Nov 05 '14 at 14:50
  • That means that 1 project has a `live='LIVE'` and `company='E'`. Isn't that what you need? – Kypros Nov 05 '14 at 14:51
  • Yes this is what I want to display but in phpmyadmin it displays but on my html page it displays nothing?? I have a database login script on an include function which is as follows;- ` ` – Dan Nov 05 '14 at 15:11
  • Please view the updated answer and make sure to understand why you shouldn't be using `mysql`. – Kypros Nov 05 '14 at 15:28