Jump to content

Mysql query in an Array


dreamwest

Recommended Posts

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);

 

 

Link to comment
https://forums.phpfreaks.com/topic/175088-mysql-query-in-an-array/
Share on other sites

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

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);

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);

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.