0

更具体地说,我收到:

*注意:第 38 行 C:\xampp\htdocs\Project1_Tables\index.php 中的数组到字符串转换 注意:第 39 行 C:\xampp\htdocs\Project1_Tables\index.php 中的数组到字符串转换*

这是代码

<?php

    //SETUP FOR CONNECTION
    $host = "localhost";
    $user = "root";
    $password = "password";
    $database = "test";

    $con = mysqli_connect($host, $user, $password, $database);

    $tableName = "funding";
    $sql = "";

    //QUERIES NEEDED TO GATHER DATA FOR DROP-DOWN MENU'S (Research Theme and Institution)
    $column = "research_theme";
    $query = sprintf("SELECT DISTINCT %s FROM ", $column);
    $sql = $query.$tableName;
    $ddThemeList = mysqli_query($con, $sql);

    $column = "institution";
    $query = sprintf("SELECT DISTINCT %s FROM ", $column);
    $sql = $query.$tableName;
    $ddInstitutionList = mysqli_query($con, $sql);

    $list1 = mysqli_fetch_array($ddThemeList, MYSQLI_NUM);
    $list2 = mysqli_fetch_array($ddInstitutionList, MYSQLI_NUM);
    require_once("support.php");

    $body="";

    $scriptName = $_SERVER["PHP_SELF"];
    $topPart =  <<<EOBODY
            <form action="$scriptName" method="post">
                <p>
                    Theme
                    <select name="theme">
                        <?php if(mysqli_num_rows($list1) != 0) { ?>
                            <?php while ($list1) { ?>
                                <option value="<?php echo $list1[0]; ?>">
                                    <?php echo $list1[0]; ?>
                                </option>
                            <?php } ?>
                        <?php } ?>
                    </select>
                </p>

            </form>
EOBODY;

    $body = $topPart.$body;

    $page = generatePage($body);
    echo $page;

?>
4

3 回答 3

0

您的问题来自 ,您使用array (list1)inmysqli_num_rows而不是 a mysqli_result

在第 39 行,您使用 anarray作为条件语句while而不是 string/int/bool。

<?php

/*
** SOME CODE
**
*/

$list1 = mysqli_fetch_array($ddThemeList, MYSQLI_NUM);

/*
** MORE CODE
**
*/

                    <select name="theme">
                        <?php if(mysqli_num_rows($ddThemeList) != 0) { ?>
                            <?php for($i = 0;$list1[$i]; $i++) { ?>
                                <option value="<?php echo $list1[$i]; ?>">
                                    <?php echo $list1[$i]; ?>
                                </option>
                            <?php } ?>
                        <?php } ?>
                    </select>

/*
** MORE CODE
*/

?>
于 2013-07-18T14:00:09.890 回答
0

这条线不会做任何事情:

<?php while ($list1) { ?>

像这样的东西可能更像你需要的东西,虽然我有一段时间没有使用 mysql 或 mysqli_ 函数:

  <?php

      //SETUP FOR CONNECTION
      $host = "localhost";
      $user = "root";
      $password = "password";
      $database = "test";

      $con = mysqli_connect($host, $user, $password, $database);

      $tableName = "funding";
      $sql = "";

      //QUERIES NEEDED TO GATHER DATA FOR DROP-DOWN MENU'S (Research Theme and Institution)
      $column = "research_theme";
      $query = sprintf("SELECT DISTINCT %s FROM ", $column);
      $sql = $query.$tableName;
      $ddThemeList = mysqli_query($con, $sql);

      $column = "institution";
      $query = sprintf("SELECT DISTINCT %s FROM ", $column);
      $sql = $query.$tableName;
      $ddInstitutionList = mysqli_query($con, $sql);

      $list1 = mysqli_fetch_array($ddThemeList, MYSQLI_NUM);
      $list2 = mysqli_fetch_array($ddInstitutionList, MYSQLI_NUM);
      require_once("support.php");

      $body="";

      $scriptName = $_SERVER["PHP_SELF"];
      $topPart =  <<<EOBODY
              <form action="$scriptName" method="post">
                  <p>
                      Theme
                      <select name="theme">
  EOBODY;


            foreach ($list1 as $item) {
              $topPart .= "<option value='{$item}'>{$item}</option>";
            }

        TopPart .= <<<EOBODY
                      </select>
                  </p>

              </form>
  EOBODY;

      $body = $topPart.$body;

      $page = generatePage($body);
      echo $page;
于 2013-07-18T14:05:30.680 回答
0

如果我没记错的话,您的 php 指令不会在 heredoc 语句中进行评估,但您的变量是。因此,您的块只是试图回显 "$list1" 而没有被认为是字符串的 while 或 if 语句。

在忽略 php 语句的情况下,heredoc 只是试图回显“$list1”,这是一个数组而不是字符串。

于 2013-07-18T14:06:37.337 回答