0

I have table named category, it contains 2 columns (id primary key , item)

with the name of item's i have another tables

i get the data in category table using foreach loop,

in that loop for every item there is a table. I trying to retrieve those data but i got a single table data only.

here is my code:

$con=mysql_connect('localhost','root','');
$db="testing";
$a=mysql_select_db($db,$con);
$c=mysql_query("select * from category");
$d=mysql_fetch_array($c);
mysql_data_seek($c,0);
foreach($d as $x)
{ 
    echo $x['item']." ";
    $e=mysql_query("select * from ".$x['item']);

        if($e)
        {
            $f=mysql_fetch_array(mysql_query("select * from ".$x['item']));
            mysql_data_seek($e,0);
            foreach($f as $y)
            { 
                echo $y['price']." ".$y['Quantity'];
            }
        }
        else continue;//break;
} 
  • `$d=mysql_fetch_array($c);` This will only give you one row. Use a `while` loop instead – asprin Jun 18 '14 at 04:28
  • what do you mean by "i got a single table data only" ? – ashok_p Jun 18 '14 at 04:30
  • I think your terminology is misleading here. You seem to be referring to ROWS as a table. It also seems you should be using a join for this. Most importantly you should definitely NOT be using mysql_* functions which are deprecated. – Mike Brant Jun 18 '14 at 04:42
  • http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Emilio Gort Jun 18 '14 at 04:44
  • 1
    if you post your table schema, I think we can help you better, generally speaking is better retrieve data from multiples table doing a join in the sql statement instead of what you are doing – Emilio Gort Jun 18 '14 at 04:47
  • it means first table data only remaining tables data not getting – ubuntuUser Jun 18 '14 at 05:08

1 Answers1

0

I have tried my best to understand what you want from your post. Also, mysql has been deprecated as of PHP 5.5.0. You should use mysqli instead. My answer is done with both mysqli and mysql.

Result:

Item - car
Price: 29000.00 Quantity: 5
Price: 18000.00 Quantity: 6
Item - house
Price: 50000.00 Quantity: 4

Source Code - Using mysqli:

$con=mysqli_connect('localhost','root','');
$db="testing";
$a=mysqli_select_db($con, $db);
$c=mysqli_query($con,"select * from category");
while($x = mysqli_fetch_array($c)){
    echo "Item - ".$x['item']."<br/>";
        $e=mysqli_query($con,"select * from ".$x['item']);
        if($e)
        {
            while($y = mysqli_fetch_array($e)){
                echo "Price: ".$y['price']." Quantity: ".$y['Quantity']."<br/>";
            }       
        }
        else continue;//break;  
}

Source Code - Using mysql:

$con=mysql_connect('localhost','root','');
$db="testing";
$a=mysql_select_db($db, $con);
$c=mysql_query("select * from category");
while($x = mysql_fetch_array($c)){

    echo "Item - ".$x['item']."<br/>";
        $e=mysql_query("select * from ".$x['item']);
        if($e)
        {
            while($y = mysql_fetch_array($e)){
                echo "Price: ".$y['price']." Quantity: ".$y['Quantity']."<br/>";
            }       
        }
        else continue;//break;  
}

SQL Dump:

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `testing`
--

-- --------------------------------------------------------

--
-- Table structure for table `car`
--

CREATE TABLE IF NOT EXISTS `car` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `price` decimal(10,2) NOT NULL,
  `Quantity` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

--
-- Dumping data for table `car`
--

INSERT INTO `car` (`id`, `price`, `Quantity`) VALUES
(5, 29000.00, 5),
(6, 18000.00, 6);

-- --------------------------------------------------------

--
-- Table structure for table `category`
--

CREATE TABLE IF NOT EXISTS `category` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `item` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `category`
--

INSERT INTO `category` (`id`, `item`) VALUES
(1, 'car'),
(2, 'house');

-- --------------------------------------------------------

--
-- Table structure for table `house`
--

CREATE TABLE IF NOT EXISTS `house` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `price` decimal(10,2) NOT NULL,
  `Quantity` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `house`
--

INSERT INTO `house` (`id`, `price`, `Quantity`) VALUES
(2, 50000.00, 4);

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
user2067005
  • 859
  • 7
  • 15