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.
Any help would be greatly appreciated.
echo
takes string as parameter butmysql_fetch_array
returns array - message that you received means that array is converted to string to be printed byecho
. Try to use var_dump to print out the array:To print out all the rows returned by query use something like this:
You could also just convert the array to a string like so:
You receive the error message:
because you're doing this:
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):