Jump to content

[SOLVED] Combining queries


xiao

Recommended Posts

Is it possible to get this into 1 query?

$query = "SELECT * FROM jobs WHERE werkgever_id='".$rij['id']."'";
$query = mysql_query($query) or die("Fout in query");
$tot_jobs = mysql_num_rows($query);
$query = "SELECT * FROM jobs WHERE (werkgever_id='".$rij['id']."' AND status='1')";
$query = mysql_query($query) or die("Fout in query");
$act_jobs = mysql_num_rows($query);
$query = "SELECT * FROM jobs WHERE (werkgever_id='".$rij['id']."' AND status='2')";
$query = mysql_query($query) or die("Fout in query");
$afg_jobs = mysql_num_rows($query);
$query = "SELECT * FROM jobs WHERE (werkgever_id='".$rij['id']."' AND status='0')";
$query = mysql_query($query) or die("Fout in query");
$te_valideren = mysql_num_rows($query);

 

And could you maybe also explain what PHP code I need to use to display the 4 variables? Because I'm not really familiar with AS and GROUP BY etc.

 

Thank you

Link to comment
https://forums.phpfreaks.com/topic/86776-solved-combining-queries/
Share on other sites

Something like this, just change the mysql_num_rows variable to go with $statusid.  Also, you should probably use COUNT instead, since it seems you just care about how many rows return.

 

$statusid = 1

$query =  "SELECT * FROM jobs WHERE (werkgever_id='".$rij['id']."' AND status='$statusid')";

$query = mysql_query($query) or die("Fout in query");

$act_jobs = mysql_num_rows($query);

 

I currently have this, which makes sence I think:

$query = "SELECT count(werkgever_id) AS tot_jobs, status FROM jobs WHERE werkgever_id = '{$rij['id']}' GROUP BY status";
$query = mysql_query($query);
$rij = mysql_fetch_array($query);

 

But I don't know how to display the results seperately in PHP, without using a while loop.

Once you performed your query and you don't want to use the entire array at once, simply use something like this:

 

$rij['0'], $rij['1'], etc.....

 

Since you are fetching as an array you don't even need to use a while loop to actually construct the array, it is done by php.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.