PHP Function

Hi Guys,

Im trying to create a really simple function but I can’t figure out exactly how to do it. I think it is just a php array?

I want to do a select from my database and assign each coloum to a different variable name. Kind of like this.

[php]<?php

function fname()

mysql query…

$column1=querycolumn1

$column2=querycolumn2

?>[/php]

I then want to be able to use these variables later on in the page.

I hope that makes sense

Regards,

Sam

Did you mean the fuction mysql_fetch_array($query) ?
this function results array of columns and rows
you can see it’s manual here
http://php.net/manual/en/function.mysql-fetch-array.php
I hope this is what you want

Thank you for your reply… I think this is what I want but i don’t fully understand it.

I will clarify with an example.

I want to use a select statement to load user preferences

[php]“SELECT * FROM preferenses WHERE user=’”.$_SESSION[‘user’]."’"[/php]

Lets say my column headers are user, box1, box2, box 3 and my values for this user are blue, green, red

I want the query to get the results where the user = the session user and return then as variables

$box1 = “blue”
$box2 = “green”
$box3 = “red”

I then want to be able to use these variables later on in my php script with in the same page.

I hope this clarifies… would be extremely grateful if you are able to give me some guidance on this.

Regards,

Sam

at your state
you can use
echo $data[‘box1’];
will print “blue”
$data is the variable which you stored the fetch_array in it
try this code
[php]$query=“SELECT * FROM preferenses WHERE user=’”.$_SESSION[‘user’]."’";
while $data=mysql_fetch_array("$query"){
echo $data[‘box1’]."
";
echo $data[‘box2’]."
";
echo $data[‘box3’]."
";
}[/php]

What happens if i don’t want to echo them all together but rather user the variables to call them at different point on the page?

Many thanks

Sam

write all you want between the two { and }
so, you can use the variables in true way.

I want to use it to create dynamic css…

for example

#box1{
width:200px;
height:200px;
background-color:$box1;
float:left;
}

#box2{
width:200px;
height:200px;
background-color:$box2;
float:left;
}

#box3{
width:200px;
height:200px;
background-color:$box3;
float:left;
}

how would this work using this example?

Thank you for your time

Sam

Sponsor our Newsletter | Privacy Policy | Terms of Service