简体   繁体   English

PHP表单返回通知:数组到字符串的转换

[英]PHP form returns Notice: Array to string conversion

I have a simple form that arrives at the following code to be processed. 我有一个简单的表格,其中包含以下要处理的代码。

include("connect.php");

$items = array_key_exists('equipment', $_POST) ? $_POST['equipment'] : '';

if(!empty($items))
{
    if ($_POST["equipment"] == "computer") {
        echo "checked computer!";
    } else if($_POST["equipment"] == "projector")
    {
        echo "checked projector!";

        $sql = "SELECT room_name, day_avail, from_time, to_time, equip_name
        FROM rooms
        JOIN equipment ON (equipment.room_id = rooms.room_id)
        JOIN room_availability ON (room_availability.room_id = rooms.room_id)
        WHERE equip_name='Projector'
        GROUP BY day_avail";

        $myData = mysql_query($sql, $conn) or die(mysql_error());

        $row = mysql_fetch_array($myData);
        echo $row;
        // echo mysql_num_rows($myData);

    } else if($_POST["equipment"] == "whiteboard")
    {
        echo "checked whiteboard!";
    } else if($_POST["equipment"] == "visualiser")
    {
        echo "checked visualiser!";
    } else if($_POST["equipment"] == "desk")
    {
        echo "checked desk!";
    }
} else {
    echo "> Sorry, You have not made a selection.";
}

The form gets called via a jQuery AJAX call like so: 通过jQuery AJAX调用来调用表单,如下所示:

<script>
  $('form').submit(function(){
      var str = $(this).serialize();
      $.ajax({
        url: "userLogic.php",
        type: "post",
        data: str,
        cache: false
      }).done(function( html ) {
        $("#rooms_wrap").append(html);
      });
  });
</script>

When the projector checkbox is selected, and the form submitted, I was hoping to get the list of rooms out from the database, so that I may print them out via a PHP foreach loop. 选中projector复选框并提交表单后,我希望从数据库中获取房间列表,以便可以通过PHP foreach循环将它们打印出来。 Instead I get the error saying: 相反,我得到的错误是:

Notice: Array to string conversion 注意:数组到字符串的转换

How can I obtain the list of rooms, and print them out in a foreach loop? 如何获取房间列表,并在foreach循环中将其打印出来? I know that 5 rows of data are returned, because the mysql_num_rows works when I echo it out. 我知道返回了5行数据,因为当我将其回显时, mysql_num_rows可以工作。

echo takes string as parameter but mysql_fetch_array returns array - message that you received means that array is converted to string to be printed by echo . echo将字符串作为参数,但是mysql_fetch_array返回array-收到的消息表示将数组转换为要通过echo打印的字符串。 Try to use var_dump to print out the array: 尝试使用var_dump打印出数组:

var_dump($row);

To print out all the rows returned by query use something like this: 要打印出查询返回的所有行,请使用以下命令:

while($row = mysql_fetch_array($myData)) {
    echo $row["room_name"]; // for example
}

You receive the error message: 您收到错误信息:

Notice: Array to string conversion 注意:数组到字符串的转换

because you're doing this: 因为您正在这样做:

    $row = mysql_fetch_array($myData);
    echo $row;

when projector is checked. 检查projector时。

Such an echo call made with an array gives you the notice message. 用数组进行的echo调用会给您通知消息。

Try to do something like this (for example): 尝试执行以下操作(例如):

while ($row = mysql_fetch_array($myData)) {
    var_dump($row);
    // or:
    // print_r($row);
}

You could also just convert the array to a string like so: 您也可以将数组转换为如下所示的字符串:

    $arr = $_POST;
    echo implode(', ', $arr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM