473,761 Members | 9,284 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

mySQL result to real array in function

Hi there!

I'd like to create a function which input is the result of a mySQL
query.
The output should be exactly the same, only not a mySQL result array,
but a 'real' array.
So it should also get the fieldnames returned by mySQL and use those as
keys.

I can't get things to work properly: it should return a
multidimensiona l array,
like

$result_array[1] = array(
[field1] => field1 value,
[field2] => field2 value,
etc.
)

somehow my result is (with code below)

$result_array[1] = array(
[0] => field1 value,
[field1] => field1 value,
[1] => field2 value,
[field2] => field2 value,
etc.
)

Hope someone can help me on this one.

Frizzle.
+++++ code ++++++

$get_res = mysql_query(QUE RY);

if( $res = mysql_fetch_arr ay( $get_res ) )
{

do{

$result[] = $res;

}while( $res = mysql_fetch_arr ay( $get_res ) );

};

foreach( $result as $key => $value ){

print_r($value) ;

};

Apr 25 '06 #1
16 2381
What i even forgot to mention:

I'd like to have the function called like

CreateArray( $get_result, 'res' );

where 'res' would be the name of the returned array.

Frizzle.

Apr 25 '06 #2
the trick is in ur mysql_fetch_arr ay() function...repl ace with
mysql_fetch_arr ay( $get_res, 1 )

Should work.

Peace,
Gerard.

Apr 25 '06 #3
$result[] = causes the result to be pushed onto an indexed array not an
associative array. See inline for fix.

-david-

$get_res = mysql_query(QUE RY);

if( $res = mysql_fetch_arr ay( $get_res ) )
{

do{
$result[$res['field1']] = $res['field1 value'];
} while( $res = mysql_fetch_arr ay( $get_res ) );

};

foreach( $result as $key => $value ){

print_r($value) ;

};


Apr 25 '06 #4

David Haynes wrote:
$result[] = causes the result to be pushed onto an indexed array not an
associative array. See inline for fix.

-david-

$get_res = mysql_query(QUE RY);

if( $res = mysql_fetch_arr ay( $get_res ) )
{

do{

$result[$res['field1']] = $res['field1 value'];

} while( $res = mysql_fetch_arr ay( $get_res ) );

};

foreach( $result as $key => $value ){

print_r($value) ;

};


David,

i don't alwas know all fieldnames beforehand.
So SELECT * should also put all fieldnames as
keys in the array.

Frizzle.

Apr 25 '06 #5
frizzle wrote:
David Haynes wrote:
$result[] = causes the result to be pushed onto an indexed array not an
associative array. See inline for fix.

-david-
$get_res = mysql_query(QUE RY);

if( $res = mysql_fetch_arr ay( $get_res ) )
{

do{

$result[$res['field1']] = $res['field1 value'];
} while( $res = mysql_fetch_arr ay( $get_res ) );

};

foreach( $result as $key => $value ){

print_r($value) ;

};


David,

i don't alwas know all fieldnames beforehand.
So SELECT * should also put all fieldnames as
keys in the array.

Frizzle.

My assumption was that one column in the table being queried formed the
keys for the associative array. If so, then my method works. If not,
what were you planning on using as the key to the array?

-david-

Apr 25 '06 #6

David Haynes wrote:
frizzle wrote:
David Haynes wrote:
$result[] = causes the result to be pushed onto an indexed array not an
associative array. See inline for fix.

-david-
$get_res = mysql_query(QUE RY);

if( $res = mysql_fetch_arr ay( $get_res ) )
{

do{

$result[$res['field1']] = $res['field1 value'];
} while( $res = mysql_fetch_arr ay( $get_res ) );

};

foreach( $result as $key => $value ){

print_r($value) ;

};


David,

i don't alwas know all fieldnames beforehand.
So SELECT * should also put all fieldnames as
keys in the array.

Frizzle.

My assumption was that one column in the table being queried formed the
keys for the associative array. If so, then my method works. If not,
what were you planning on using as the key to the array?

-david-


I'm sorry, i don't understand what you mean ...
(my lack of english comes to play ... )

Apr 25 '06 #7
Frizzle,

Did the mysql_fetch_arr ay( $get_res, 1 ) change fix the problem?

Apr 25 '06 #8

David Haynes wrote:
frizzle wrote:
David Haynes wrote:
$result[] = causes the result to be pushed onto an indexed array not an
associative array. See inline for fix.

-david-
$get_res = mysql_query(QUE RY);

if( $res = mysql_fetch_arr ay( $get_res ) )
{

do{

$result[$res['field1']] = $res['field1 value'];
} while( $res = mysql_fetch_arr ay( $get_res ) );

};

foreach( $result as $key => $value ){

print_r($value) ;

};


David,

i don't alwas know all fieldnames beforehand.
So SELECT * should also put all fieldnames as
keys in the array.

Frizzle.

My assumption was that one column in the table being queried formed the
keys for the associative array. If so, then my method works. If not,
what were you planning on using as the key to the array?

-david-


Having read it again and again i get it.
ideally, it would check if there is an 'id' in the returned fields and
use that
as index ...

Frizzle.

Apr 25 '06 #9

Gerard Matthew wrote:
Frizzle,

Did the mysql_fetch_arr ay( $get_res, 1 ) change fix the problem?


Yes thanks gerard, it removed the 'duplicate' indexes on the keys,
only i don't understand why ...

Frizzle.

Apr 25 '06 #10

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

Similar topics

1
1619
by: jerrygarciuh | last post by:
Hello, I am using this very slightly modified function found here: http://us2.php.net/mysql_fetch_object to make mySQL rows into objects of type $classname: // This takes db result rows and makes real objects of $classname type function &buildObj($result, $classname) { while($row = mysql_fetch_assoc($result)) { if ($row === null) return null;
0
5771
by: Phil Powell | last post by:
The table already has a fulltext index and from there I can use the MySQL fulltext search query to get results as well as the relevancy score. The problem I have is that MySQL has a default setting whereby the minimum amount of characters is 4 for a search. Being that we're government and full of TLA (three-letter acronyms), that is not practical, and furthermore, the app I'm building must be fully portable, so having MySQL tweaked...
9
2460
by: Börni | last post by:
Hi, I have an sql query like this: SELECT column FROM table WHERE column1="3" AND column2="1" This query works perfectly if i run it in the command line, to be exactly it return two results. But if i run it from php i just get the first of the two results. Any ideas? Mysql 4.1.8 php 5.0.3
3
13957
by: auron | last post by:
Hi there, I have a really stupid and banal problem with showing the results of a MySQL query in PHP, preciselly with MySQL count() function that gives to a variable in PHP the result. NOTE: The problem here is PHP not MySQL, in MySQL everything works just fine. Here is the query that I wrote for getting the number of how much
6
1817
by: Duderino82 | last post by:
I was wondering if there is a way to collect the names of the fields from a specific table. I think the soluction is to be researched in the sql code but maybe someone knows of a way to o so directly from php. Example. Table: Categories Field1: type1 Field2: type2 Field3: type3 Field4: type4
5
2275
by: strawberry | last post by:
In the function below, I'd like to extend the scope of the $table variable such that, once assigned it would become available to other parts of the function. I thought 'global $table;' would solve this but it's clear that I'm misunderstanding $variable persistence. I posted a similar enquiry over at alt.php.mysql, but I guess this is a more appropriate forum because the problems I'm having relate to PHP. Any help appreciated. ...
110
10623
by: alf | last post by:
Hi, is it possible that due to OS crash or mysql itself crash or some e.g. SCSI failure to lose all the data stored in the table (let's say million of 1KB rows). In other words what is the worst case scenario for MyISAM backend? Also is it possible to not to lose data but get them corrupted?
21
3702
by: bruno_guedesav | last post by:
I've made a function to fetch all results as an array of result- arrays. Getting the result arrays is easy, via mysql_fetch_array, and function itself is quite simple, as follows: function db_query($db, $query) { $result = mysql_query($query, $db); $res_array = array(); if ($result) //it is a search
2
9357
by: Iain Adams | last post by:
I have a db class that sets up a connection. It then has methods to query the db and fetch results etc that encapsulate the normal mysql functions (i.e. mysql_query($sql)). I seem to get this error only when I try and create a new copy of this class when one already is open. For instance I query a db to get all the event ids from the table. I then go through an load each event from the id. Because the query is being issued whilst a...
0
9554
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9376
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9988
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9811
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.