/    Sign up×
Community /Pin to ProfileBookmark

array problem

I’m trying to populate an array with:
Column Name
Column Data Type
Next Column
Previous Column
From a mysql database.

I have:

[code=php]
<?php
mysql_connect(“host”,”username”,”pass”) ;
mysql_select_db(“db”) ;

$result = mysql_query(“SELECT COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = ‘test'”) ;

$result_set = array() ;

while ($array = mysql_fetch_assoc($result))
{
$result_set[] = array(‘COLUMN_NAME’=>$array[‘COLUMN_NAME’],’DATA_TYPE’=>$array[‘DATA_TYPE’], ‘PREV_COLUMN’=>$temp, ‘NEXT_COLUMN’=>’?????’) ;
$temp = $array[‘COLUMN_NAME’] ;
}
print_r($result_set) ;
?>
[/code]

the column name and data type were no problem, i got the previous column by storing the column name in a temp variable and adding it in the next loop.
problem is i can’t get the next column.

I don’t use arrays very much, and i’m sure there is a much better method than this. can anyone help me out.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 23.2006 — Do you really even need the previous_column/next_column elements? Your array will already have a sequential index which you can use to get the previous/next values.

For instance:
[code=php]
for($ix = 0; $ix < count($result_set); $ix++)
{
echo "<p>Current column: {$result_set[$ix]['COLUMN_NAME']}n";
if($ix > 0)
{
echo "<br>Previous column: {$result_set[$ix - 1]['COLUMN_NAME']}n";
}
if($ix < count($result_set) - 1)
{
echo "<br>Next column: {$result_set[$ix + 1]['COLUMN_NAME']}n";
}
echo "</p>n";
}
[/code]
Copy linkTweet thisAlerts:
@Vectorman211Jul 23.2006 — mysql_fetch_assoc() automatically builds an associative array for you? Isn't this process redundant?
Copy linkTweet thisAlerts:
@Sid3335authorJul 24.2006 — Your absolutely right NogDog, what was i thinking. works fine.
×

Success!

Help @Sid3335 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...