0

我正在尝试根据用户使用 php 发布的数据来获取一行。以下是代码。是他们的一些语法错误吗?

   Poll name: <input type="text" name="question" /><br />

    $question=$_POST['question'];

      $sql = "INSERT INTO poll_question(question)
       VALUES('" . $_POST['question'] . "')";
    $result = mysql_query($sql);

    $query=mysql_query("select * from poll_question where question = '$question'");

    $numrows=mysql_num_rows($query);

因为 numrows 的值仍然是 0 ,所以没有获取任何行。问题出在哪里?我已经连接到数据库并且“插入”查询运行良好,只有选择查询不起作用。

4

2 回答 2

0

您必须通过数据库连接才能执行查询。例如,

// Connect to the database
$dbc = mysqli_connect('hostname', 'username', 'password', 'database');

$sql = "query text";

// pass the query to the database connection
$result = mysqli_query($dbc, $sql);

// close the database connection when you're done
mysqli_close($dbc);
于 2013-04-28T19:05:30.780 回答
0

检查mysql-error()PHP 引擎抛出的错误。我认为您没有连接数据库。

 //connect the databse

 $question=$_POST['question'];
   $q=mysql_query("select * from poll_question where question='$question'") or die(mysql_error());                  
  if($q)     //check the $q have any value
  {
    $sql = "INSERT INTO poll_question(question)
   VALUES('" . $_POST['question'] . "')";
    $result = mysql_query($sql) or die(mysql_error());

   // query for fetching records
     $query=mysql_query("select * from poll_question where question = '$question'") or die(mysql_error()); 

     $numrows=mysql_num_rows($query);
     if($numrows)
   {  
     echo "have rows!";
   }
   else
 {  
 echo "does not have rows";

}
}
else

{
echo " question is already exist";

}
    // form part

 ?>


   Poll name: <input type="text" name="question" /><br />
   <input type-"submit" name="submit" value="Submit">
于 2013-04-28T18:41:07.170 回答