PDA

View Full Version : Grabbing Data from a table


Archangel
02-09-2005, 09:24 PM
So yeah, I'm just starting to work with PHP more and I'm trying to interact with a Database. I'm not going to saying it's tougher than ASP (although it seems to be) b/c I know that it's just that I don't know what to do.

I have my code posted below...I'm connect to the DB and (I believe) pulling the data off of it...but how do I pull the data out of the $result? I have a DB with 3 fields...stat_type_id, stat_type_name, and stat_count. I simply want to get the information for the stat_count field where stat_type_id = 1. I just can't figure out how to get it.

Thanks for the help! Here's my code...


<?php
//Connect to Database Server
$link = mysql_connect( "localhost", "User", "Password" );
if (! $link)
die( "Couldn't connect to MySql" );

//Selecting Database
$database = "database_website";
mysql_select_db( $database ) or die ( "Couldn't open $database ".mysql_error() );

$result = mysql_query( "SELECT stat_count FROM t_Stats WHERE stat_type_id=1" );
?>

Zee
02-10-2005, 03:18 AM
$result = mysql_query("SELECT stat_count FROM t_Stats WHERE stat_type_id=1;"); //Don't forget that mySQL requires the semi-colon at the end of an SQL statement.
if (!$result)
die("Error in query: " . mysql_error());

while ($q_row = mysql_fetch_assoc($result)) {
echo $q_row['stat_count'];
echo "<br>\n";
}

Archangel
02-10-2005, 08:40 AM
Thanks for the help Zee!

Zee
02-10-2005, 08:10 PM
No problem ;)