Page 1 of 1

Is this bad coding, ?

Posted: Sat Oct 25, 2003 6:03 pm
by jamrop
I am wondering if this will slow my web pages down if i have this included .
Is there a better more efficent way of doing this, or is this the quickest?

Code: Select all

<?php
<?php







$sqlhome = "select count(*) from id where cat = 'Home'";



$numhome=mysql_query($sqlhome,$db) or die("Select Failed");



$home = mysql_fetch_array($numhome);



//get count of all  family



$sqlfamily = "select count(*) from id where cat = 'Family' '";



$numfamily=mysql_query($sqlfamily,$db) or die("Select Failed");



$family = mysql_fetch_array($numfamily);



//get count of all of student



$sqlstudent = "select count(*) from id where cat = 'Student' ";


$numstudent=mysql_query($sqlstudent,$db) or die("Select Failed");



$student = mysql_fetch_array($numstudent);



//get count of all taxes



$sqlvehicle = "select count(*) from id where cat = 'taxes' ";



$numvehicle=mysql_query($sqlvehicle,$db) or die("Select Failed");



$vehicle = mysql_fetch_array($numvehicle);



//get count of all  garden



$sqlgarden = "select count(*) from id where cat = 'Garden' ";



$numgarden=mysql_query($sqlgarden,$db) or die("Select Failed");



$garden = mysql_fetch_array($numgarden);



//get count of all the ids



$sqlall = "select count(*) from advert where valid='1'";



$numall=mysql_query($sqlall,$db) or die("Select Failed");



$all = mysql_fetch_array($numall);







//get count of all of for the events



$sqlevent = "select count(*) from event where type = 'Event' ";



$numevent=mysql_query($sqlevent,$db) or die("Select Failed");



$event = mysql_fetch_array($numevent);



//get count of all of for the announements



$sqlannounce = "select count(*) from event where type = 'Announcement'  ";



$numannounce=mysql_query($sqlannounce,$db) or die("Select Failed");



$announce = mysql_fetch_array($numannounce);


//get count of all the jobs

$sqljob = "select count(*) from job";



$numjob=mysql_query($sqljob,$db) or die("Select Failed");



$jobs = mysql_fetch_array($numjob);


?>

Posted: Sat Oct 25, 2003 6:28 pm
by kta
That isn't too bad as long as it does what you want. But you could probably write a function that accepts your query and returns the results. That way would save you some typing and file size.

Posted: Sun Oct 26, 2003 6:21 am
by Cruzado_Mainfrm
one thing that is misleading is WHY you call a table 'id'...
next, u do not use mysql_fetch_array() for single results,
u use mysql_result() like this:
$answer = @mysql_result($resultResource,0,'columnName');
where $resultResource is the result of a mysql query, 0 means the first result, and columnName means to get the result of that specific column
i'll rewrite your code, but still u need to use functions for this

Posted: Sun Oct 26, 2003 6:36 am
by Cruzado_Mainfrm
this is your code rewritten: check for errors cuz i didn't

Code: Select all

<?php
function select($query) {
$result = @mysql_query($query,$db) or die("Select Failed On Query $query");
return @mysql_result($result,0,'count(*)');
}
$query = "select count(*) from id where cat = 'Home'"; 
$home = select($query);

//get count of all  family 
$query = "select count(*) from id where cat = 'Family' '"; 
$family = select($query);

//get count of all of student 
$query = "select count(*) from id where cat = 'Student' "; 
$student = select($query);//get count of all taxes 
$query = "select count(*) from id where cat = 'taxes' "; 
$vehicle = mysql_result($result,0,'count(*)'); 

//get count of all  garden 
$query = "select count(*) from id where cat = 'Garden' "; 
$garden = select($query);

//get count of all the ids 
$query = "select count(*) from advert where valid='1'"; 
$all = select($query);

//get count of all of for the events 
$query = "select count(*) from event where type = 'Event' "; 
$event = select($query);

//get count of all of for the announements 
$query = "select count(*) from event where type = 'Announcement'  "; 
$announce = select($query);

//get count of all the jobs 
$query = "select count(*) from job"; 
$jobs = select($query);

?>

Posted: Sun Oct 26, 2003 8:03 pm
by JAM
You could also aply the use of GROUP BY and if it works in your mysql version, UNION...
http://www.mysql.com/doc/en/GROUP-BY-Functions.html
http://www.mysql.com/doc/en/UNION.html

Code: Select all

(select count(cat), cat from id group by cat)
 union
(select count(type), type from event group by type)
... or similiar