turpentyne Posted July 26, 2010 Share Posted July 26, 2010 I'm a newbie just trying to get an idea of whether my idea is possible and how, before I sit down to try writing this code (because it's on a page that's got a heck of alot of code on it already) I want to access a specific table, then pull the info from one column from every row that matches a specific variable, and then use that array to insert information into a second table. I'm getting the sense that WHILE and mysql_fetch_array is how I might do this. Or will it only give me the entire row? Can someone give me just a quick idea how I'd do this so I'm not flying too much in the blind? thanks! Quote Link to comment https://forums.phpfreaks.com/topic/208949-general-question-on-mysql_fetch_array/ Share on other sites More sharing options...
wildteen88 Posted July 26, 2010 Share Posted July 26, 2010 Sound like you only need to use a simple sql query. SELECT column_name FROM table_name WEHERE column_name='some value to match' Quote Link to comment https://forums.phpfreaks.com/topic/208949-general-question-on-mysql_fetch_array/#findComment-1091386 Share on other sites More sharing options...
turpentyne Posted July 26, 2010 Author Share Posted July 26, 2010 oh? haha! in other words, I might be overthinking this one. heheh! beginners! Probably because I'm still rough at trying to figure out arrays. it would be several rows. Like searching a table for any column that matches U.S.A.. It would return 50 states. Quote Link to comment https://forums.phpfreaks.com/topic/208949-general-question-on-mysql_fetch_array/#findComment-1091389 Share on other sites More sharing options...
turpentyne Posted July 26, 2010 Author Share Posted July 26, 2010 I quickly wrote this out. Is this close to what I would want to do? $query = ("SELECT column2 FROM table1 WHERE column1 = $variable") $result = mysql_query($query); if (!$result) { while ($row = mysql_fetch_assoc($result)) { INSERT INTO table2 (columnA) VALUES ($row['column2']) } Quote Link to comment https://forums.phpfreaks.com/topic/208949-general-question-on-mysql_fetch_array/#findComment-1091393 Share on other sites More sharing options...
wildteen88 Posted July 26, 2010 Share Posted July 26, 2010 Yes thats sort if it. However you should note that MySQL has a SELECT INTO statement, which will write the results of an SQL query into a table. An example can be found here http://www.w3schools.com/sql/sql_select_into.asp Quote Link to comment https://forums.phpfreaks.com/topic/208949-general-question-on-mysql_fetch_array/#findComment-1091395 Share on other sites More sharing options...
turpentyne Posted July 26, 2010 Author Share Posted July 26, 2010 ah! Now that I've looked into it, that makes it alot simpler. But now that leads me to another question. I didn't mention that I have another variable (from a form) that has to be combined when inserting into the second table. Each row would be: column 1 that comes from the form, and column 2 that comes from the first table. Is there a way to do this using SELECT INTO? I saw some words used like UNION. Does that work to combine a form and a table query? Quote Link to comment https://forums.phpfreaks.com/topic/208949-general-question-on-mysql_fetch_array/#findComment-1091399 Share on other sites More sharing options...
tomtimms Posted July 26, 2010 Share Posted July 26, 2010 You could use an Insert Into Statement as well. INSERT INTO "TABLE_NAME"(values) SELECT (values) FROM "Table_NAME" WHERE column ="?" Quote Link to comment https://forums.phpfreaks.com/topic/208949-general-question-on-mysql_fetch_array/#findComment-1091400 Share on other sites More sharing options...
turpentyne Posted July 26, 2010 Author Share Posted July 26, 2010 aha! that sounds like exactly what I'm trying to do. Awesome. Another lesson learned. thanks. Quote Link to comment https://forums.phpfreaks.com/topic/208949-general-question-on-mysql_fetch_array/#findComment-1091401 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.