PHP - Query Against An Array
I am trying to compare a teams average attendance compared to the average across all teams. The query that show the average by team (column 'atte') is
Code: [Select] $att_table = mysql_query(" SELECT t.team_name as Tm , ROUND(AVG(g.attendance))atte , SUM(g.attendance) tot , MAX(g.attendance) max , MIN(g.attendance) min from teams t left join all_games g on t.team_id = g.home_team WHERE comp = '1' AND home_goals IS NOT NULL AND date BETWEEN '2010-07-01' AND '2011-06-31' GROUP BY t.team_id ORDER BY atte DESC ");A simple query to show the total average is Code: [Select] $lgeav = mysql_query(" (AVG(g.attendance))atte2 from all_games g WHERE comp = '1' AND home_goals IS NOT NULL AND date BETWEEN '2010-07-01' AND '2011-06-31' ");I am hoping to put the second query into an array and use the two to show the teams whose own average attendance is higher than the total average attendance in bold, with something along the lines of while ($row_att_table = mysql_fetch_assoc($att_table)){ if ($row_att_table['atte'] > $lgeav['atte2']) echo '<tr style="font-weight:bold">'; else echo '<tr>'; I know that this can be done with views but in an attempt to broaden my knowledge (and also in case I ever have to do something similar on MySQL4) I wondered if there was a simple way to do this through PHP? Thanks in advance Steve Similar TutorialsSo I'm querying my database to add the results (mapID's) into a PHP array. The MySQL query I used in the below code would usually return 10 values (only 10 mapID's in the database) Code: [Select] while($data = mysql_fetch_array(mysql_query("SELECT mapID FROM maps"))){ $sqlsearchdata[] = $data['mapID']; } Instead the page takes ages to load then gives this error: Quote Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 16 bytes) It says the error begins on the first line of the above code. I'm assuming this is not the right way to add the value from the MySQL array into a normal PHP array. Can anyone help me? Hi I am trying to store the results of a mysql query into a php array. Here is the code i am using $test = implode(",",$_GET['checkbox']); $query = mysql_query("SELECT categoryTitle FROM `category` where categoryid in ($test)"); $test2 = array(); while ($result = mysql_fetch_assoc($query)) { $test2[] = $result; } $test3 = implode(", ",$test2); print_r($test3); Everything works fine until implode the array. When i try to print after the implode the result is "Array, Array". if i remove the implode it prints "Array ( => Array ( [categoryTitle] => Escalators & Lifts ) [1] => Array ( [categoryTitle] => Human Resource/Payroll/ Training ) )" The values here are ture. Im thinking that its storing an array inside another array so cannot implode. Is there something i can do to solve this I'm trying to delete items from a table by selecting them via checkboxes. I managed to post all the ids of the checkboxes I filled in the url like this, check_box=154,153,152 etc... and turned that into an array Array ( [0] => 154 [1] => 153 [2] => 152 ) Array using $delete_selected = $connection->real_escape_string($_POST['check_box']); $check_box_array = explode(",", $delete_selected); Now I can't figure out how to put that into $sql = "DELETE FROM categories WHERE cat_id = '$delete_selected'"; it only deletes one. Any ideas? Hi there i need to make an array with the result from a query (numbers 1-4) I need to calculate how many occurances of each 1-4 number is in the table. Im new to this and havent much experience with arrays but ive managed to create an array and echo the results: 2 Class 3 Class 1 Class 3 Class 3 Class 2 Class 2 Class 2 Class So i need to somehow Add the occurance of them and echo the results e.g. 4 Class 2, 1 Class 1, 3 Class 3. If someone could please point me in the right direct that would be great. Code: [Select] $colname_Recordset1 = "-1"; if (isset($_SESSION['MM_Username'])) { $colname_Recordset1 = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']); } mysql_select_db($database_swb, $swb); $query = sprintf("SELECT Class FROM ships WHERE PlayerName = %s", GetSQLValueString($colname_Recordset1, "text")); $result = mysql_query($query, $swb) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo $row['Class']. " Class "; echo "<br />"; } mysql_free_result($result);?>; Thank You I'm using a web service call, my response comes back as an array:
stdClass Object ( [contractVehicle] => Array ( [0] => ITSchedule70 [1] => ITCommodityProgram ) ) I then need to hand this response off to anther developer who needs to know that "ITSchedule70" is the selected Contract Vehicle (actually there could be more than one, this only returns one for now), so he can insert it into a SQL query against a MySQL database (to match "Products" with the right "Contract Vehicle". So basically how do I do that? How to wrap it up and send it off, and how to insert a query? Hi I need to do a SELECT query like $query = "SELECT name FROM myusers WHERE myidnum = ".$_SESSION['myid'].""; and put the results into an array like $myusers = array("David","John","Lucy","Sarah"); But I cannot figure out how to do this Can anyone help? Thanks Hello just wondering if someone can help me... I have a select multiple form that sends the id numbers of records I want to get... how do I construct the query to the mysql db to do this? $array-$_POST['form-array'] so something like $r = ("SELECT column from 'Table' WHERE ID = (array, values, here)") Thanks for any help. hi guys, i want to retrieve data from database table using select query store the result into an array in database class and then in view class e.g in html i want to make an object of that database class and call the array and display the data into html table. someone help would be appreciated. Thanks in advance. Hello guys, Got a question here.. If I have a database table called features with the following columns with example f_id | f_name | f_status 1 | deck | 1 2 | Fireplace | 1 3 | Alarm | 1 I have another table another table called ads that has a column called features and it has the following 1,2,3 (for example) I want to be able to query the db and display all the f_name. Here is what I have so far but it's not working and need some help Code: [Select] // Query the ads table $result3 = mysql_query("SELECT * FROM ads WHERE ad_id='$id2'") or die(mysql_error()); $row3 = mysql_fetch_array( $result3 ); $features = $row3['features']; echo'This is the features ' . $features .''; $feature2 = explode(",", $features); print_r($feature2); echo "test" . $feature2[0]; // piece1 echo $feature2[1]; // piece2 $result = mysql_query("SELECT * FROM features WHERE f_id=$feature2[1]") or die("Sql error : " . mysql_error()); while($row = mysql_fetch_assoc ($result)){ $f_name=$row["f_name"]; echo $f_name; } Hi, I am creating a new menu (food) in my system. This consists of a menu, menu_items and menu_connection table. I can insert the menu name just fine and return its id just fine. When inserting the menu items, i need to get each of the menu_item_ids to use in the query that inputs the menu_connection. This is what i have so far: if ($_SERVER['REQUEST_METHOD']=="POST") { ///////////////////// //menu name insert // ///////////////////// $mname = mysqli_real_escape_string($conn, $_POST['newMenuName']); $stmt=$conn->prepare(' INSERT IGNORE INTO ssm_menu (menu_name) VALUES (?); '); $stmt->bind_param('s',$mname); $stmt->execute(); $menuInsId = $stmt->insert_id; echo $menuInsId; $stmt->close(); ///////////////////// //menu item insert // ///////////////////// $mitname = $_POST['newMenuItem']; $stmt=$conn->prepare(' INSERT IGNORE INTO ssm_menu_items (menu_item_name) VALUES (?); '); foreach ($_POST['newMenuItem'] as $k => $nmItem) { $mitname = mysqli_real_escape_string($conn, $nmItem); $stmt->bind_param('s',$mitname); $stmt->execute(); $menuItmInsId = $stmt->insert_id; echo $menuItmInsId; } $stmt->close(); /////////////////////////// //menu connection insert // /////////////////////////// $stmt=$conn->prepare(' INSERT IGNORE INTO ssm_menu_connection (menu_id, menu_item_id) VALUES (?,?) '); foreach ($_POST['newMenuItem'] as $k => $nmItem) { $stmt->bind_param('ii',$menuInsId, $menuItmInsId); $stmt->execute(); $connectionInserId = $stmt->insert_id; echo $connectionInserId; } $stmt->close(); } Currently it is inserting each of the items in the connection table with the same id - i understand why but i dont know how to collect up all of the ids to use later hi i have this function for select / function select($table, $rows = '*', $where = null, $group = null, $order = null, $limit = null) this is what i do to display the records. $db->select('loan'); $records = $db->getResult(); foreach($records as $row) { $user = $row['id']; //$name = $row['firstname']; //print_r($row); echo $user; ?> the codes is running.but when i want to use WHERE id = $_POST['id']; this is what im doing. $db->select('member')->where(array('id' => $_POST['client'])); an error as occured where in function where is not existing. please help how to use the proper way of using select * $table where id=$id; in array. thanks Hi, I have attached sql script for table structure and sample data. When I run following sql query ,I get result set. Code: [Select] select * from tablename order by col10 desc I want to create array using result set. Following is the require structure of array. Code: [Select] <?php $resultSet[]=array ( ('0') =>array ( ('col10value1') => array ( ('0') => array ( ('col1') => 'col1', ('col2') => 'col2', ('col3') => 'col3', ('col4') => 'col4', ('col5') => 'col5', ('col6') => 'col6', ('col7') => 'col7', ('col8') => 'col8', ), ('1') => array ( ('col1') => 'col1', ('col2') => 'col2', ('col3') => 'col3', ('col4') => 'col4', ('col5') => 'col5', ('col6') => 'col6', ('col7') => 'col7', ('col8') => 'col8', ), ), ), ('1') => array ( ('col10value2') => array ( ('0') => array ( ('col1') => 'col1', ('col2') => 'col2', ('col3') => 'col3', ('col4') => 'col4', ('col5') => 'col5', ('col6') => 'col6', ('col7') => 'col7', ('col8') => 'col8', ), ), ), ); ?> So my question is the following: i have a form where the user can search for multiple terms (green, red, black, orange etc) the terms are seperated by a coma when the form is submitted and then those terms are placed in an array. I then make for values matching those terms, using the following script: Code: [Select] //$tagArr = array(red, black); $makeQuery = mysql_query("SELECT * FROM points_tags WHERE tag_id IN('".implode("','", $tagArr)."')"); $num_results = mysql_num_rows($makeQuery); The table "point_tags" containts two columns one that has that holds a value id and the other holds the term, a value can have multiple terms so it looks kinda like this: tag_id term_id car red car black boat orange plane black You get the idea right? Anyway the current query is grabbing all values matching those terms in. However what i want to get done is grab the values that have those exact terms only, so in theory something similar to the following: Code: [Select] $makeQuery = mysql_query("SELECT * FROM points_tags WHERE tag_id="term1' AND tag_id='term2'"); Where if i search for red and black i only get car returned because car has both the terms red and black, while plane has black it does not contain the term red. Does that make sense? I've been trying to figuring this out myself for a while already and i just can't think of a way to do this. I hope someone here can help me out EDIT::: What i meant to accomplish with the second query example was that i need to accomplish that same type of task (or using OR ) but while keeping the values in an array, so again in theory that (tag_id='term1' AND tag_id='term2') would just keep incrementing based on the number of values i'm searching for, so if there is only one value been searched for then there would not be any "AND". The AND or OR would get added if there are more than one value. Make sense what i'm talking about? Hello Guys, I have a question I have the following query $result3 = mysql_query("SELECT * FROM table1 WHERE ad_id='$id2'") or die(mysql_error()); $row3 = mysql_fetch_array( $result3 ); // Grab all the var $features = $row3['features']; I am turning it into an array (comma separated) $feature2 = explode(",", $features); print_r($feature2); ) The result is like so Array ( [0] => 5 [1] => 9 [2] => 13 I want to query the features for just the ids (F_name) are for the features . This query will show all.. I would like to just display the f_name values from the array query. // build and execute the query $sql = "SELECT * FROM features"; $result = mysql_query($sql); // iterate through the results while ($row = mysql_fetch_array($result)) { echo $row['f_name']; echo "<br />"; } Please Advise.. Thanks, Dan I have tried many ways of storing an array of data that comes from a multiple selection form into a mysql table, including serialization. I am currently trying to do it by forming a string, and using the string in the insert query, but keep getting errors, or just nothing happening. I am not sure what I am doing wrong. Code: [Select] $categoryString = array(); if ($categoryArray){ foreach ($categoryArray as $category){ $categoryString[] = $category.'<br />';} } $categoryquery= "INSERT INTO categoryname VALUES('$categoryString')"; mysql_query($categoryquery); Thank you for your time as always. Hi guys, I have a simple query he $sql="SELECT user_id FROM $tbl_name WHERE email='$loginuseremail' AND password='$loginpassword'"; $result=mysql_query($sql); $row = mysql_fetch_assoc($result); //START SESSION session_start(); $_SESSION['username']=$loginuseremail; $_SESSION['user_id']=$row; echo $_SESSION['user_id']; It should obviously output an ID number but instead I just get the word 'Array'. If I remove the mysql_fetch_assoc() I just get 'Resource ID #4'. Can anyone point me in the right direction? Thanks! when i run the below code i get an error...
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in...
but when i take out the h>0 then there is no errors in the code. in table users, i need all columns in the array. How to get h>0 working?
$query = "SELECT * FROM users WHERE h>0 ORDER BY id"; $result = mysqli_query($link, $query); $data = array(); while($row1 = mysqli_fetch_array($result)) { $data[] = $row1; } print_r($data); Edited by kalster, 27 October 2014 - 07:38 PM. Hey Guys, I need some assistance, hopefully there is just a simple command that I am not aware of or an easy answer to this. Suppose I have an array with x amount of values... $arr = array("111111" => 10, "222222" => 20); And i have a query.. mysql_query("SELECT * FROM table WHERE table.zip = "111111" "); Suppose I want to modify the query, so it can check if table.zip exists anywhere in $arr, regardless of length of array. And Select the value that the array location refers to. So I need it to check if "111111" is anywhere in the array, and then take the value 10. The example I am applying this to is that I have a database full of business addresses/zip codes and a database full of every zip code in country. So I have a script to get every zip within x miles, and the distance from it, but need to be able to apply it when I query my business addresses to see if it exists within x miles. Thanks! Greg Hi, I want to develop array like following Code: [Select] $BCD=array('type' =>'TYPE1', array( 0=>array('column1'=>'value1','column2'=>'value1','column3'=>'value1','column4'=>'value1','column5'=>'value1','column6'=>'value1','column7'=>0), 1=>array('column1'=>'value1','column2'=>'value1','column3'=>'value1','column4'=>'value1','column5'=>'value1','column6'=>'value1','column7'=>0), 2=>array('column1'=>'value1','column2'=>'value1','column3'=>'value1','column4'=>'value1','column5'=>'value1','column6'=>'value1','column7'=>0), 3=>array('column1'=>'value1','column2'=>'value1','column3'=>'value1','column4'=>'value1','column5'=>'value1','column6'=>'value1','column7'=>0), ) )); I have written following code to achieve the same but not getting result. Code: [Select] $sql = "select * from tablename "; $result = mysql_query($sql); $k=0; while ($row = $db->mysql_fetch_array($result)) { // array_push($BCD['type'],$row['type']); $BCD1=array('type' =>$row['type'], array( $k=>array('column1'=>$row['column1'],'column2'=>$row['column2'],'column3'=>$row['column3'],'column4'=>$row['column4'], 'column5'=>$row['column5'],'column6'=>$row['column6'],'column7'=>$row['column7']) )); $k++; } I'm having trouble getting all of the results out of a query array, as it is I only get the first result and nothing else. Here's the basic code I'm working with: Code: [Select] $query = "SELECT data_txt FROM jos_servicedirectory_fields_data WHERE fieldid = 19 AND itemid = $item->itemid"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ $listtags = $row['data_txt']; $tags = "$listtags, "; $title = "<div class='servicedirectoryItemTitle'><table class=\"sdlistingitemtitle\" cellspacing=\"5\"><tbody><tr>$listingimage<td style=\"vertical-align:top;width:690px;\">$listingbasicicon<span class=\"$listingtitleclass\">$listinglogo<a href=\"{$href}\" {$onClick} title=\"{$item->title}\">{$item->title}</a></span><br /><span class=\"listingdescription\">$listingdescription</span></td><td style=\"vertical-align:top;\">$featuredribbon$moreinfobasic</td></tr></tbody></table><div class=\"listingbottom\">Tags:<span class=\"listingtags\"> $tags </span></div></div>"; } I've also tried using a foreach loop thinking that would pop all of the results but I end up not getting anything at all then. I'm guessing I'm setting the foreach loop up wrong. Here's how I'm trying to do it: Code: [Select] foreach($listtags as $value) { $tags = $value; } Does it have something to do with sticking the $tags variable in the $title variable? I wouldn't think that would matter, but the strange thing is I use this exact same query in a different part of this component and just echo it directly and it works fine. |