473,394 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Optimizing a function...

All,
This is a function that I use quite often. I pass in my SQL string and
return the results as an array. Does anyone see any optimizations that I can
make (eventually I would like to make a class, but I am still new).

function table_array ($sql_string)
{
$result = mysql_query($sql_string) or die("dead");
$num_rows = mysql_num_rows($result);
$num_fields = mysql_num_fields($result);
$j = 0;
$x = 1;
while ($row = mysql_fetch_array($result)) {
for($j = 0;$j < $num_fields;$j++) {
$name = mysql_field_name($result, $j);
$arr[$x][$name] = $row[$name];
}
$x++;
}
return array('arr' => $arr, 'rows' => $num_rows, 'fields' => $num_fields);
}

Always trying to shave off some access times =)
Thanks.
Jul 17 '05 #1
5 1507
Here's a basic class I made a while ago that does something similar..

Once on a page you'll need to do:
$db = new dbObject("localhost", "database", "myuser", "mypassword");

from then on, you can just use:
$db->query("SELECT * FROM wherever");

It'll return just the array of contents of your query.

Once you have the array, you can get the number of rows by:
$num_rows = count($returnedArray);

and you can get the number of fields by:
$num_fields = intval(count(@$returnedArray[0]));

The plan was back then that I could replace the class if I ever moved to
a different database technology.

<?php

class dbObject {
var $server = "localhost";
var $database = "";
var $username = "root";
var $password = "";

var $connection;

function dbObject ($server, $database, $username, $password) {
if ($this->connection = mysql_connect($server, $username,
$password)) {
mysql_select_db ($database, $this->connection);
}
}

function query($sql) {
$dbO = mysql_query($sql, $this->connection);
if (strtolower(substr($sql, 0, 6))=="select") {
$ret = Array();
if (!$dbO) {print "SQL: {$sql} <BR /> FAILED";}
while ($dbR = mysql_fetch_array($dbO)) {$ret[] = $dbR;}
}else{
$ret = $dbO;
}
return $ret;
}

function close() {
mysql_close($this->connection);
}
}

?>

StinkFinger wrote:
All,
This is a function that I use quite often. I pass in my SQL string and
return the results as an array. Does anyone see any optimizations that I can
make (eventually I would like to make a class, but I am still new).

function table_array ($sql_string)
{
$result = mysql_query($sql_string) or die("dead");
$num_rows = mysql_num_rows($result);
$num_fields = mysql_num_fields($result);
$j = 0;
$x = 1;
while ($row = mysql_fetch_array($result)) {
for($j = 0;$j < $num_fields;$j++) {
$name = mysql_field_name($result, $j);
$arr[$x][$name] = $row[$name];
}
$x++;
}
return array('arr' => $arr, 'rows' => $num_rows, 'fields' => $num_fields);
}

Always trying to shave off some access times =)
Thanks.

Jul 17 '05 #2
"neur0maniak" <us****@neur0maniak.co.uk> wrote in message
news:40*********************@ptn-nntp-reader03.plus.net...
Here's a basic class I made a while ago that does something similar..

Once on a page you'll need to do:
$db = new dbObject("localhost", "database", "myuser", "mypassword");

from then on, you can just use:
$db->query("SELECT * FROM wherever");

It'll return just the array of contents of your query.

Once you have the array, you can get the number of rows by:
$num_rows = count($returnedArray);

and you can get the number of fields by:
$num_fields = intval(count(@$returnedArray[0]));

The plan was back then that I could replace the class if I ever moved to a
different database technology.

<?php

class dbObject {
var $server = "localhost";
var $database = "";
var $username = "root";
var $password = "";

var $connection;

function dbObject ($server, $database, $username, $password) {
if ($this->connection = mysql_connect($server, $username,
$password)) {
mysql_select_db ($database, $this->connection);
}
}

function query($sql) {
$dbO = mysql_query($sql, $this->connection);
if (strtolower(substr($sql, 0, 6))=="select") {
$ret = Array();
if (!$dbO) {print "SQL: {$sql} <BR /> FAILED";}
while ($dbR = mysql_fetch_array($dbO)) {$ret[] = $dbR;}
}else{
$ret = $dbO;
}
return $ret;
}

function close() {
mysql_close($this->connection);
}
}

?>


Huge thanks. I'm going to try it out right now =)
Thanks again.
Jul 17 '05 #3
"StinkFinger" <st****@pinky.com> wrote in message
news:10*************@corp.supernews.com...
Does anyone see any optimizations that I can make

function table_array ($sql_string)
{
$result = mysql_query($sql_string) or die("dead");
$num_rows = mysql_num_rows($result);
$num_fields = mysql_num_fields($result);
$j = 0;
$x = 1;
while ($row = mysql_fetch_array($result)) {
for($j = 0;$j < $num_fields;$j++) {
$name = mysql_field_name($result, $j);
$arr[$x][$name] = $row[$name];
}
$x++;
}
return array('arr' => $arr, 'rows' => $num_rows, 'fields' => $num_fields); }


You could do the following:

function table_array ($sql_string){
$result = mysql_query($sql_string) or die("dead");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$arr[] = $row;
}

return array('arr' => $arr, 'rows' => mysql_num_rows($result), 'fields'
=> mysql_num_fields($result));
}

This should be identical, but with less variable declarations and
assignments.

Chris Finke
Jul 17 '05 #4
"Christopher Finke" <ch***@efinke.com> wrote in message
news:2l************@uni-berlin.de...
"StinkFinger" <st****@pinky.com> wrote in message
news:10*************@corp.supernews.com...
Does anyone see any optimizations that I can make

function table_array ($sql_string)
{
$result = mysql_query($sql_string) or die("dead");
$num_rows = mysql_num_rows($result);
$num_fields = mysql_num_fields($result);
$j = 0;
$x = 1;
while ($row = mysql_fetch_array($result)) {
for($j = 0;$j < $num_fields;$j++) {
$name = mysql_field_name($result, $j);
$arr[$x][$name] = $row[$name];
}
$x++;
}
return array('arr' => $arr, 'rows' => $num_rows, 'fields' =>

$num_fields);
}


You could do the following:

function table_array ($sql_string){
$result = mysql_query($sql_string) or die("dead");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$arr[] = $row;
}

return array('arr' => $arr, 'rows' => mysql_num_rows($result), 'fields'
=> mysql_num_fields($result));
}

This should be identical, but with less variable declarations and
assignments.


Thank you also - this is great. I can use this until I fully understand the
classes, etc.
Jul 17 '05 #5
If you want to convert a result set into an associative array then try this
for size:

// convert result set into a simple associative array for each row
while ($row = mysql_fetch_assoc($result)) {
$array[] = $row;
} // while

return $array;

As you can see this takes the associative array produced by
mysql_fetch_assoc() and appens it to $array. It does not have to waste time
in iterating over every field in each row.

I know this code works as I have been using it for several years. Take a
look at http://www.tonymarston.co.uk/php-mys...seobjects.html for
details. This gives a basic introduction into classes, and how to create a
class to handle all your database access.

HTH.

--
Tony Marston

http://www.tonymarston.net

"StinkFinger" <st****@pinky.com> wrote in message
news:10*************@corp.supernews.com...
All,
This is a function that I use quite often. I pass in my SQL string and
return the results as an array. Does anyone see any optimizations that I can make (eventually I would like to make a class, but I am still new).

function table_array ($sql_string)
{
$result = mysql_query($sql_string) or die("dead");
$num_rows = mysql_num_rows($result);
$num_fields = mysql_num_fields($result);
$j = 0;
$x = 1;
while ($row = mysql_fetch_array($result)) {
for($j = 0;$j < $num_fields;$j++) {
$name = mysql_field_name($result, $j);
$arr[$x][$name] = $row[$name];
}
$x++;
}
return array('arr' => $arr, 'rows' => $num_rows, 'fields' => $num_fields); }

Always trying to shave off some access times =)
Thanks.

Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Jacek Generowicz | last post by:
I have a multiple disptacher which, conceptually, looks something like this: class Multimethod: def __init__(self): self.methods = {}
3
by: Simon | last post by:
Is there utility/function that you can use to provide lookup values for the tags defined in a DTD? The problem lots of good tag names that are used to describe small data items is causing some...
14
by: Ian Richardson | last post by:
I'm writing a large Javascript application (uncompressed source around 400K) which is doing almost all the initialisation it needs to in a just-in-time manner. However, I have included an option...
4
by: J. Campbell | last post by:
From reading this forum, it is my understanding that C++ doesn't require the compiler to keep code that does not manifest itself in any way to the user. For example, in the following: { for(int...
6
by: Uros | last post by:
Hello! I have some trouble getting good results from my query. here is structure stat_views id | integer id_zone | integer created | timestamp
2
by: Brian | last post by:
In particular, this question goes out to the Microsoft C++ development team. Back in June, Ronald Laeremans posted the following message. Has the optimizing compiler been included with the...
4
by: Flashman | last post by:
A little confusing with setting up optimizing options with 2003 .NET. Under the Optimization Tab. if you set to /O1 or /O2 is the program ignoring the settings for Inline Function expansion,...
2
by: Jack | last post by:
I have a chunk of code that loads a few dozen function pointers into global variables. I'm concerned with unused memory consumption. What if the client only needs to use one or two functions? Then...
0
by: Miguel Perez | last post by:
Please critique this tail call optimizing decorator I've written. I've tried to fix the pitfalls of other proposed decorators, and the result is this one that supports mutual recursion, does not...
2
by: Andrea Taverna | last post by:
Hello everyone, I wrote a bunch of recursive functions to operate on multi-dimensional matrices. The matrices are allocated dynamically in a non-contiguous way, i.e. as an array of pointers...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.