从json mysql输出id

时间:2015-06-23 22:08:18

标签: php mysql json

我已经遵循了一个教程,其中从数据库表中查询ID的代码是这样输出的,但我不断收到错误,我的输出没有找到。我的表有3行:id,FirstName&姓。我不知道我做错了什么,有人能帮帮我吗?

<?php

    if(isset($_REQUEST['CustomerID'])) {
       $con = mysql_connect("localhost","root","");
       if (!$con) {
           die('Could not connect: ' . mysql_error());
       }
       mysql_select_db("TestDatabase", $con);

       $id = $_REQUEST['CustomerID'];

       $result = mysql_query("SELECT * FROM Customers WHERE id = '$id' ") or die('Errant query:');


       while($row = mysql_fetch_assoc($result)) {
            $output[]=$id;
       }

       print(json_encode($output));

       mysql_close($con);
   } else {
       $output = "not found";
       print(json_encode($output));
   }
?>


--
-- Database: `TestDatabase`
--

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

--
-- Table structure for table `Customers`
--

CREATE TABLE IF NOT EXISTS `Customers` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `FirstName` varchar(60) NOT NULL,
  `LastName` varchar(60) NOT NULL,
  `Age` int(11) NOT NULL,
  `Mobile` varchar(40) NOT NULL,
  `imageName` varchar(60) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

--
-- Dumping data for table `Customers`
--

INSERT INTO `Customers` (`id`, `FirstName`, `LastName`, `Age`, `Mobile`, `imageName`) VALUES
(1, 'Zur', 'Kareem', 23, 'iPhone', '1.jpg'),
(2, 'Second', 'Name', 42, 'Android', '2.jpg'),
(3, 'Dasiy', 'Shah', 29, 'iPhone', 'daisy.jpg');

/*!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 */;

0 个答案:

没有答案