Jump to content

Error: mysql_num_rows


phulla

Recommended Posts

My local machine i have
php v4.3.10
mysql v4.1.10-nt
apache v2.0.50

My uk2.net host has
php v4.3.11
mysql v4.0.24
apache v1.3.33

The following code works fine locally
[code]
<?
    if ($conn) {
        $result = $dbConn->queryDB( "SELECT * FROM ajmerphull_mainnav WHERE display = 1 ORDER BY id ASC" );
    }
?>
<div id="nav" class="fudge">
  <ul>
  <?
    if (mysql_num_rows($result) >= 1) {
        $count = 1;
        while ($row = mysql_fetch_array($result)) {
            $last = ($count == mysql_num_rows($result) ? ' class="noborder"' : '');
            print (
            "<li". $last ."><b>[</b><a href=\"". $row['link'] ."\" accesskey=\"". $count ."\" title=\"". $row['title'] ."\">". $row['name'] ."</a><b>]</b></li>
            ");
            $count++;
        }
    }
  ?>
  </ul>
</div>
[/code]

Database class
[code]
    function queryDB($sql) {
        return mysql_query($sql) or die(mysql_error());

    }

[/code]

I can connect to the database i have replicated on the host using phpMyAdmin and execute the SQL statement and it returns the desired result.

On the host, outputing the value of $result returns a value of 1, which is correct.

I can not understand what's wrong with mysql_num_rows. If i remove the if statement, i get the same error for mysql_fetch_array.

I can not print_r(mysql_fetch_array($result)) to check the value.

Can anybody help?

You can see this live at [a href=\"http://ajmerphull.com\" target=\"_blank\"]http://ajmerphull.com[/a]

Thanks
Link to comment
Share on other sites

Hrm..

Well, at first glance, it looks like you check to see if $conn is valid, but then you're calling it $dbConn when you do your query.. Are these 2 independent objects?

Are you sure that $conn is valid each time? If $conn fails, then $result is never populated with a valid resource. So when you try to get the number of rows, it will fail.

Try tossing some debug info in there. Inside the first if loop, do something like this :

[code]
    if ($conn) {
        print "Connection is Valid";
        $result = $dbConn->queryDB( "SELECT * FROM ajmerphull_mainnav WHERE display = 1 ORDER BY id ASC" );
    } else {
        print "Invalid Connection!";
    }
[/code]

Your second if statement could look something like this :

[code]
        if (isset($result) && (mysql_num_rows($result) >= 1)) {
[/code]

I would recommend re-working this a bit, though. If you can't get a connection to the database, don't bother continuing. Display an error message instead of displaying partial data...
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.