6 Replies - 276 Views - Last Post: 01 January 2018 - 03:55 PM Rate Topic: -----

#1 [email protected]   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 24-February 15

no result from foreach

Posted 31 December 2017 - 02:05 PM

Can someone tell me why my foreach echoes array instead of the $row variable.

<!DOCTYPE html>
<html>
	<head>
		<title></title>
	</head>
	<body>

	<?php
		// Database + connection
		require_once('config.php');
		
		// Make and check the connection with PDO
		try {
			$conn = new PDO($dsn, $user, $pass);

		} catch (PDOException $e) {
			echo 'Connection failed: ' . $e->getMessage();	
		} 

		if ($conn) {
			echo 'Connected';
		}
		
		// Create query
		$result = 'SELECT * FROM Students';

		// Insert query into table
		foreach ($conn->query($result) as $row)  {
			echo $row . "/n";
		}

		// Close connection
		$conn = null;
	?>
	</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: no result from foreach

#2 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2557
  • View blog
  • Posts: 10,269
  • Joined: 03-December 12

Re: no result from foreach

Posted 31 December 2017 - 02:28 PM

Because $row is an array, not a single value.
Was This Post Helpful? 0
  • +
  • -

#3 [email protected]   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 24-February 15

Re: no result from foreach

Posted 31 December 2017 - 02:30 PM

Maybe my explanation was a little bit wrong, but you check this link to see what I mean.
http://75076.ict-lab...oject/index.php
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2557
  • View blog
  • Posts: 10,269
  • Joined: 03-December 12

Re: no result from foreach

Posted 31 December 2017 - 02:37 PM

print_r($row)
Was This Post Helpful? 0
  • +
  • -

#5 [email protected]   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 24-February 15

Re: no result from foreach

Posted 31 December 2017 - 03:50 PM

Trying to echo a table inside PHP, looking at examples etc.., but can't figure it out, it also still prints array as a string on the screen. maybe you can help me out?

// Insert query into table
	foreach ($conn->query($result) as $row) 
	{
	 	echo "<table border=1 >";
		echo	"<tr>";
		echo		"<td>";
	    print_r ($row);
		echo		"</td>";
		echo	 "</tr>";
		echo  "</table>" . "<br>";
	}


Was This Post Helpful? 0
  • +
  • -

#6 benanamen   User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 169
  • Joined: 28-March 15

Re: no result from foreach

Posted 01 January 2018 - 10:28 AM

Perhaps you should RTFM and learn what print_r is.
http://php.net/manua...ion.print-r.php

foreach ($conn->query($result) as $row)
{
echo "<td>{row['column_name']}</td>";
}

This post has been edited by benanamen: 01 January 2018 - 10:32 AM

Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is online

  • Bouncy!
  • member icon

Reputation: 6562
  • View blog
  • Posts: 26,615
  • Joined: 12-December 12

Re: no result from foreach

Posted 01 January 2018 - 03:55 PM

Look at the first example at the docs for PDO::query (or any other example of using this method).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1