Jump to content

SOLVED: counting rows in someone else's code


bad_gui

Recommended Posts

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??

Link to comment
Share on other sites

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>";

?>

Link to comment
Share on other sites

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?

 

 

 

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.