dreamwest Posted September 22, 2009 Share Posted September 22, 2009 How can i get a mysql query into an array? I cooked up this but it doesnt work $result = mysql_query ("SELECT * from mains where scan=0 order by main_id asc limit 10 ") or die("Fail"); while($row = mysql_fetch_assoc( $result )){ $links .= "http://".$row['main']."' ,'"; } $links = substr($links, 0, -4); $links = "'{$links}'"; echo $links; $nodes = array($links); Quote Link to comment https://forums.phpfreaks.com/topic/175088-mysql-query-in-an-array/ Share on other sites More sharing options...
trq Posted September 22, 2009 Share Posted September 22, 2009 A query is a string, nothing more. Do you mean you want mysql results in an array? while($row = mysql_fetch_assoc( $result )){ $links[] = "http://".$row['main']; } Links is now aan array containing your results. Quote Link to comment https://forums.phpfreaks.com/topic/175088-mysql-query-in-an-array/#findComment-922792 Share on other sites More sharing options...
PravinS Posted September 22, 2009 Share Posted September 22, 2009 Use below given function, it will give you 2 dimension array function select_row($sql) { $rs = mysql_query($sql); if (!$rs) return false; else { while($row = mysql_fetch_array($rs,MYSQL_ASSOC)) { $res[] = $row; } return $res; } } Quote Link to comment https://forums.phpfreaks.com/topic/175088-mysql-query-in-an-array/#findComment-922799 Share on other sites More sharing options...
dreamwest Posted September 22, 2009 Author Share Posted September 22, 2009 Yes. Something like this. $result = mysql_query ("SELECT * from mains where scan=0 order by main_id asc limit 10 ") or die(mysql_error()); $row = mysql_fetch_array( $result ); $nodes = array($row[1]); But for all the results and not just one Quote Link to comment https://forums.phpfreaks.com/topic/175088-mysql-query-in-an-array/#findComment-922801 Share on other sites More sharing options...
dreamwest Posted September 22, 2009 Author Share Posted September 22, 2009 Use below given function, it will give you 2 dimension array function select_row($sql) { $rs = mysql_query($sql); if (!$rs) return false; else { while($row = mysql_fetch_array($rs,MYSQL_ASSOC)) { $res[] = $row; } return $res; } } I tried it but it didnt work, heres what i did: function select_row($sql) { $rs = mysql_query($sql); if (!$rs) return false; else { while($row = mysql_fetch_array($rs,MYSQL_ASSOC)) { $res[] = $row; } return $res; } } select_row("SELECT * from mains order by main_id asc limit 10 "); $nodes = array($res); Quote Link to comment https://forums.phpfreaks.com/topic/175088-mysql-query-in-an-array/#findComment-922803 Share on other sites More sharing options...
PravinS Posted September 22, 2009 Share Posted September 22, 2009 Use like this $res = select_row("SELECT * from mains order by main_id asc limit 10 "); echo "<pre>"; print_r($res); echo "</pre>"; Quote Link to comment https://forums.phpfreaks.com/topic/175088-mysql-query-in-an-array/#findComment-922808 Share on other sites More sharing options...
dreamwest Posted September 22, 2009 Author Share Posted September 22, 2009 Use like this $res = select_row("SELECT * from mains order by main_id asc limit 10 "); echo "<pre>"; print_r($res); echo "</pre>"; Yes that printed it out onto the screen, but how can i now use it as an array $res = select_row("SELECT * from mains order by main_id asc limit 10 "); $nodes = array($res); $node_count = count($nodes); Quote Link to comment https://forums.phpfreaks.com/topic/175088-mysql-query-in-an-array/#findComment-922814 Share on other sites More sharing options...
PFMaBiSmAd Posted September 22, 2009 Share Posted September 22, 2009 Function calls that return something must either assign what is returned to a variable or use the function call directly in another function as a parameter. Quote Link to comment https://forums.phpfreaks.com/topic/175088-mysql-query-in-an-array/#findComment-922815 Share on other sites More sharing options...
PravinS Posted September 22, 2009 Share Posted September 22, 2009 Use for loop like this, replace fieldnam1, fieldname2 .... with your table fieldnames for($i=0;$i<count($res);$i++) { echo $res[$i]['fielname1']." ".$res[$i]['fielname1']; } Quote Link to comment https://forums.phpfreaks.com/topic/175088-mysql-query-in-an-array/#findComment-922818 Share on other sites More sharing options...
trq Posted September 22, 2009 Share Posted September 22, 2009 Why do you insist on passing what is already an array ($res) to the array function? eg; $nodes = array($res); This simply makes your one dimensional array into a two dimensional array. Quote Link to comment https://forums.phpfreaks.com/topic/175088-mysql-query-in-an-array/#findComment-922823 Share on other sites More sharing options...
PravinS Posted September 22, 2009 Share Posted September 22, 2009 I have not insisted to use array($res) in the code. Quote Link to comment https://forums.phpfreaks.com/topic/175088-mysql-query-in-an-array/#findComment-922831 Share on other sites More sharing options...
trq Posted September 22, 2009 Share Posted September 22, 2009 I have not insisted to use array($res) in the code. Obviously I wasn't talking to you. Quote Link to comment https://forums.phpfreaks.com/topic/175088-mysql-query-in-an-array/#findComment-922833 Share on other sites More sharing options...
PravinS Posted September 22, 2009 Share Posted September 22, 2009 Oh I am really sorry Quote Link to comment https://forums.phpfreaks.com/topic/175088-mysql-query-in-an-array/#findComment-922835 Share on other sites More sharing options...
dreamwest Posted September 22, 2009 Author Share Posted September 22, 2009 Use for loop like this, replace fieldnam1, fieldname2 .... with your table fieldnames for($i=0;$i<count($res);$i++) { echo $res[$i]['fielname1']." ".$res[$i]['fielname1']; } Yes. Thats worked, i was missing the ['fielname1'] field name from $res[$i] Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/175088-mysql-query-in-an-array/#findComment-922843 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.