MySQL, Turn Column Into Array
Today is the first time I've used MySQL with PHP.
What is the best way to turn a column into an array?
This is how I am doing it at the moment, surely this is sub-optimal:
$query = 'SELECT something FROM table'
$result = mysql_query( $query );
$i = 0;
while( $arrayTemp = mysql_fetch_array( $result ) )
{
$array[ $i++ ] = $arrayTemp[ 0 ];
}
echo "<ul>";
foreach( $array as $string )
{
echo "<li>$string</li>";
}
echo "</ul>";
View Complete Forum Thread with Replies
Related Forum Messages:
Loading A MySQL Column Into An Array
ok i have a forum i am creating and i store the id of the forums where people are moderators in their userinfo. i speperate them with a comma. i need to either load them into an array or use explode() but i'm not sure how to create the array. i'm thinking that using an array would be faster because i could search in it to find out if the user is a mod.
View Replies !
Select Single Column From Mysql Into Array
Is there a way to select a single column from a mysql database and directly put the results into an arrray? Here is the workaround below, but I would like to elimniate the array_push step: $uids=array(); $res = mysql_db_query("db", 'select uid from tbl;', $link); while ($row = mysql_fetch_row($res)) { array_push ($uids, $row[0]); }
View Replies !
Turn Array Into Multi-dimensional Array?
How can I take an existing array and turn it into a multi-dimensional array? I want to get a single row out of a MySQL table and have it in an array, but have along side the values the true field (column) length for each of the fields from the table. Here's my stab at it: $userDetails = mysql_fetch_array($resultID); $i = 0; while ($i < mysql_num_fields($resultID)) { $userDetails[$i][2] = mysql_field_len($resultID, $i); $i++; } And I guess the follow up question (if this is indeed possible) is will this work with a multi-row return from the DB also?
View Replies !
How Do I Turn A Variable Into An Array?
I'm having trouble understanding this of finding an example. How do I make a variable like this: $state = " US:AK:Alaska| US:AL:Alabama| US:AR:Arkansas| US:AS:American Samoa| US:AZ:Arizona| "; Into an array? I then want to cut each array value into its three parts so I can convert the state code to the actual name.
View Replies !
Use A PHP Array And Turn It Into Am Xml Request For Ajax Autocomplete?
I recently was assigned to a project that deals with AJAX, something I have never dealt with before. I do know what I am doing with javascript/php/mysql though. I just installed a plugin that I know how to use every part of it, except where it processes the request. This is an autocomplete plugin. It says I need to format my data like this <?xml version="1.0"?> <ajaxresponse> <item> <text><![CDATA[text to display]]></text> <value><![CDATA[value to fill]]></value> </item> </ajaxresponse> Is there a way to do that with PHP using an array of data I have?
View Replies !
Turn Searched Results Into Links, Using Mysql
Using mysql and php, I have managed to patch together a simple search facility using php to access my database of products. Upon clicking search it returns each result a line at a time. What I need to be able to do is turn these results into links that would go to secific product pages chosen. Also formatting the results that come from the search I would like to be able to decide where for example the manufacturer, a description, and a price should be listed on the page. Code:
View Replies !
Need Column Names, Not The Array Key...
I am running a script that exports a MySQL table into an excel file. The problem I am having is the column (header) names are not showing up in the final .xls file. I instead get the key value 0, 1, 2, etc. Here is the section of the code that should write the actual names. I can't figure out what's wrong. It looks good to me. Am I missing something? Code:
View Replies !
PHP - Column Names In Array
see the code below <? $rsQry = mysql_query ("select * from tblCustomer"); $arrQry = mysql_fetch_row($rsQry); ?> Now if I try to fetch a Customer's Name from the "Customer_Name" column from the "tblCustomer" table with the following code then I dont get anything: <? echo $arrQry["Customer_Name"]; ?> Why?
View Replies !
How To Call A Column Instead Of Row In A Multidimensional Array
I have a 2-d array. I want to pass columns from the 2-d array to a file that will graph the columns. Let's say its called my2darray I can pass a row with something like my2darray[4] But I can't pass a specific column with my2darray[][4] Any help with the syntax or a link to the solution would be helpful. Do I have to use a loop to call a whole column?
View Replies !
Recursive Array From A Twin-column Table
I have a MySQL table with two columns: catagory and parent. Parent contains the parent catagory of the catagory. I need to query the db ONCE, assigning the info to an array, then create a recursive array parenting catagories and grouping accordingly.
View Replies !
Structure The Data In Column That Should Be Index Of Array
MySQL Table structure. ID name value 1 c01 abc 2 c01 def 3 c02 xyz Here i want to structure the data in such a way that name column should be index of array and values should be mapped to index for eg. Array( [1] => Array ( [c01] => array([0] => abc [1] => def [2] => Array ( [c02] => array([0] => xyz .
View Replies !
Assign Column Names As Array Keys
Want to create an array that has db column names for the key names and field values as the array values. Is there an easy way to do this? If not possible directly, is there a way in php/mysql to get a list of column names in a table such that I could use those values as the keys?
View Replies !
MySql Column ??
What i want to do is this. I have an field in one of my database tables with money price of product, i want to display the total amount of the table, i try to use "select sum(fieldname) from table where" but is not working.
View Replies !
Mysql Add Column
$date = date("F j, Y"); $new = "ALTER TABLE closing ADD May 9, 2007 VARCHAR(30) NOT NULL;"; Is this wrong? mysql says : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near Ə, 2007 VARCHAR(30) NOT NULL' at line 1 Whts The prob?
View Replies !
MySQL Column
I was looking through the WordPress (blog platform) database structure and was curious about something: for the user table, the id column is bigint(20) and for the post table, the associated user id column is int(4). I was just wondering if the two columns are different types for a specific reason, since it seems they hold the same info.
View Replies !
Alter A MySQL Column With Php
I have a table named sentcards with a field called cardid with a current type int... I wish to change the type int to bigint(30) with an attribute that is unassisgned and a default of 0... This is my code:
View Replies !
Mysql Longtext Column
I have a column in a mysql table defined as longtext data type. I am trying to search for key words using php. I know that mysql indexes the first 1k (1024 chars) of data but I can't seem to even search the first byte.
View Replies !
MYSQL TEXT Column
I have a table in mysql with this column called 'keywords'. in eah row, the keywords column consists of a set of words separated by spaces, ex: row1 keywords = 'stuff boing foo'. row3's keywords column might contain SOME of the keywords that were in row1's keywords, and it might contain a few others too. I want to query the table and get a php array of all the UNIQUE keywords in the entire table -- in other words I don't want to have any duplicates in the array.
View Replies !
Displaying MySQL Column Names Using PHP
I'd like to be able to display the column names of a table on a page. Is there a way to read that information in using SHOW? And then to display that information again? Or would I have to store these names in a seperate table and display these values? I tried this, but it sure didn't work: $result = mysql_query ("show columns from industries"); print ("<table>"); if ($row = mysql_fetch_array($result)) { do { print ("<tr><td bgcolor=#cccccc>"); print $row["field"]; print ("</td><td bgcolor=#cccc00>"); print $row["type"]; print ("</td><td bgcolor=#cccc00>"); print $row["null"]; print ("</tr>"); } while($row = mysql_fetch_array($result)); } else {print "Sorry, no records were found!";} print ("</table>");
View Replies !
Check If Column In Mysql Exists With Php
I wanted to know if you can let php check if a column exists in a table. cuz i have a 1 script script and i want it to check a table for having the following columns and if 1 o fthem is missing it would automaticly add the column.
View Replies !
Get Mysql Unique Column Fields
I have data with the field name "monthyear" and I am trying to get a list of all the different field names without repeating like "may2003, january2001, june2002", this is for my news script i am making, im going to use it to make an archives page which is organized by month any ideas?
View Replies !
Should I Add A Multi Column Index To MySQL?
I have a mysql table. Two columns in the table are used to pull out stories from the table. One of the columns stands for categories of the stories, and the other column is the primary key and has the id for each story. I am thinking of writing queries like this: mysql_query("SELECT * FROM articles WHERE category='Science_Fiction' AND story_id=ཀྵ'"); I am wondering if I should create a multi column index that covers both "categories" and "story_id" columns. -- INDEX (categories, story_id) Would this increase performance?
View Replies !
Numeric Column Names In Mysql?
i am trying to create a table with a column name called 2004. But I keep getting a mysql error? Is there any workaround? Unable to execute: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near �� TEXT,2004_reference TEXT,2005 TEXT,2005_reference TEXT,200 CREATE TABLE test (id int(6) NOT NULL auto_increment, membership_number TEXT,first_name TEXT,last_name TEXT,spouse TEXT,phone_priv TEXT,phone_bus TEXT,address_street TEXT,addr_town TEXT,addr_city TEXT,membership TEXT,donor TEXT,1998sa TEXT,1999sa TEXT,2000sa TEXT,200s1 TEXT,2002sa TEXT,2003sa TEXT,2003_reference TEXT,2004 TEXT,2004_reference TEXT,2005 TEXT,2005_reference TEXT,2006 TEXT,2006_reference TEXT, PRIMARY KEY (id),UNIQUE id (id))
View Replies !
Question About Datetime Column In Mysql
i tried this question in the mysql group, but have not heard anything. i have a database with about 20 tables. The some of these tables have datetime type columns in them. What i am basically wondering is if there is a way to make a default value of now() apply if it is not specified when the row is created. i know this is possible with a timestamp, but is it possible with a datetime column ? and if so, how ?
View Replies !
Exclude A Column From A Mysql Command
Is there a way to exclude one specific column in a mysql database from a command. IE: I want to use the command $sql = "TRUNCATE TABLE DailyDataSF_copy"; But have it affect all columns except just one specified column that'll stay the same. Any solutions to this? The table has like 90+ columns, which is why I'd like to do it this way if at all possible.
View Replies !
MySQL Order By For Varchar Column
I have a VARCHAR column where most rows start with numbers; however, a few rows start with letters. I have ordered the rows to appear in descending order. SELECT * FROM db ORDER BY varcharcolumn DESC With this query the numbers appear in the desired order, but the letters appear before the numbers. This order makes sense based on my query but I don't want the letters before the numbers. Is there a way to force the letters to appear after the numbers?
View Replies !
MySQL Multi-column Indexes
I'm setting up indexes for a number of mysql tables. In some cases, these multi-column indexes (of type INDEX) include the primary key. For instance, in table Users, where pkey is UserID, I also have a two-column index on UserID and UserType (to accommodate queries that look at both these values). phpMyAdmin is complaining that "Primary and Index keys should not both be set for column "UserID" I'm wondering what I should do - do I leave UserID out and just index UserType (and assume that MySQL will use both indexes)? Or is phpMyAdmin wrong?
View Replies !
Column Values Into MySQL Textfile
I have a list of email addresses which I would like to insert into a MySQL (version 2.6.0) table which has the fields id, username, pw, email, lastupdated. Is there a way to adapt the following code to insert all of them at once. I basically want to ignore id, username,pw, lastupdated. INSERT INTO mail(email) VALUES ('bob@bob.com'); INSERT INTO mail(email) VALUES ('geoff@geoff.com');
View Replies !
Variable As Column Name In MySQL Statement
Let's say db has 4 cols: user id, favorite color, shoe size, hair color. User only has the option of entering 2 variables. 1 has to be user id, 2nd one (radio button) can be color OR size OR hair color. Depending on what user entered, php should look up user id #, and then locate parameter under variable 2 col (whatever was entered for variable 2: fave color or shoe size or hair color) that belongs to that user id.
View Replies !
MySQL Auto-increment Id Column
need to set-up a mySQL database with a unique id, so I can have an update section. I have a site with a "events" page (a list of events, date, event name and event description) I have a form with 3 fields (date, name and description)... I need to do 4 things... insert records (not a problem) display records (not a problem) update records (can't figure how to update, need id)
View Replies !
How To Display Each Mysql Row In A Table Column
After I retrieved 3 rows from my mysql product database, I want to display each row in its own column, similar to what you see on this product comparison page. Each row also contains 15 different specs, FYI. reviews.cnet.com/4504-5_7-0.html?id=30569389&id=20536585&id=30525842&id=30527150&tag=compare I know how to display mysql result on a page, but this format presents major difficulty for someone with limited PHP knowledge like myself.
View Replies !
MySQL Number Of Rows In A Column
I have small problem with mysql, i have a large database and one table suppose to have 70 000 rows unfortunately it fits only 65535, i can't add more its seems to be a limit. Is this a hosting setting or is this just a MySQL (or any database) limit?
View Replies !
Searching A Mysql Column Against Multiple Variables
I have an array of zipcodes which I am trying to check against a mysql table. I want to search in the column "zipcode" in a table of a bunch of entries. I want to search each rown to see if the value of the zipcode column is listed within my array. This could be said vis vera, that I want to check the mysql table to see if any of the rows have one of the values from the array inside the zipcode column. I've tried the statement "select * from table where zipcode IN ('$zipsearch')", but it seems to return an odd sequence of data. It doesn't seem thurow enough. similarly the entire statement is : select * from table where description like "%$trimmed%" OR title like "%$trimmed%" AND zipcode IN ('$zipsearch') order by date" I can't seem to get the statemtent to break up my requests right so that the logistics of teh statement are : select from table where (descriptions like variable or title like variable) AND <zipcode search problem>
View Replies !
How To Display Newly Added Mysql Column
I have a mysql table and i want the user to add columns using a php interface and then display the table with the newly added column. I was thinking about putting all the columns names in a special table but there must be another more efficient way. as an example this is my query to display the table: $result = mysql_query("select * from news"); while($r=mysql_fetch_array($result)) { $title=$r["title"]; $message=$r["message"]; //display the row echo "$title <br> $message <br>"; } now if the user alter the table and add a column "rank", how can i update my query automatically so it can display the newly added row (or column)?
View Replies !
MySQL Query Returning Empty Column
Here's my code: $query_get_machine = sprintf("SELECT machines.ID, machines.NotifyID, machines.Name, notify.ID, notify.`Data` FROM machines RIGHT JOIN notify ON machines.notifyID = notify.ID WHERE machines.ID = %s", GetSQLValueString($colname_get_machine, "int")); Can anyone tell why the notify.`Data` column might be returned empty? All others display as they should.
View Replies !
|