简体   繁体   中英

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:

<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. 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? I know that 5 rows of data are returned, because the mysql_num_rows works when I echo it out.

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 . Try to use var_dump to print out the array:

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.

Such an echo call made with an array gives you the notice message.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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