/    Sign up×
Community /Pin to ProfileBookmark

MySQL query to array function

Just hacked together the following function for querying a MySQL database and returning the results as a multi-dimensional array. Enjoy…

[code=php]<?php
#
# query_db(query_string [, connection])
#
# Sends query to MySQL DB and returns results in array with the structure:
# array[row_num][‘col1’]=value
# array[row_num][‘col2’]=value
# etc….
#
# Created 2004/08 by Charles Reace
#
# NOTE: must connect to desired DB before calling
# returns 0 on any failure
#
function query_db($query, $connx=””)
{
switch($connx)
{
case “”:
$result = mysql_query($query);
if(!$result) { return(0); }
break;
case 2:
$result = mysql_query($query, $connx);
if(!$result) { return(0); }
break;
default:
return(0);
}
$ix = 0;
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
foreach($row as $key => $value)
{
$arr[$ix][$key] = $value;
}
$ix++;
}
return($arr);
}[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@sciguyryanAug 20.2004 — WOA! Me like that - just what I needed - Thanks for posting it ?



RyanJ
×

Success!

Help @NogDog spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...