Prominent Error of PHP: Base table or view not found (SQLSTATE[42S02])
0 1225
Reason for this Error
PHP developers usually comes across the error "Base table or view not found" which is also stated as SQLSTATE[42S02], when the code is applied to a database table that does not exist, or (for some reason) could not be found.
For Instance:-
$user_name="abc";
$data=array('user_name'=>$user_name);
DB::table('xyz_table')->insert($data);
The above code example is throwing "SQLSTATE[42S02]: Base table or view not found:" because we are trying to insert data into a table that does not exist in the database.
Resolution
To resolve this error, PHP developer must make sure that the table called thought the code is present firstly. Also ensure that you wrap the call to insert in a try/catch block, as in the previous two examples, this will catch the QueryException, Let’s look through an example to understand better.
For Instance:-
$user_name="abc";
$data=array('user_name'=>$user_name);
try {DB::table('xyz_table')->insert($data);
} catch (QueryException $excep) {
printf ("Error occurred: %s\n”, $excep ->getMessage( ));
}
Share:
Comments
Waiting for your comments