WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Create an Array from SQL Result
Categories : PHP, MySQL, Arrays, Databases Update Picture
Anton Suryawan
Date : May 05th 2002
Grade : 5 of 5 (graded 2 times)
Viewed : 9143
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Anton Suryawan
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

<?
/********************************************
* nas_mysql_result -- connect to mysql then load query result
* into a two dimensional array.
* -----
* Author: Anton Suryawan
* -----
* Description:
* array nas_mysql_result
* (string $dbhost, $dbuserlogin, $dbpassword, $dbname, $dbsql, $rstype)
*/
function nas_mysql_result($dbhost,$dbuserlogin,$dbpassword,$dbname,$dbsql,$rstype) {
$dbconn = mysql_connect($dbhost,$dbuserlogin,$dbpassword) or die("Server
Unavailable");
mysql_select_db($dbname,$dbconn) or die("Database Unavailable");
$result = mysql_query($dbsql) or die("Query Unavailable");

//-- initial value --
$iRows = 0;
$iCols = 0;

$iRows = mysql_num_rows($result);
$iCols = mysql_num_fields($result);
settype($arrContents,"array");

switch ($rstype) {
case "byindex":
for($row=0; $row<$iRows; $row++){
$rs = mysql_fetch_row($result);
for($col=0; $col<$iCols; $col++){
$arrContents[$row][$col] = $rs[$col];
};
};
return $arrContents;
break;
case "byname":
for($row=0; $row<$iRows; $row++){
$rs = mysql_fetch_row($result);
for($col=0; $col<$iCols; $col++){
$arrContents[$row][mysql_field_name($result,$col)] = $rs[$col];
};
};
return $arrContents;
break;
};

mysql_free_result($result);
mysql_close($dbconn);
};
?>

<!--
Example...
Change the appropriate values with your own.
-->

<html>
<head><title>Array From SQL Result</title></head>
<body>
<table border=1 cellpadding=0 cellspacing=0>
<tr><th>#</th><th>Field 01</th><th>Field 02</th><th>Field 03</th></tr>
<?
$dbhost = "mysqlserver";
$dbuserlogin = "myname";
$dbpassword = "mypassword";
$dbname = "mydatabase";
$dbsql = "SELECT field1,field2,field3 FROM tables";

//-- by field index --
$myarray = nas_mysql_result
($dbhost,$dbuserlogin,$dbpassword,$dbname,$dbsql,"byindex");

for ($i=0; $i < count($myarray); $i++) {
echo "<tr><td align=right>$i.</td>";
for ($j=0; $j < 3; $j++) {
echo "<td nowrap>".$myarray[$i][$j]."</td>";
};
echo "</tr>\n";
};

//-- by field name --
$myarray = nas_mysql_result
($dbhost,$dbuserlogin,$dbpassword,$dbname,$dbsql,"byname");

for ($i=0; $i < count($myarray); $i++) {
echo "<tr><td align=left>$i.</td><td nowrap>".$myarray[$i]["field1"]."</td><td
nowrap>".$myarray[$i]["field2"]."</td><td nowrap>".$myarray[$i]
["field3"]."</td></tr>\n";
};
?>
</table>
</body>
</html>



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
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
dynamic table columns
Categories : PHP, HTML and PHP, Arrays, Databases, MySQL
Simple conversion functions to change MySQL dates to arrays, arrays to MySQL dates.
Categories : PHP, Arrays, Date Time, Databases, MySQL
How to load a query result into a PHP Array
Categories : PHP, Databases, Arrays, 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
myCSV-dump converts a simple CSV-flatfile-database into an MySQL-dump.
Categories : PHP, MySQL, Databases, Complete Programs
Authorize Me! An authentication script.
Categories : MySQL, Databases, Authentication, PHP
Multiple Search using PHP and Mysql
Categories : PHP, Databases, General SQL, MySQL
DDN FFA Network Script
Categories : PHP, MySQL, Complete Programs, HTML and PHP, Databases