Jump to content

Running queries from an array?


acook

Recommended Posts

I have a simple page that queries a database and stores the results in an array called $results_array (see below). 

 

<?php


$db_user = "";
$db_pass = "";
$dsn = "OPASRPT";

$conn = odbc_connect($dsn, '', '');

$query = "SELECT Incident_Id, Affected_Item__, Severity, Facility, Customer_Name__, Assignee_Group__, Assignee_Individual, Status_History_Closed_TIME, Status_History_Closed_USER, Submitter, Create_date, VIP, Control_Panel_Create, FCR_Candidate, Source, Assignee_Group_Counter FROM Incident_Management";

$result = odbc_exec($conn, $query);


$results_array = array();

while(odbc_fetch_row($result))
{
  $results_array[] = array(
  'Incident_Id' => odbc_result($result, 1),
  'Affected_Item' => odbc_result($result, 2),
  'Severity' => odbc_result($result, 3),
  'Facility' => odbc_result($result, 4),
  'Customer_Name' => odbc_result($result, 5),
  'Assignee_Group' => odbc_result($result, 6),
  'Assignee_Individual' => odbc_result($result, 7),
  'Closed_Time' => odbc_result($result, ,
  'Closed_User' => odbc_result($result, 9),
  'Submitter' => odbc_result($result, 10),
  'Create_Date' => odbc_result($result, 11),
  'VIP' => odbc_result($result, 12),
  'Control_Panel_Create' => odbc_result($result, 13),
  'FCR_Canidate' => odbc_result($result, 14),
  'Source' => odbc_result($result, 15),
  'Assignee_Group_Counter' => odbc_result($result, 16),
  );
  }

foreach($results_array AS $this_row)

{

echo"{$this_row['Incident_Id']}<br>";

}


odbc_close($conn);
?>

 

My question is, is it possible to perform operations on the stored values?  For example, let's say I wanted to count the number of "Incident_Id"s.  Could this be done?  Also, is it possible to run a function that only counts Severity 4's from "Severity"?  Or can I do any compare functions? 

 

I know this can all be achieved by using more queries, but I'd like to avoid that.  It seems easier to only query the DB once and let PHP do the work.  Please help.

Link to comment
Share on other sites

I know this can all be achieved by using more queries, but I'd like to avoid that.  It seems easier to only query the DB once and let PHP do the work.  Please help.

 

This is in fact the wrong way around. It is usually best to query the database for the exact information you want. Of course, there are all the array functions at your displosal if you still wish to do it your way though.

Link to comment
Share on other sites

This is in fact the wrong way around. It is usually best to query the database for the exact information you want. Of course, there are all the array functions at your displosal if you still wish to do it your way though.

 

Ok, so are you suggesting I run several queries within 1 php page?  What I am looking to do is very complicated and might need some 20+ queries.  Would this be ok?

Link to comment
Share on other sites

Yeah, what I posted was just a basic sample of what I need.  Eventually I need to total certain numbers, show averages of other numbers, show how many of a certain item one has (ie, How many Severity 4's someone had), etc.  That's why I was thinking PHP could do it.  It would take less time to execute and save the DB the trouble of all of the queries. 

 

PS.  Your profile pic seems to be some sort of molecule?  Or part of a molecule...

Link to comment
Share on other sites

After playing with this further, it seems that I'll be impossible to run seperate queries on the database as it is too slow and times out.  I don't understand why if I have the results stored in the array, I can't manupliate them and get the data I need.  The server that the PHP webserver is on is 1000x faster.  I'm sure PHP can handle it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.