为什么我的查询不输出预期结果?

时间:2010-10-16 17:36:08

标签: php mysql

    CREATE TABLE IF NOT EXISTS `Channels` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(30) NOT NULL,
  `commercial` tinyint(1) NOT NULL DEFAULT '0',
  `usrid` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;


INSERT INTO `Channels` (`id`, `name`, `commercial`, `usrid`) VALUES
(2, 'ORF 1', 0, 0);

PHP:

<?php
  if (isset($_POST['name'])){
    mysql_connect("localhost", "test", "test") or die(mysql_error());
    mysql_select_db("test") or die(mysql_error());
    $tmp = mysql_query("SELECT commercial 
                          FROM Channels 
                         WHERE name='".mysql_real_escape_string($_POST['name'])."'");
    $row = mysql_fetch_row($tmp);
    echo $row['commercial'];
  }
  else
  {
    ?>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
      <input name="name" type="text">
      <input type="submit" name="submit" value="submit" >
    </form>
    <?php
  }
?>

提交“ORF 1”时没有输出。

1 个答案:

答案 0 :(得分:0)

学习使用基本的调试技术至关重要:

而不是:

$tmp = mysql_query("SELECT commercial FROM Channels WHERE 
    name='".mysql_real_escape_string($_POST['name'])."'");
$row = mysql_fetch_row($tmp);
echo $row['commercial'];

这样做:

$tmp = mysql_query("SELECT commercial FROM Channels WHERE 
    name='".mysql_real_escape_string($_POST['name'])."'");

// Make sure the query was successful
if (false === $tmp) {
   die("Query failed: " . mysql_error());
}

$row = mysql_fetch_row($tmp);

// Don't assume a row was returned
var_dump($row);