Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newbie php/mysql code

Status
Not open for further replies.

leegold

Technical User
Joined
Mar 19, 2002
Messages
39
Location
US
Could you explain the code below - it's newbie stuff,
but having trouble understanding? The lines are in logical order. If you could tell me :
1. what the line's doing and
2. what value is in the variable on the left as a result. Thanks.
Lee G.

$query_count = "SELECT count(*) FROM table";

$result_count = mysql_query($query_count);

$totalrows = mysql_num_rows($result_count);
 
Apples and pears.
1. $query_count = "SELECT count(*) FROM table";
This is assigning a SQL query to a string variable. The query will return 1 row that holds the count of record.

2. $result_count = mysql_query($query_count);
Executing the query and getting a result resource id.

3. $totalrows = mysql_num_rows($result_count);
Assigning "1" to the variable $totalrows as the count query returns one row that holds the total count.

To get that number of rows I'd do this:
Code:
$query_count = "SELECT count(*) AS num FROM table";
$result   = mysql_query($query_count) OR die("Query failed :".mysql_error());
$row = mysql_fetch_assoc($result);
echo "There are ".$row['num'].' records in the table.';
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top