<?php
/*==================================
Author : Lord Nightslave
in_aeternum@hotmail.com
Load a query result into two dimensional array.
You can later refer the result in an array form with
the original field name.
i.e print $myresultarray[$i]["My Table Field Name"]
====================================*/
?>
<?php
/* You can put this in other file and just include it */
function OpenDb($hostname,$uid,$pwd,$dbname){
$link = @mysql_pconnect($hostname,$uid,$pwd);
if($link && mysql_select_db($dbname)){
return($link);
}
else{
return(FALSE);
}
}
?>
<?php
function QueryIntoArray($query){
settype($retval,"array");
$result= mysql_query($query);
if(!$result){
print "Query Failed";
}
for($i=0;$i<mysql_numrows($result);$i++){
for($j=0;$j<mysql_num_fields($result);$j++){
$retval[$i][mysql_field_name($result,$j)] = mysql_result
($result,$i,mysql_field_name($result,$j));
}//end inner loop
}//end outer loop
return $retval;
}//end function
?>
<!--An Example How To Use The functions
To try it simple change the appropriate variable to your own database & tables
-->
<HTML>
<HEAD>
<TITLE>PHP Array Test</TITLE>
</HEAD>
<BODY BGCOLOR=WHITE>
<?php
OpenDb("myhost","myuid","mypwd","mydatabase") or die("Failed Opening Database");
settype($myresult,"array");
Lisa St.Laurent wrote :621
I just wanted to say that this code works perfect. It was very easy to integrate with my existing code and the results were great. Thank-you:)
Alexei Selivanov wrote :624
It works great! Thanks!
I am using this to compare the resultsets from 2 queries. At first I wanted to create 2 tables & query them(classical approach for sub-query). But then, with the help of this function and "array_intersect" function (good for php4 only) I `ve got the same with less overhead.
Thanks again!
John Vincent Mombay wrote :1136
This codes great, it really did help out in some real world situation. i used it to retrieve my records from one of my classes, now i don`t have to expose my class on the presentation layer. good job! hope you have more to share.