Need code to fetch a row and put into variables
Moderator: General Moderators
Need code to fetch a row and put into variables
Can't find the code anywhere. I am trying to pull a row of data from MySql and put 5 of the fields into separate variables. Any help would be appreciated! Thanks in advance.
-
kettle_drum
- DevNet Resident
- Posts: 1150
- Joined: Sun Jul 20, 2003 9:25 pm
- Location: West Yorkshire, England
Code: Select all
$result = mysql_query("SELECT * FROM my_table WHERE blah = 'blah'");
$row = mysql_fetch_assoc($result);
echo $row['field1'];
echo $row['blah'];
echo $row['this'];
$test = $row['test'];lol do you have a specific question?
and for tips, never use mysql_assoc
use mysql_fetch_array, it does both number / string forms of data that it pulls form the DB table
the while() function is a loop. It can be used to display ALL the data in a table by assign it a parameter. ie:
that help?
and for tips, never use mysql_assoc
use mysql_fetch_array, it does both number / string forms of data that it pulls form the DB table
the while() function is a loop. It can be used to display ALL the data in a table by assign it a parameter. ie:
Code: Select all
<?php
$result = mysql_query("SELECT * FROM my_table WHERE blah = 'blah'");
// display all the usernames
while ($row = mysql_fetch_array($result)) {
$name = $row['username'];
echo $name;
}
// this while loop would go thru each row and grab the usernames/display them.
?>I ended up changing it to array. My bud used it so I changed it and it seemed to work the same. Thanks for letting me know the difference. Don't really have any specific questions. I believe I understand it now. After 5 hours and 4 coffees, by brain is a little overloaded though. Thanks everybody for your help!