Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
read.php

PHP
<?php
require_once('../dao/config.php');
require_once('../dao/crudDAO.php');

$list = crudDAO::read();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CRUD



	<div>
    	<div>
        	
            	                	<?php foreach($list as $key => $value) { ?>
                    
                                        <?php } ?>
            <table><tbody><tr>                	<td>id</td>                    <td>Name</td>                    <td>Email</td>                    <td>Address</td>                    <td>Mobile number</td>                </tr><tr><td><?= $value['id'] ?></td>                    <td><?= $value['name'] ?></td>                    <td><?= $value['email'] ?></td>                    <td><?= $value['address'] ?></td>                    <td><?= $value['mob'] ?></td>                </tr></tbody></table>
        </div>
    </div>



error:
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\crud_php\oop_crud\pages\read.php on line 25
id Name Email Address Mobile number



crudDAO.php

PHP
<?php
class CrudDAO {
	public function create($name, $email, $address, $mob) {
		global $db;
		$sql = "INSERT INTO users SET name = '$name', email = '$email', address = '$address', mob = '$mob'";
		$result = $db->query($sql);
		
		if($result) {
			return $result;
		} else {
			return false;
		}
	}
	
	public function read() {
		global $db;
		$sql = "SELECT * FROM users ORDER BY id";
		$result = $db->query($sql);
		
		if($result->num_rows > 0) {
			$i = 0;
			$list = " ";
			while($row = $result->fetch_assoc()) {
				$list[$i] = $row;
				$i++;
			}
			return $list;
		} else {
			return false;
		}
	}
	
	public function update() {
		global $db;
	}
	
	public function delete() {
		global $db;
	}
}
?>


error:
Notice: Array to string conversion in C:\xampp\htdocs\crud_php\oop_crud\dao\crudDAO.php on line 24

Notice: Array to string conversion in C:\xampp\htdocs\crud_php\oop_crud\dao\crudDAO.php on line 24

Notice: Array to string conversion in C:\xampp\htdocs\crud_php\oop_crud\dao\crudDAO.php on line 24

Notice: Array to string conversion in C:\xampp\htdocs\crud_php\oop_crud\dao\crudDAO.php on line 24

Notice: Array to string conversion in C:\xampp\htdocs\crud_php\oop_crud\dao\crudDAO.php on line 24

What I have tried:

Find solution on internet

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\crud_php\oop_crud\pages\read.php on line 25
id Name Email Address Mobile number



Notice: Array to string conversion in C:\xampp\htdocs\crud_php\oop_crud\dao\crudDAO.php on line 24

Notice: Array to string conversion in C:\xampp\htdocs\crud_php\oop_crud\dao\crudDAO.php on line 24

Notice: Array to string conversion in C:\xampp\htdocs\crud_php\oop_crud\dao\crudDAO.php on line 24

Notice: Array to string conversion in C:\xampp\htdocs\crud_php\oop_crud\dao\crudDAO.php on line 24

Notice: Array to string conversion in C:\xampp\htdocs\crud_php\oop_crud\dao\crudDAO.php on line 24
Posted
Updated 3-Apr-18 21:35pm
v2

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\crud_php\oop_crud\pages\read.php on line 25
is issued for
PHP
<?php foreach($list as $key => $value) { ?>
The $list argument is invalid. PHP: foreach - Manual[^] expects an array expression.

It is assigned here
PHP
$list = crudDAO::read();

So the next step would be inspecting that function.
PHP
if($result->num_rows > 0) {
    $i = 0;
    $list = " ";
    while($row = $result->fetch_assoc()) {
        $list[$i] = $row;
        $i++;
    }
    return $list;
} else {
    return false;
It has two return paths. If not matching recordsets can be found the boolean value false is returned. So you have to check for that return value when calling the function or return an empty array instead of a boolean.

There are also the other warnings
Notice: Array to string conversion in C:\xampp\htdocs\crud_php\oop_crud\dao\crudDAO.php on line 24
They inform you that $list is an array of strings. But you want an array of an array. The solution is to declare $list as array. Putting it together you may use something like
PHP
$list = array();
if($result->num_rows > 0) {
    $i = 0;
    while($row = $result->fetch_assoc()) {
        $list[$i] = $row;
        $i++;
    }
}
return $list;
}
 
Share this answer
 
Comments
Member 13761429 4-Apr-18 4:45am    
thanks....
Jochen Arndt 4-Apr-18 5:04am    
You are welcome and thank you for accepting my solution.
Even without any error, I fear this is an empty loop:
PHP
<?php foreach($list as $key => $value) { ?>
<?php } ?>


PHP
$sql = "INSERT INTO users SET name = '$name', email = '$email', address = '$address', mob = '$mob'";

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 
v2
Comments
Member 13761429 4-Apr-18 1:05am    
please help me to solve my above foreach problem
Patrice T 4-Apr-18 1:21am    
"foreach($list as $key => $value)"
where have you seen this syntax in php manual ?
Jochen Arndt 4-Apr-18 3:38am    
http://php.net/manual/en/control-structures.foreach.php:
foreach (array_expression as $key => $value)
Patrice T 4-Apr-18 4:03am    
Ok
Member 13761429 4-Apr-18 7:17am    
then how to solve this problem and protect my code from sql injection

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900