Problem with php/mysql count() function showing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • auron

    Problem with php/mysql count() function showing

    Hi there,
    I have a really stupid and banal problem with showing the results of a
    MySQL query in PHP, preciselly with MySQL count() function that gives
    to a variable in PHP the result.

    NOTE:
    The problem here is PHP not MySQL, in MySQL everything works just
    fine.

    Here is the query that I wrote for getting the number of how much
    records I have in my litte and tottaly trivial DB:

    ------------------------------ CODE ----------------------
    $res = @mysql_query("S ELECT COUNT(*) FROM my_data_table") ;

    echo ($res);
    ------------------------------ CODE ----------------------
    PS: my_data_table is a name of table not a function or other MySQL
    related syntax.

    The result of this query on the screen is:
    Resource id #4

    I was around the net and I get some info that don't give me help.
    The solution from the net was to insert the value of the variable (in
    this case $res) in a function like mysql_fetch_obj ect or
    mysql_fetch_row etc.

    After some hours of trying to do something wtih those functions I
    started to think that getting how much records I have in my DB is
    almost a thing that only persons with a extremlly huge brain can
    concive and only a few superior entities can made.
    :)

    Can some body with a huge brain help me?

    Thank you in advance.
    Auron

    PS: I'm not a PRO I get only started to lear about PHP and MySQL.
    because somebody have told me that "was" easy to make some thing with
    those 2 tool.
    http://eye.cc php newsgroups
  • Michael Austin

    #2
    Re: Problem with php/mysql count() function showing

    auron wrote:[color=blue]
    > Hi there,
    > I have a really stupid and banal problem with showing the results of a
    > MySQL query in PHP, preciselly with MySQL count() function that gives
    > to a variable in PHP the result.
    >
    > NOTE:
    > The problem here is PHP not MySQL, in MySQL everything works just
    > fine.
    >
    > Here is the query that I wrote for getting the number of how much
    > records I have in my litte and tottaly trivial DB:
    >
    > ------------------------------ CODE ----------------------
    > $res = @mysql_query("S ELECT COUNT(*) FROM my_data_table") ;
    >
    > echo ($res);
    > ------------------------------ CODE ----------------------
    > PS: my_data_table is a name of table not a function or other MySQL
    > related syntax.
    >
    > The result of this query on the screen is:
    > Resource id #4
    >[/color]

    you are echoing the result of the connection id not the result of the query -
    this is a poorly written example, but you can use it to see what went wrong (or
    you can just copy/paste)


    $sql = "select count(*) from my_data_table";
    $link=mysql_con nect($HOST,$USR ,$PWD,$DB);
    if( $link ) {
    mysql_select_db ( $DB, $link );
    if( $sql ) {
    // make sure there are no trailing semi-colons (ie no sql-injection)
    $sql = ereg_replace( ";$", "", $sql );
    $sql = ereg_replace( "\\\\", "", $sql );
    $result = mysql_query( $sql, $link );
    if( $result ) {
    echo( "<p>Results : for '$sql;'.</p>\n" );
    if( $num = mysql_num_rows( $result ) ) {
    echo( "<pre>\n" );
    while( $array = mysql_fetch_row ( $result ) ) {
    while( list($key, $val) = each( $array ) ) {
    echo "$val | ";
    }
    echo "<br />\n";
    }
    echo( "</pre>" );
    } elseif( $af = mysql_affected_ rows( $link ) ) {
    echo( "<p>$af rows affected.</p>\n" );
    } else {
    echo( "<p>Command completed.</p>\n" );
    } } else {
    echo( "<p>Bad query: '$sql;'.</p>" );
    echo( mysql_errno() . ": " .
    mysql_error() . "<br />" );
    }
    } else {
    echo( "<p>Connect ed to '$DB'.</p>" );
    } # end if
    } else {
    echo( "<p>Bad connection.</p>" );
    echo( mysql_errno() . ": " .
    mysql_error() . "<br />" );
    }
    ?>


    [color=blue]
    > I was around the net and I get some info that don't give me help.
    > The solution from the net was to insert the value of the variable (in
    > this case $res) in a function like mysql_fetch_obj ect or
    > mysql_fetch_row etc.
    >
    > After some hours of trying to do something wtih those functions I
    > started to think that getting how much records I have in my DB is
    > almost a thing that only persons with a extremlly huge brain can
    > concive and only a few superior entities can made.
    > :)
    >
    > Can some body with a huge brain help me?
    >
    > Thank you in advance.
    > Auron
    >
    > PS: I'm not a PRO I get only started to lear about PHP and MySQL.
    > because somebody have told me that "was" easy to make some thing with
    > those 2 tool.
    > http://eye.cc php newsgroups[/color]


    --
    Michael Austin.
    DBA Consultant
    Donations welcomed. Http://www.firstdbasource.com/donations.html
    :)

    Comment

    • Alex

      #3
      Re: Problem with php/mysql count() function showing

      You can also use PEAR DB to do this. IMO much easier than using
      mysql_XXX function calls. I have some examples on my blog:
      Blogger ist ein Veröffentlichungs-Tool von Google, mit dem du ganz einfach deine Gedanken der Welt mitteilen kannst. Mit Blogger kannst du problemlos Texte, Fotos und Videos in deinem persönlichen Blog oder deinem Team-Blog veröffentlichen.


      Alex

      Comment

      • cvanschalkwijk@gmail.com

        #4
        Re: Problem with php/mysql count() function showing

        Hi Auron,

        $result_count=m ysql_query("SEL ECT COUNT(*) FROM my_data_table") ;
        $count=mysql_re sult($result_co unt, 0)
        echo $count;

        After you run your COUNT query you want to fetch the first row of your
        result.

        - Clay

        Comment

        Working...