This topic is locked
[SOLVED]

 Novice Question about Count()

1/4/2020 11:57:50 AM
PHPRunner General questions
M
mhollibush author

I am trying to create a couple of snippets for the menu page.
I need to get:

SELECT COUNT(*) as unknown from dog where gender = '0'
I need this viewable on the menupage as a snippet... I am lost on how to create the query in the snippet
Any help would be appreciated

M
mhollibush author 1/4/2020

Not getting any errors and it displays:

there are Males in our system ( not getting the count )



$SQL ="SELECT COUNT(*) FROM dog where gender = '1'";

$result = mysql_query($query);

// Print out result

$data = mysql_fetch_array($result);

echo "there are ". $data["$result"] ." Males in our system";


Can someone tell me where I am going wrong

Thanks

S
Steve Seymour 1/4/2020



Not getting any errors and it displays:

there are Males in our system ( not getting the count )



$SQL ="SELECT COUNT(*) FROM dog where gender = '1'";

$result = mysql_query($SQL);

// Print out result

$data = mysql_fetch_array($result);

echo "there are ". $data["$result"] ." Males in our system";


Can someone tell me where I am going wrong

Thanks


Try this...
$SQL ="SELECT COUNT(*) FROM dog where gender = '1'";

$result = mysql_query($SQL);

// Print out result

$data = mysql_fetch_array($result);

echo "there are ". $data[0] ." Males in our system";

A
acpan 1/4/2020

There are many issues with your Query.

  1. you select gender = '1' as string, so that could be a problem if it is numeric in database, although it is okay it this case.
    As a general guide, you should always try in SQL Editor to confirm query statement is Okay first,

    as a novice to eradicate the doubt of error in your SQL statement and also to ease the helper's doubt.
  2. There is problem with your statement:



$data = mysql_fetch_array($result);

echo "there are ". $data["$result"] ." Males in our system";


$data["$result"] is wrong. And if you think you want to try $data["result"] (result without $ sign) it will also be wrong. result is not a field you intended to extract from the SQL query (see below point 3 how to define and extract the field).
You need to read the document and try out how to construct a proper query. Your SQL statement is trial

and error without following the right example.
3. You codes below is also wrong:
$SQL ="SELECT COUNT(*) FROM dog where gender = '1'";

$result = mysql_query($query);
You had defined $SQL but not $query.
@halcyondaze gave you the answer, but $SQL should replace $query instead. $data[0] is the first field of the SQL Query result. you can also do so:



$SQL ="SELECT COUNT(*) as cnt FROM dog where gender = '1'";

$result = mysql_query($SQL);

$data = mysql_fetch_array($result);

echo "there are ". $data["cnt"] ." Males in our system";


ACP

M
mhollibush author 1/4/2020



There are many issues with your Query.

  1. you select gender = '1' as string, so that could be a problem if it is numeric in database, although it is okay it this case.
    As a general guide, you should always try in SQL Editor to confirm query statement is Okay first,

    as a novice to eradicate the doubt of error in your SQL statement and also to ease the helper's doubt.
  2. There is problem with your statement:



$data = mysql_fetch_array($result);

echo "there are ". $data["$result"] ." Males in our system";


$data["$result"] is wrong. And if you think you want to try $data["result"] (result without $ sign) it will also be wrong. result is not a field you intended to extract from the SQL query (see below point 3 how to define and extract the field).
You need to read the document and try out how to construct a proper query. Your SQL statement is trial

and error without following the right example.
3. You codes below is also wrong:
$SQL ="SELECT COUNT(*) FROM dog where gender = '1'";

$result = mysql_query($query);
You had defined $SQL but not $query.
@halcyondaze gave you the answer, but $SQL should replace $query instead. $data[0] is the first field of the SQL Query result. you can also do so:



$SQL ="SELECT COUNT(*) as cnt FROM dog where gender = '1'";

$result = mysql_query($SQL);

$data = mysql_fetch_array($result);

echo "there are ". $data["cnt"] ." Males in our system";


ACP


Being a novice and trying to learn... I can create majors headaches...

I ran the query in the SQl query of PHPR



SELECT

COUNT(*) FROM dog WHERE gender = '1'



and it displayed the count as 4183
I then ran the query



SELECT

COUNT(*) AS total FROM dog WHERE gender = '1'


and it displayed the "total" as 4183
I am not grasping how to display it in a snippet
sorry - just having a hard time with it....

N
Nir Frumer 1/4/2020

hi

if you use plain php code

it should be mysql

$data = mysql_fetch_array($result);
your required info is: $data[0];
OR use mysql_fetch_assoc($result)

this will give you your required value
hope it helps,

Nir.

A
acpan 1/4/2020


I ran the query in the SQl query of PHPR
SELECT

COUNT(*) FROM dog WHERE gender = '1'
and it displayed the count as 4183
I then ran the query
SELECT

COUNT(*) AS total FROM dog WHERE gender = '1'
and it displayed the "total" as 4183


So, is the above result expected by you? You have problem displaying the text or query ?

With your earlier query, you will get problem at the Query

(which is why i suggest you always check your query is okay with SQL editor like SQLyog).

M
mhollibush author 1/4/2020

Trying both way:



$SQL ="SELECT COUNT(*) as cnt FROM dog where gender = 1";

$result = mysql_query($SQL);

// Print out result

$data = mysql_fetch_array($result);

echo "there are ". $data["cnt"] ." Males in our system";


and also



$SQL ="SELECT COUNT(*) FROM dog where gender = '1'";

$result = mysql_query($SQL);

// Print out result

$data = mysql_fetch_array($result);

echo "there are ". $data[0] ." Males in our system";


It still displays "there are Males in our system"

Its not displaying the actual count?
I have searched all over the place "google" and everything I am reading and trying is telling me this should work.

Running the query in the PHPR query it does show the "4183" males....
I am missing something....
This is on the menu page - if that makes a difference

M
mhollibush author 1/4/2020




I ran the query in the SQl query of PHPR
SELECT

COUNT(*) FROM dog WHERE gender = '1'
and it displayed the count as 4183
I then ran the query
SELECT

COUNT(*) AS total FROM dog WHERE gender = '1'
and it displayed the "total" as 4183


So, is the above result expected by you? You have problem displaying the text or query ?

With your earlier query, you will get problem at the Query

(which is why i suggest you always check your query is okay with SQL editor like SQLyog).


Yes - but everything I am trying and also trying other people's code doesn't display the actual count - " There are Males in our system" its missing the count

Not getting any errors

A
acpan 1/4/2020

Yes, i can see why, try this:


$strSQL = "SELECT COUNT(*) as cnt FROM dog WHERE gender = '1'";

$rs = DB::Query($strSQL);

while ($data = $rs->fetchAssoc())

{

$cnt = $data["cnt"];

}
echo "there are ". $cnt ." Males in our system";


Try to only use the latest database API to be consistent.

https://xlinesoft.com/phprunner/docs/db_query.htm
ACP

M
mhollibush author 1/4/2020



Yes, i can see why, try this:


$strSQL = "SELECT COUNT(*) as cnt FROM dog WHERE gender = '1'";

$rs = DB::Query($strSQL);

while ($data = $rs->fetchAssoc())

{

$cnt = $data["cnt"];

}
echo "there are ". $cnt ." Males in our system";


That Did It!!!!

This has me concerned about my other snippets now ( time to read ).....

Thanks For All Your Help!!!!!
Try to only use the latest database API to be consistent.

https://xlinesoft.com/phprunner/docs/db_query.htm
ACP

A
acpan 1/4/2020

You can also put the code in events, after application init (app init), if you need to use the dog count at multiple places so that you don't have to repeat the codes everywhere, like so:



$strSQL = "SELECT COUNT(*) as cnt FROM dog WHERE gender = '1'";

$rs = DB::Query($strSQL);

while ($data = $rs->fetchAssoc())

{

$cnt = $data["cnt"];

}

$_SESSION["count_dog"] = $cnt;


and just put this in a snippet anywhere in your pages:
echo "there are ". $_SESSION["count_dog"] ." Males in our system";
ACP
>That Did It!!!!

>This has me concerned about my other snippets now ( time to read ).....

>Thanks For All Your Help!!!!!