Question about dynamic data/mysql query's into variables

Now, im showing all the code of what im trying to work with, but all i really have a question on is when the “while” statement ends up giveing me 3 records from my database…is there a way i can turn each of the outputed records(which is just the “name” column that i need) into single variable’s? so that i can use them elsewhere on that page? Please let me know if im not giveing enough information or not explaining things well enough.

<?php 
$query = "SELECT * FROM bships WHERE owner ='$player' AND Y = '$playerY' AND X ='$playerX' ";
$result = mysql_query($query) or die(mysql_error());
echo "<table border='0'>"

while($row = mysql_fetch_array( $result )) {
	echo "<tr align=center><td>"; 
	echo $row['name'];
	echo "</td></td>";
} 
echo "</table>";
?>

You can do something like the following, not sure if it will working your case, but it may be of some help…

[php]<?
while($row = mysql_fetch_array( $result )) {
echo “

”;
$$row[‘name’] = $row[‘name’];
echo “”;
}
?>[/php]

I have not tested this, but it should work. How it works:

Say the name you pull is John. It will now create a variable like $John that equals John.

Hope that helps.

I’d suggest something like this:

[php]
$somearray = array();
$i = 0;
while ($row = mysql_fetch_array($result)) {
echo “

”;
$somearray[$i++] = $row[‘name’];
echo “”;
}
[/php]

After the while() loop, you’ll have an indexed array in variable $somearray containing all names from the database query.

@Zyppora:
That is how I orginally was going to suggest, but the only problem with that is it doesn’t it make it very expandable does it? I mean, if you are just going to take all the names out of a table and dump them into an array and later referance them, you would need to know at which index they are.

I guess then you can just do the in_array function when it comes time to pull a specific name anyway.

Either way, I guess whatever gets the job done.

:)

What I’d do is pull out both the name and the ID of the record and store the name in the array at index ID.

Then again, I haven’t had many occasions where I needed to use queried data outside the ‘loop’ :confused:

Sponsor our Newsletter | Privacy Policy | Terms of Service