You are here: Re: Is this a good idea? « PHP Programming Language « IT news, forums, messages
Re: Is this a good idea?

Posted by sam.s.kong on 09/26/41 11:37

sam.s.kong@gmail.com wrote:
> Hi!
>
> I've been programming ASP for 5 years and am now learning PHP.
> In ASP, you can use GetRows function which returns 2 by 2 array of
> Recordset.
> Actually, it's a recommended way in ASP when you access DB as it
> disconnects the DB earlier.
> Also, it's handy as you can directly access any data in the array
> without looping.
>
> As far as I know, there's no such function in PHP and I can make one.
> My question is whether it's good in PHP.
>
> pseudo-code:
>
> $data = get_data("select * from table1");
> $var = $data[3][2]; //value at 4th row, 3rd column
>
> This way, I can wrap db connection, data retrieval, and error handling
> with one function (or maybe a class).
> Is the idea workable?
>

I found a code that suits my intention.

<?php

class mysql_array
{

public function __construct ( $s_host , $s_user , $s_pass ,
$s_db )
{
$this -> r_conn = mysql_connect ( $s_host , $s_user ,
$s_pass ) or die ( mysql_error ( ) ) ;
mysql_select_db ( $s_db ) ;
}

private function array_make ( $s_sql , $i_type )
{
$r_rs = mysql_query ( $s_sql , $this -> r_conn ) or die (
mysql_error ( ) ) ;
while ( $a_col = mysql_fetch_array ( $r_rs , $i_type ) )
{
$a_rs [ ] = $a_col ;
}
mysql_free_result ( $r_rs ) ;
return ( $a_rs ) ;
}

public function array_logic ( $s_sql )
{
$a_rs = $this -> array_make ( $s_sql , MYSQL_NUM ) ;
return ( $a_rs ) ;
}

public function array_assoc ( $s_sql )
{
$a_rs = $this -> array_make ( $s_sql , MYSQL_ASSOC ) ;
return ( $a_rs ) ;
}

public function array_both ( $s_sql )
{
$a_rs = $this -> array_make ( $s_sql , MYSQL_BOTH ) ;
return ( $a_rs ) ;
}

}

$o_mysql = new mysql_array ( 'localhost' , 'user' , 'pass' , 'db' )
;
$s_sql = "SHOW TABLES" ;
$a_rs = $o_mysql -> array_assoc ( $s_sql ) ;

echo '<pre>' ;
print_r ( $a_rs ) ;

?>

It's from http://us2.php.net/manual/en/ref.mysql.php .


Sam

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация