Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MYSQL\u FETCH\u数组参数资源错误。_Php_Mysql - Fatal编程技术网

Php MYSQL\u FETCH\u数组参数资源错误。

Php MYSQL\u FETCH\u数组参数资源错误。,php,mysql,Php,Mysql,可能重复: 我收到以下错误消息:警告:mysql\u fetch\u array()希望参数1是资源,布尔值在C:\xampp\htdocs中给出 它只在页面第一次加载时发生。(但如果单击按钮时表单中没有数据,通常会发生这种情况-通过javascript验证解决) 有人能告诉我我要去哪里吗?第49行出现错误,…while($row=mysql\u fetch\u array($result)) 您的查询在第一页加载时失败,因为您尚未提交表单。这将导致带有空LIMIT子句的无效语法 除非填充了$

可能重复:

我收到以下错误消息:警告:mysql\u fetch\u array()希望参数1是资源,布尔值在C:\xampp\htdocs中给出

它只在页面第一次加载时发生。(但如果单击按钮时表单中没有数据,通常会发生这种情况-通过javascript验证解决)

有人能告诉我我要去哪里吗?第49行出现错误,…while($row=mysql\u fetch\u array($result))


您的查询在第一页加载时失败,因为您尚未提交表单。这将导致带有空
LIMIT
子句的无效语法

除非填充了
$\u POST['numberOfSequences']
,否则不要执行查询。此外,请确保已将POST字段过滤为数字,因为您的脚本容易受到当前形式的SQL注入的攻击

// Verify the field was submitted AND is a valid number
if (isset($_POST['numberofsentences']) && is_numeric($_POST['numberofsentences'])) {

  // Connect to server and select databse.
  // Note that quotes were removed from these variables. Unnecessary and bad practice 
  // to quote them unless they are part of larger interpolated strings.
  mysql_connect($host, $username, $password)or die("cannot connect");
  mysql_select_db($db_name)or die("cannot select DB");

  // Cast it to an integer
  $sn = intval($_POST['numberofsentences']);


  $query="SELECT line FROM sentence ORDER BY rand() LIMIT $sn";
  $result = mysql_query($query);

  $count = 0;

  // Verify the query succeeded...
  if ($result) {
    while ( $row = mysql_fetch_array($result) )
    { 
      // fetch result and do other stuff...
    }
 }
 // Query failed...
 else echo "An error occurred: " . mysql_error();
}

既然不能发布空字符串,isset就足够了。empty()是另一个有用的函数。太好了。谢谢你的帮助。现在效果很好。感谢您对语法的其他评论。
// Verify the field was submitted AND is a valid number
if (isset($_POST['numberofsentences']) && is_numeric($_POST['numberofsentences'])) {

  // Connect to server and select databse.
  // Note that quotes were removed from these variables. Unnecessary and bad practice 
  // to quote them unless they are part of larger interpolated strings.
  mysql_connect($host, $username, $password)or die("cannot connect");
  mysql_select_db($db_name)or die("cannot select DB");

  // Cast it to an integer
  $sn = intval($_POST['numberofsentences']);


  $query="SELECT line FROM sentence ORDER BY rand() LIMIT $sn";
  $result = mysql_query($query);

  $count = 0;

  // Verify the query succeeded...
  if ($result) {
    while ( $row = mysql_fetch_array($result) )
    { 
      // fetch result and do other stuff...
    }
 }
 // Query failed...
 else echo "An error occurred: " . mysql_error();
}