Jump to content

mysql_fetch_array(): Error - Cannot figure it out.


Nuv

Recommended Posts

I am doing pagination. I am using php  and mysql.However i am getting an error.Can someone please point me my mistake

 

My Code

            <?php
             $mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "flourists";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)
or die("Opps some thing went wrong");
mysql_select_db($mysql_database, $bd) 
or die("Opps some thing went wrong");
            $per_page = 10; 
            if($_GET)
            {
            $page=$_GET['page'];
            }
            $start = ($page-1)*$per_page;
            $sql = "select * from state order by id limit $start,$per_page ";
            $dat = mysql_query($sql);
            while($row = mysql_fetch_array($dat))
            {
            $state=$row['state'];
?>
            <p><span> • </span> <a href="#"><strong><?php echo $state; ?></strong></a>
            
            <?php
}
?>

 

Error i am getting

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Users\Nuv\AppData\Roaming\NuSphere\PhpED\projects\test.php on line 18

 

Line 18 is "while($row = mysql_fetch_array($dat))"

 

 

Mysql version

 

MySQL

Server: localhost via TCP/IP
Server version: 5.5.8
Protocol version: 10
User: root@localhost
MySQL charset: UTF-8 Unicode (utf8)
Web server

Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8o PHP/5.3.4 mod_perl/2.0.4 Perl/v5.10.1
MySQL client version: mysqlnd 5.0.7-dev - 091210 - $Revision: 304625 $
PHP extension: mysql
phpMyAdmin

 

Raw sql -

select * from state order by id limit $start,$per_page

 

Create table

 

CREATE TABLE IF NOT EXISTS `state` (
  `id` int(2) NOT NULL AUTO_INCREMENT,
  `state` varchar(30) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=52 ;

 

 

 

 

 


How about posting it this time itself  ;)

 

Well i didn't check is $_GET


was set or not and it was taking the value of $page = 0.That was making $ start = -10.

But in sql query 'id' cannot have limits -10 to $per_page.Thus the error. So i changed my code to the following and it worked. :)

 

Changed code -

 

 

<?php
  $per_page = 10; 
            $page = 1;   // Setting it initially to 1 
            if(isset($_GET['page'])) //Using isset to check the value of $page 
            {
            $page=$_GET['page'];// If its set then changing the value of $page
            }
            $start = ($page-1)*$per_page;
            $sql = "select * from state order by id limit $start,$per_page";
            $dat = mysql_query($sql);
            while($row = mysql_fetch_array($dat))
            {  
            $state=$row['state'];
?>
            <p><span> • </span> <a href="#"><strong><?php echo $state; ?></strong></a>
            
            <?php
}
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.