Forum Moderators: coopster

Message Too Old, No Replies

DB limit selecting

         

tr8er8

11:45 pm on Feb 7, 2008 (gmt 0)

10+ Year Member



Hi I have this script and I want it to show the results from my database as a seperate variable, right now its just showing $result, I want it to show them seperate, because Im going to change the styles for each one. Anyone know how to approach this? For example, Im pulling the title and date from the table, I want to display them seperatly with seperate varibles, or at least make it so I can customize the color and etc of each one.

// sending query
$result = mysql_query("SELECT date, title FROM {$table} ORDER BY id DESC
LIMIT 5");
if (!$result) {
die("Query to show fields from table failed");
}

$fields_num = mysql_num_fields($result);

// printing table rows
while($row = mysql_fetch_row($result))
{

// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "$cell<br>";

}

GamingLoft

2:46 am on Feb 8, 2008 (gmt 0)

10+ Year Member



This should be rather easy... first you make a simple query..

$query = mysql_query("SELECT * FROM {$table} ORDER BY id DESC
LIMIT 5");
$result = mysql_fetch_array($query);

then use something like this to get each seperate variable..

$title = $result[title];
$date = $result[date];

then do w/e you want with those variables...

also for your num_field just change result to query..

[edited by: GamingLoft at 2:49 am (utc) on Feb. 8, 2008]

tr8er8

2:54 am on Feb 8, 2008 (gmt 0)

10+ Year Member



Ok when I do it I just get:

$result[title]

when I echo it.

Then i tried doing

$title = $result['title'];
$date = $result['date'];

And now its just blank. Any help?

menace_sa

6:14 am on Feb 8, 2008 (gmt 0)

10+ Year Member



Change code :

// sending query
$result = mysql_query("SELECT date, title FROM {$table} ORDER BY id DESC
LIMIT 5");
if (!$result) {
die("Query to show fields from table failed");
}

$theresult = mysql_result($result);

// printing table rows
while($row = mysql_fetch_array($theresult))
{
$title = $row["title"];
$date = $row["date"];

echo $date . " " . $title; (Or whatever you want to do to them)

}