注意:未定义的变量:我在第20行的F:\ wamp \ www \ search \ search.php和

时间:2015-12-20 07:26:59

标签: php mysql database

我试图解决此错误但失败了。我正在尝试创建像谷歌这样的搜索引擎,但有2个错误如下: -

错误编号。 1

Notice: Undefined variable: i in F:\wamp\www\searchwall\search.php on line 20

这是错误2号

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in F:\wamp\www\searchwall\search.php on line 31

*这里的代码和文件名是search.php *

<!DOCTYPE HTML>
<html>
 <head>
  <title>
  SearchWall search 
  </title>
 </head>
 <body>
     <h2>WELCOME TO SEARCHWALL!!</h2>
   <form action ='./search.php' method= 'get'>
     <input type='text' name='r' size ='50' value='<?php echo $_GET['r']; ?>' />
     <input type='submit' value='search' />
   </form>
   <hr />
   <?php
    $r = $_GET['r'];
    $terms = explode(" ", $r);
    $query = "SELECT * FROM searches WHERE ";
    foreach ($terms as $each) {
     $i++;
      if ($i == 1)
        $query .= "keywords LIKE '%$each%' ";
      else
        $query .= "OR keywords LIKE '%$each%' ";
    }
    // connect
    mysql_connect("localhost","","");
    mysql_select_db("search");

      $query = mysql_query($query);
      $numrows = mysql_num_rows($query);
      if ($numrows > 0 ){
         while ($row = mysql_fetch_assoc($query)) {
          $id = $row['id'];
          $title = $row['title'];
          $description = $row['description'];
          $keywords = $row['keywords'];
          $link = $row['link'];

           echo"<h2><a href='$link'>$title</a></h2>
           $description<br /><br />";
         }
      }
      else
        echo "No results found for \"<b>$r</b>\"";
    // disconnect
    mysql_close();
   ?>
 </body>
</html>

2 个答案:

答案 0 :(得分:4)

在引用$i之前,您尚未初始化0。只需将其初始化为$i = 0; foreach ($terms as $each) { $i++; # etc. ,您就可以了。

#include <stdio.h>
#include <stdint.h>
#include <string.h>

int main() {
    uint8_t oct1, oct2, oct3, oct4;

    char buf[20];
    memset(buf, 0, sizeof(buf));

    sprintf(buf,"%d.%d.%d.%d", 1, 2, 3, 4);
    printf("%s\n", buf);
    int f = sscanf(buf,"%d.%d.%d.%d", &oct1, &oct2, &oct3, &oct4);
    printf("%d.%d.%d.%d \nSuccessfully read - %d\n", oct1, oct2, oct3, oct4, f);
    return 0;
}

Output:
    1.2.3.4
    0.0.0.4
    Successfully read - 4

答案 1 :(得分:0)

首先你需要在循环之前定义$ i,然后你不应该写 WHERE ,然后确保有搜索的东西,或者你应该保留它WHERE 1 = 1

<?php

$pyramid = [];
$levels_count = 3; // height of the pyramid
$parts_count = 4; // number of subdividing parts
$index = 1; // starting index to numerate vertices
$level = 0;

$columns_count = pow($parts_count, $levels_count - 1);
for ($level = 0; $level < $levels_count; $level++) {
  $level_items_count = pow($parts_count, $level);
  $pyramid[$level] = [
    'colspan' => $columns_count / $level_items_count,
    'items' => range($index, $index + $level_items_count - 1)
  ];
  $index += $level_items_count;
}

// The following code prints the prepared pyramid data
?>
<style>
td {
  text-align: center;
  background-color: #ccc;
}
</style>
<table>
<?php foreach ($pyramid as $row): ?>
  <tr>
  <?php foreach ($row['items'] as $col): ?>
    <td colspan="<?= $row['colspan']; ?>"><?= $col; ?></td>
  <?php endforeach; ?>
  </tr>
<?php endforeach; ?>
</table>