mysql_num_rows HELP

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

    #1

    mysql_num_rows HELP

    Hi

    Sorry for my terreble english.

    On my local (win) comp i have apache+mysql+ph p 4.05
    I'm counting rows using mysql_num_rows function, and everything works fine.
    When i upload php file on server it says:

    Warning: mysql_num_rows( ): supplied argument is not a valid MySQL result
    resource in /home/optikara/public_html/crosun/izn/insert_form.php on line 80
    Server runs php 5.0.5

    Code is:
    --------------------------
    //connect to database code goes here

    $result_v_smjes taj = mysql_query("se lect * from v_smjestaja order by
    v_smjestaja_naz iv ASC", $db);
    // some other html code goes here

    if (mysql_num_rows ($result_v_smje staj)) {
    print "<select name=\"v_smjest aja\">\n";
    while ($qry = mysql_fetch_arr ay($result_v_sm jestaj)) {
    print "<option
    value=\"$qry[id_smjestaja]\">$qry[v_smjestaja_naz iv]</option>";
    }
    print "</select>\n";
    }
    --------------------------

    On my local comp this works. Plz any solution, in plain simple code:)))
    tnx in advance.
    Dejan



  • Jerry Stuckle

    #2
    Re: mysql_num_rows HELP

    Dejan wrote:[color=blue]
    > Hi
    >
    > Sorry for my terreble english.
    >
    > On my local (win) comp i have apache+mysql+ph p 4.05
    > I'm counting rows using mysql_num_rows function, and everything works fine.
    > When i upload php file on server it says:
    >
    > Warning: mysql_num_rows( ): supplied argument is not a valid MySQL result
    > resource in /home/optikara/public_html/crosun/izn/insert_form.php on line 80
    > Server runs php 5.0.5
    >
    > Code is:
    > --------------------------
    > //connect to database code goes here
    >
    > $result_v_smjes taj = mysql_query("se lect * from v_smjestaja order by
    > v_smjestaja_naz iv ASC", $db);
    > // some other html code goes here
    >
    > if (mysql_num_rows ($result_v_smje staj)) {
    > print "<select name=\"v_smjest aja\">\n";
    > while ($qry = mysql_fetch_arr ay($result_v_sm jestaj)) {
    > print "<option
    > value=\"$qry[id_smjestaja]\">$qry[v_smjestaja_naz iv]</option>";
    > }
    > print "</select>\n";
    > }
    > --------------------------
    >
    > On my local comp this works. Plz any solution, in plain simple code:)))
    > tnx in advance.
    > Dejan
    >
    >
    >[/color]

    1. Always check the result from mysql_query (and all similar mysql_ calls).
    2. When it fails, check the error message with mysql_error().

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • milahu

      #3
      Re: mysql_num_rows HELP

      Some hints:
      - You don't need to pass your database connection to mysql_query() (2nd
      argument)
      - When sending queries to your database *always* check for errors using
      sth. like
      mysql_query('.. .') or die(mysql_error ());
      - Use shorter variable names to avoid typing errors
      - Use single quotes ('...') for HTML if you don't like escaping all the
      double quotes too much. ;)

      Comment

      • Jerry Stuckle

        #4
        Re: mysql_num_rows HELP

        milahu wrote:[color=blue]
        > Some hints:
        > - You don't need to pass your database connection to mysql_query() (2nd
        > argument)
        > - When sending queries to your database *always* check for errors using
        > sth. like
        > mysql_query('.. .') or die(mysql_error ());
        > - Use shorter variable names to avoid typing errors
        > - Use single quotes ('...') for HTML if you don't like escaping all the
        > double quotes too much. ;)
        >[/color]

        No, you don't *need* to pass the database connection. But it's a good idea to
        do so. And it never hurts.

        If you don't, PHP will use the last opened connection. That may be good now -
        but changes in the code at a later date may change that. Now you've added a bug
        which can be hard to fix (it fails in an area of code you didn't touch).

        Also, I prefer *longer* more descriptive variable names. I don't understand
        Dejan's native language, but these variables don't look exactly random to me.
        I'm sure they mean something to him.

        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        • webramz@gmail.com

          #5
          Re: mysql_num_rows HELP

          Write your code like this:

          $query = "....";
          $result = mysql_query($qu ery);
          if($result) {
          $num_rows = mysql_num_rows( $result); // notice the Result Resource
          here //
          } else {
          echo mysql_error();
          // die();
          }


          Best Regards,

          Comment

          • Dejan

            #6
            Re: mysql_num_rows HELP

            ppl, tnx very much on your anwsers.
            I have dealed with this problem and solved it.
            I have added this error checking, buth the real error was tiping mistake.
            I have named mysql table "gred", and neaded "grad". and then mysql sead
            "mysql_num_rows (): supplied argument is not a valid MySQL result
            resource "

            tnx again
            Dejan


            <webramz@gmail. com> wrote in message
            news:1144140487 .542634.17920@t 31g2000cwb.goog legroups.com...[color=blue]
            > Write your code like this:
            >
            > $query = "....";
            > $result = mysql_query($qu ery);
            > if($result) {
            > $num_rows = mysql_num_rows( $result); // notice the Result Resource
            > here //
            > } else {
            > echo mysql_error();
            > // die();
            > }
            >
            >
            > Best Regards,
            >[/color]


            Comment

            • Anonymous

              #7
              Re: mysql_num_rows HELP

              Dejan wrote:[color=blue]
              >
              > Hi
              >
              > Sorry for my terreble english.
              >
              > On my local (win) comp i have apache+mysql+ph p 4.05
              > I'm counting rows using mysql_num_rows function, and everything works fine.
              > When i upload php file on server it says:
              >
              > Warning: mysql_num_rows( ): supplied argument is not a valid MySQL result
              > resource in /home/optikara/public_html/crosun/izn/insert_form.php on line 80
              > Server runs php 5.0.5
              >
              > Code is:[/color]
              [...][color=blue]
              > On my local comp this works. Plz any solution, in plain simple code:)))
              > tnx in advance.
              > Dejan[/color]

              If it works at home then most likely the database connect code fails to
              connect on the server.

              Check the user and password parameters in your database connect code and
              make sure they are correct on the server.

              Bye!

              Comment

              • Steve

                #8
                Re: mysql_num_rows HELP

                First, you need to make your life a little easier by seperating out the
                sql query:

                $sql = "SELECT *
                FROM my_table";

                Assuming $db is a successful mysql_connect call...

                $result = mysql_query($sq l, $db) or die ("<p>$sql</p>" .
                mysql_error());

                If the query is bad, the script wont try to go any further

                Comment

                Working...