Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.php

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 04-01-2006, 01:01 PM
Dejan
Guest
 
Posts: n/a
Default mysql_num_rows HELP

Hi

Sorry for my terreble english.

On my local (win) comp i have apache+mysql+php 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_smjestaj = mysql_query("select * from v_smjestaja order by
v_smjestaja_naziv ASC", $db);
// some other html code goes here

if (mysql_num_rows($result_v_smjestaj)) {
print "<select name=\"v_smjestaja\">\n";
while ($qry = mysql_fetch_array($result_v_smjestaj)) {
print "<option
value=\"$qry[id_smjestaja]\">$qry[v_smjestaja_naziv]</option>";
}
print "</select>\n";
}
--------------------------

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



Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 04-02-2006, 12:37 PM
Dejan
Guest
 
Posts: n/a
Default 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@t31g2000cwb.googlegro ups.com...
> Write your code like this:
>
> $query = "....";
> $result = mysql_query($query);
> if($result) {
> $num_rows = mysql_num_rows($result); // notice the Result Resource
> here //
> } else {
> echo mysql_error();
> // die();
> }
>
>
> Best Regards,
>



Reply With Quote
  #3 (permalink)  
Old 04-03-2006, 12:46 PM
Jerry Stuckle
Guest
 
Posts: n/a
Default Re: mysql_num_rows HELP

Dejan wrote:
> Hi
>
> Sorry for my terreble english.
>
> On my local (win) comp i have apache+mysql+php 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_smjestaj = mysql_query("select * from v_smjestaja order by
> v_smjestaja_naziv ASC", $db);
> // some other html code goes here
>
> if (mysql_num_rows($result_v_smjestaj)) {
> print "<select name=\"v_smjestaja\">\n";
> while ($qry = mysql_fetch_array($result_v_smjestaj)) {
> print "<option
> value=\"$qry[id_smjestaja]\">$qry[v_smjestaja_naziv]</option>";
> }
> print "</select>\n";
> }
> --------------------------
>
> On my local comp this works. Plz any solution, in plain simple code))
> tnx in advance.
> Dejan
>
>
>


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@attglobal.net
==================
Reply With Quote
  #4 (permalink)  
Old 04-03-2006, 02:34 PM
milahu
Guest
 
Posts: n/a
Default 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.

Reply With Quote
  #5 (permalink)  
Old 04-03-2006, 04:56 PM
Jerry Stuckle
Guest
 
Posts: n/a
Default Re: mysql_num_rows HELP

milahu wrote:
> 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.
>


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@attglobal.net
==================
Reply With Quote
  #6 (permalink)  
Old 04-04-2006, 08:48 AM
webramz@gmail.com
Guest
 
Posts: n/a
Default Re: mysql_num_rows HELP

Write your code like this:

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


Best Regards,

Reply With Quote
  #7 (permalink)  
Old 04-04-2006, 01:57 PM
Anonymous
Guest
 
Posts: n/a
Default Re: mysql_num_rows HELP

Dejan wrote:
>
> Hi
>
> Sorry for my terreble english.
>
> On my local (win) comp i have apache+mysql+php 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:

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


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!
Reply With Quote
  #8 (permalink)  
Old 04-04-2006, 02:13 PM
Steve
Guest
 
Posts: n/a
Default 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($sql, $db) or die ("<p>$sql</p>" .
mysql_error());

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

Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 10:06 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.