bad_gui Posted April 27, 2008 Share Posted April 27, 2008 I'm modifying an open source project and trying to get a listing of the total number of entries in a mysql DB. $query = "SELECT COUNT(*) FROM library"; $result = @mysql_query ($query); $rows = @mysql_fetch_row ($result); $articles = $rows[0]; $where = "overall"; @mysql_close($link); print "<BR><BR>$articles articles $where<BR><BR>"; I don't understand why mysql_fetch_row is used since it gives the next available number as a scalar not an array. I tried simply setting $articles = @mysql_query ($query); but no value is printed. I get the correct result if I paste the command SELECT COUNT(*) FROM library into phpmyadmin's sql window. Postscript. When I removed the error suppression "@" the verbose errors revealed that no DB connection was established and what was missing was the following code to connect to the DB before executing the query $link = @mysql_connect($database_host, $database_user, $database_password); @mysql_select_db ($database_name); However I still don't understand why it doesn't just use the result from SELECT COUNT(*) FROM library?? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted April 27, 2008 Share Posted April 27, 2008 Try this: <?php $query = "SELECT COUNT(*) as num FROM library"; $result = @mysql_query ($query); $rows = @mysql_fetch_assoc($result); $articles = $rows['num']; $where = "overall"; @mysql_close($link); print "<BR><BR>$articles articles $where<BR><BR>"; ?> Quote Link to comment Share on other sites More sharing options...
bad_gui Posted April 28, 2008 Author Share Posted April 28, 2008 Thanks for the code but as a newbie I still don't understand why both versions use an associative array for a scalar result, namely the total number of records. Here is my limited understanding: $query = "SELECT COUNT(*) FROM library"; $result = @mysql_query ($query); The code above will have the number of records in the string $result $rows = @mysql_fetch_assoc($result); $articles = $rows['num']; Now the string $rows will be an array which contains the number of entries and $articles is set to the associative array element 'num' This code works but why have the extra complexity- or is there something I am missing? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted April 28, 2008 Share Posted April 28, 2008 $query = "SELECT COUNT(*) FROM library"; $result = @mysql_query ($query); The code above will have the number of records in the string $result No, $result wouldn't hold the number of records. This line $rows = @mysql_fetch_assoc($result); actually extracts the data from the result and puts it into an array. The only reason I had to do that is because I aliased the number of records in the query. You can use mysql_num_rows() instead if you prefer. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.