PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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?
<?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);
?>
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.
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
<?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);
?>