PHP - Foreach Item In Array Query Mysql Database Trouble
Hi All,
First time posting here. I've googled the problem, but can't seem to find a response that's the same. All I want to do is have a list of id numbers and for each id number in the array, submit a MySQL query to retrieve information relating to the id number. When I execute the code below however, I end up with only the last item in the array being printed in the echo statement. Any clues? Thanks, Code: [Select] // get array of ids $ids = getIDs($ids); // loop through input list foreach ($ids as &$id) { getVarDetails($id); } function getVarDetails($local) { $con = mysql_connect('localhost:3306', 'root', '********'); if (!$con) { die('Could not connect: ' . mysql_error()); } // set database as Ensembl mysql_select_db("Ensembl", $con); $result = mysql_query("SELECT * FROM variations WHERE name = '$local' LIMIT 1"); $row = mysql_fetch_array($result) while($row = mysql_fetch_array($result)) { echo $row['name'] . " " . $row['id']; echo "<br />"; } // close connection mysql_close($con); } Similar TutorialsHi guys, i am currently working on a project that queries an inventory database through the use of radio buttons, for example; If the first button FOR THE Item(HAMMERS)is clicked, the output should be the name of the item, the number sold and the total profit(calculations) into a table. the way i have it now, when io click on the radio button my output comes back as the results for all the items in the table, i want it to display only the information about hammers, then only about each individual item when the corresponding radio button is clicked. any help would be greatly appreciated!!! Ok, this may be just because I have been programming all day and my mind has gone blank (happens alot), but this is my PHP script: Code: [Select] <?php $query_distinct_item_types = mysql_query("SELECT DISTINCT name FROM item_types"); while($item_types = mysql_fetch_array($query_distinct_item_types)){ $distinct_item_types[] = $item_types['name']; } foreach($distinct_item_types as $item){ $query_item_total = mysql_query("SELECT item_type, SUM(price) WHERE item_type='$item' FROM costs GROUP BY item_type"); while($item_total = mysql_fetch_array($query_total_price)){ $item_totals[] = $item_total['SUM(price)']; } } $item_summery = $item_totals; ?> $item_summery which is = to $item_totals is returning null, any idea's? I have a table called "playlists", and a table called "musics". In the musics table, there is a column playlist_id which references the playlist that each music is contained.
I'm using api calls to display information on the site with JavaScript, so I need to return a JSON.
I need the json with the following structu
[
Playlists: [
{
Name: "etc",
musics: [
{
name: "teste.mp3" }, { name: "test2.mp3" } ] }, ... ] ] And this is my code: $query = $con->prepare("SELECT * FROM playlists WHERE user_id = :id"); 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. i have 8 division (div), i want to display 4 rows in 4 division and the remain 4 rows in the next 4 division here is my code structure for carousel
<div class="nyie-outer"> second row third row
fourth row fifth row sixth row seven throw eighth row
</div><!--/.second four rows here-->
sql code
CREATE TABLE product( php code
<?php how can i echo that result in those rows
This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=313679.0 Hello. Many thanks for your help. I am writing a PHP/MySQL dating-site and have hit a programming impass. I have a database full of members and a search form consisting of checkboxes. So to search, a member ticks say...gender: female; age: 21,22,23,24,25,26; height: 5'4",5'5",5'6",5'7"; county: cornwall,devon,somerset How can a run a check on the database selecting all entries that fall into the selected criteria. For example a 23 year old female of 5'5" living in Cornwall and a 26 year old female of 5'4" living in Somerset? The key index of my database is 'id' and the fields a age,height,county The names of the form checkboxes a Gender: male, female; Age: 21,22,23,24 etc; Height: 5_4,5_5,5_6 etc; county: cornwall,devon etc I am very new to php and am trying to create a simple application that uploads a PDF file to a database. I have one field for the Volume Number and on file field for the PDF to be uploaded. My issue is i can't get the PDF to upload or insert the name of the pdf (eg volume1.pdf) into the data base. I would also like to point out that I know i have a low post count, but i only seek help when i truly need it and have exhausted all other resources... Here is what i have, please go easy on me this is my first round at php: Code: [Select] <?php if(isset($_POST['submit'])){ $vol_num = $_POST['vol_num']; $pdf = $_FILES['pdf']['name']; $path = '../pdf/'.$_FILES['pdf']['name']; move_uploaded_file($_FILES["pdf"]["tmp_name"], $path); mysql_query("INSERT INTO volumes set vol_num='$vol_num', vol_link='$pdf'") or die (mysql_error()); echo "<script>location.href='add_volume.php'</script>"; } ?> what am i doing wrong here? Thanks in advance I am trying to insert values stored within two dimensional array into mysql database but it does not work as I would expect it. The locations in mysql are defined as char length of 2. When I print_r the array it shows: Array( [0] => Array ( [0] => 04 [1] => 22 [2] => 27 [3] => 28 [4] => 39 [5] => 43 [6] => 47 )) but when I insert them into the mysql like this: Number1A='$MaxMillionsNumber[0][0]', Number1B='$MaxMillionsNumber[0][1]', Number1C='$MaxMillionsNumber[0][2]', Number1D='$MaxMillionsNumber[0][3]', Number1E='$MaxMillionsNumber[0][4]', Number1F='$MaxMillionsNumber[0][5]', Number1G='$MaxMillionsNumber[0][6]'; my values in mysql all show as Ar What am I doing wrong? So 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? I am building a project that requires I store query strings in a table (stage_reqs) which are called to determine permissions. These strings will look something like this: Code: [Select] select salesman from jobs where salesman is not NULL and job_id='".$this->job_id."' limit 1 The variable value needs to be determined from within the function it is being accessed in. Can I use eval to do this? Thanks, Chris I have posted one set of values into my database and it worked fine but when i input another set they wont go inside unless i changes the value of the primary index colum. I want to be able to insert a new values regardless of the primary index value. Any idears...? Say a user puts in a support request, and for every request it generates a unqiue string, and enters it into the database. Ok, now say there is a text field, when the user enters their unique string and it finds a match, it displays the data along with it. How can I accomplish this? Im kind of new to mysql, but I know basic SQL. Would be great if somebody could point me in the right direction! Thanks 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; } 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. 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 all, I have the following code that queries a MySQL database and outputs results on-screen. Code: [Select] $result= mysql_query (" SELECT email,firstname,lastname FROM tablename WHERE ID IN('".$IDs."') "); if (!$result) {die('Error: Could not search database. ' );} while($row = mysql_fetch_assoc($result)) { echo $row['firstname']," ",$row['lastname'],"<br />"; $emails[]=$row['email']; } var_dump($emails); var_dump($IDs); However, the two var_dump's output the following (in the case of 2 results for the query): array(6) { => string(14) "email1@email.com" [1]=> string(23) "email2@email.com" [2]=> string(14) "email1@email.com" [3]=> string(23) "email2@email.com" [4]=> string(14) "email1@email.com" [5]=> string(23) "email2@email.com" } string(67) "9543219aa68ffa1d434dc8530f23fe48','d4384b2b493867706b0bcedda012ed48" ($IDs is an imploded array) Even though the MySQL table only contains one email per ID, both emails are listed 3 times in $emails, instead of just once. Can anyone see why this is? I'm stuck. Thanks! There is a "PHP ajax cascading dropdown using MySql" at codestips.com/php-ajax-cascading-dropdown-using-mysql/ I want to use this technique but with a XML or array file instead of mysql database, but my knowledge about mysql is very low. How I can modify this code to catch the categories and products from an array, instead of mysql database? Code: [Select] $connect=mysql_connect($server, $db_user, $db_pass) or die ("Mysql connecting error"); echo '<table align="center"><tr><td><center><form method="post" action="">Category:<select name="category" onChange="CategoryGrab('."'".'ajaxcalling.php?idCat='."'".'+this.value);">'; $result = mysql_db_query($database, "SELECT * FROM Categories"); $nr=0; while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $nr++; echo "<option value=".'"'.$row['ID'].'" >'.$row['Name']."</option>"; } echo '</select>'."\n"; echo '<div id="details">Details:<select name="details" width="100" >'; $result = mysql_db_query($database, "SELECT * FROM CategoriesDetails WHERE CategoryID=1"); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "<option value=".$row['ID'].">".$row['Name']."</option>"; } echo '</select></div>'; echo '</form></td></tr></table>'; mysql_close($connect); ajaxcalling.php is Code: [Select] include("config.php"); $ID=$_REQUEST['idCat']; $connect=mysql_connect($server, $db_user, $db_pass); echo 'Details:<select name="details" width="100">'; $result = mysql_db_query($database, "SELECT * FROM CategoriesDetails WHERE CategoryID=".$ID); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "<option value=".$row['ID'].">".$row['Name']."</option>"; } echo '</select>'; mysql_close($connect); |