|
|
|
<?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");
$query = "SELECT * FROM mytable";
$myresult = QueryIntoArray($query);
for($i=0;$i<count($myresult);$i++){
print $myresult[$i]["MyField1"];
print $myresult[$i]["MyField2"];
}
?>
</BODY>
</HTML>
|
|
| create a grid out of <INPUT TYPE=TEXT> then saving to a database. Uses
a 'multi-dimension array', but not really as the array is just one big array
with the index of "[$i][$j]". Have a look at the code and you'll see what I
mean. Categories : PHP, MySQL, Arrays, Databases | | | Finds the median in an array of numbers - Can be used with a MySql database column read into an array Categories : PHP, Arrays, Databases, MySQL | | | Simple conversion functions to change MySQL dates to arrays, arrays to MySQL dates.
Categories : PHP, Arrays, Date Time, Databases, MySQL | | | dynamic table columns Categories : PHP, HTML and PHP, Arrays, Databases, MySQL | | | Sort the results from a SELECT query (any number of columns) into an array automatically. Categories : PHP, PHP Classes, Arrays, Databases, MySQL | | | bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager Categories : MySQL, PHP, MySQL, Complete Programs, Databases | | | This functions makes it easy to use session-variables known from ASP. With one Cookie the array "session" will save and restore from a db-record. In this version MySQL is used but it's should very easy to change Categories : PHP, Arrays, Cookies, MySQL, Databases | | | This simple function will take a few arguments and easily set a associative array for each column in a result from a MySQL query Categories : Databases, PHP, MySQL, Arrays | | | This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server. Categories : Databases, MySQL, Complete Programs, PHP, Databases | | | Function for retrieving MySQL enum values into a PHP array.
Categories : PHP, Databases, MySQL, Arrays | | | PHP Object Example of the Perl DBI with MySQL Categories : PHP, PHP Classes, MySQL, Databases, Perl | | | AJAX Data Grid System using php and mysql. A complete login system with the ability to display data in a grid using ajax. Add , update and delete the records without reloading the page. Categories : PHP, AJAX, Databases, MySQL, Java Script | | | Identify and log search engine access (spiders, robots, etc.) to a page. Categories : HTTP, Environment Variables, PHP, MySQL, Databases | | | Visits-tracking Categories : PHP, Databases, MySQL, Errors and Logging, Functions | | | A simple script to count and report hits and the last
modification time of an HTML page. Requires MySQL support
(other DBs should work too, except possibly mSQL). Categories : HTTP, MySQL, PHP, Databases | |
| | | | 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.
| |
|
|