who can help me i have this error when i want go to the file article.php:
Fatal error: Uncaught exception 'MongoException' with message 'Invalid object ID' in /Applications/XAMPP/xamppfiles/htdocs/www/Site/Test/article1.php:14 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/www/Site/Test/article1.php(14): MongoId->__construct('Notice...') #1 {main} thrown in
beginning of the file article1.php
<?php
$id = $_GET['id'];
try {
$connection = new MongoClient();
$database = $connection->selectDB('test');
$collection = $database->selectCollection('articles');
} catch(MongoConnectionException $e) {
die("Failed to connect to database ".$e->getMessage());
}
$article = $collection->findOne(array('_id' => new MongoId($id)));
?>
You are only catching an exception of the type "MongoConnectionException" while the code is throwing a more generic "MongoException"
Catch(MongoException $e)
Related
I have been working at a code that used to work. However, suddenly, this message pops up.
Fatal error: Uncaught exception 'Exception' with message 'Email is incorrect!' in C:\xampp\htdocs\Prototype\classes\User.php:23
Stack trace: #0 C:\xampp\htdocs\Prototype\index.php(14): User->setEmail(true) #1 {main} thrown in C:\xampp\htdocs\Prototype\classes\User.php on line 23
But I just don't understand what this means. I tried using try and catch, but it keeps popping up.
This is the code where the error occurs
public function setEmail($p_email)
{
if (empty($p_email)) {
throw new Exception('Email kan niet leeg zijn!');
}
$this->email = $p_email;
if (!filter_var($p_email, FILTER_VALIDATE_EMAIL)) {
throw new Exception('Email is incorrect!'); //here is the error (line 23)
}
}
this is the code where it is summoned
$user = new User();
$user->setEmail($_SESSION['login']); //line 14
$currentUser = $user->getProfile();
$userEmail = $user->getEmail();
$userName = $user->getUserName();
$userID = $currentUser['userID'];
Your error is that you are passing a boolean instead of a valid string email...
Stack trace: #0 C:\xampp\htdocs\Prototype\index.php(14): User->setEmail(true) #1 {main} thrown in C:\xampp\htdocs\Prototype\classes\User.php on line 23
So, this line is incorrect:
$user->setEmail($_SESSION['login']); //line 14
I have
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once "configuration.php";
header('Content-Type: application/json');
try
{
$mysqli = new mysqli(MYSQL_SERVER, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATABASE);
$mysqli->set_charset("utf8");
} catch (Exception $e) {
echo json_encode(
array(
'msg' => $e->getMessage()
)
);
}
And if mysqli is not enabled then it does not catch the error:
Fatal error: Uncaught Error: Class 'mysqli' not found in C:\test\db_connect.php:8
Stack trace:
#0 C:\test\getContacts.php(2): require_once()
#1 {main} thrown in C:\test\db_connect.php on line 8
What can I do so that it catches the error?
I have tried this one but it didn't work:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once "configuration.php";
header('Content-Type: application/json');
try
{
if(!extension_loaded('mysqli'))
{
throw new Exception('mysqli is not enabled');
}
$mysqli = new mysqli(MYSQL_SERVER, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATABASE);
$mysqli->set_charset("utf8");
} catch (Exception $e) {
echo json_encode(
array(
'msg' => $e->getMessage()
)
);
}
This one does not halt, continues to execute the script.
{"msg":"mysqli is not enabled"}
Notice: Undefined variable: mysqli in C:\test\getContacts.php on line 99
Fatal error: Uncaught Error: Call to a member function query() on null in C:\test\getContacts.php:99
Stack trace:
#0 {main}
thrown in C:\test\getContacts.php on line 99
It's odd that it wouldn't be installed but if you're rolling your own I guess it could be omitted. I would check to see if the procedural functions exist
if(!function_exists('mysqli_connect')) {
throw new Exception('mysqli is not enabled');
}
As the question is tagged php-7: An error in php 7 can be caught but it does not inherit from Exception so you have to catch them differently:
...
} catch (Error $e) {
^^^^^ Not Exception
echo json_encode(
array(
'msg' => $e->getMessage()
)
);
// stop execution
exit;
}
See the manual for more information about error handling in php 7.
When i try to run php code using Parse.com PHP SDK it
the error is "Account already exists for this username"
but it throw all this error message:
Fatal error: Uncaught exception 'Parse\ParseException' with message 'Account already exists for this username' in /Users/yousef/Desktop/project/vendor/parse/php-sdk/src/Parse/ParseClient.php:357 Stack trace: #0 /Users/yousef/Desktop/project/vendor/parse/php-sdk/src/Parse/ParseObject.php(1038): Parse\ParseClient::_request('POST', 'classes/_User', NULL, '{"username":"my...', false) #1 /Users/yousef/Desktop/project/vendor/parse/php-sdk/src/Parse/ParseObject.php(947): Parse\ParseObject::deepSave(Object(Parse\ParseUser), false) #2 /Users/yousef/Desktop/project/vendor/parse/php-sdk/src/Parse/ParseUser.php(108): Parse\ParseObject->save() #3 /Users/yousef/Desktop/project/test.php(20): Parse\ParseUser->signUp() #4 {main} thrown in /Users/yousef/Desktop/project/vendor/parse/php-sdk/src/Parse/ParseClient.php on line 357
The Code i Use
<?php
require 'vendor/autoload.php';
use Parse\ParseClient;
ParseClient::initialize('YousefId', '', 'YousefMaster');
ParseClient::setServerURL('server-ip:1337/parse');
use Parse\ParseUser;
$user = new ParseUser();
$user->set("username", "my name");
$user->set("password", "my pass");
$user->set("email", "email#example.com");
$user->set("phone", "415-392-0202");
try {
$user->signUp();
} catch (ParseException $error) {
echo $error->getCode();
echo $error->getMessage();
}
?>
so how do i just show the error code and message instead of showing all this error.
You need to refer to the ParseException within the Parse namespace.
Try
catch (Parse\ParseException $error) {
// ...
}
I am trying to catch an error if I cannot connect to database (eg xamp down or database connection down etc). I have tried to use PDO::errorCode() but yield no reuslts.
In my php I have a connection.php and a method ' to connect to the datatbase many times and return a new PDO instance to achieve various queries. The response displays the error message then all the php queries that call it, which also includes the name and password of the database in a non encrypted format.
How can I catch this error and replace this error message with a human readable alternative (that doesnt display the name and password)?
CONNECTION.PHP
function connect()
{
// set database server access variables:
$host = "localhost";
$user = "root";
$pass = "password";
$dbase = "dbName";
//Establish a connection
$db = new PDO("mysql:host=".$host."; dbname=".$dbase."", $user, $pass); //line 16
$db -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
return $db;
}
RESPONSE
Fatal error: Uncaught exception 'PDOException' with message
'SQLSTATE[HY000] [2002] No connection could be made because the target
machine actively refused it. ' in
D:\Users\Username\Dropbox\Web\htdocs\sitename\php\data\connection.php:16
Stack trace: #0
D:\Users\Username\Dropbox\Web\htdocs\sitename\php\data\connection.php(16):
PDO->__construct('mysql:host=loca...', 'root', 'password') #1
D:\Users\Username\Dropbox\Web\htdocs\sitename\php\function\active-user.php(28):
connect() #2
D:\Users\Username\Dropbox\Web\htdocs\sitename\map-floorplan.php(10):
include('D:\Users\Username...') #3 {main} thrown in
D:\Users\Username\Dropbox\Web\htdocs\sitename\php\data\connection.php
on line 16
do a try/catch to catch exceptions
try{
$db = new PDO("mysql:host=".$host."; dbname=".$dbase."", $user, $pass); //line 16
$db -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
return $db;
} catch( PDOException $e){
$originalError = $e->getMessage();
echo 'something went wrong.. '.$originalError;
exit;
}
When I retrieve two tables there's an error. What is the problem with my code? I have no idea how to fix this
<?php
include ('includes/config.php');
$mysqli = new mysqli(DB_SERVER, DB_UNAME,DB_PASSWD,DB_NAME);
if(!$mysqli){
throw new Exception($mysqli->connect_error, $mysqli->connect_errno);
}
$jqry = $mysqli->prepare("SELECT time FROM table_time ORDER BY time");
if (!$jqry){
throw new Exception($mysqli->error);
}
$jqry->execute();
$jqry->bind_result($time);
$jqry->store_result();
$times = array();
while ($jqry->fetch()){
$times[] = $time;
}
$jqry->close();
$gqry = $mysqli->prepare("SELECT table_group.group FROM table_group.table_group ORDER BY group");
if(!$gqry){
throw new Exception($gqry->error);
}
$gqry->execute();
$gqry->bind_result($group);
$gqry->store_result();
$groups = array();
while ($gqry->fetch()){
$groups[] = $group;
}
?>
This is the error I got:
Notice: Trying to get property of non-object in C:\xampp\htdocs\~Jeremiah\system5\joborder.php on line 31
Fatal error: Uncaught exception 'Exception' in C:\xampp\htdocs\~Jeremiah\system5\joborder.php:31 Stack trace: #0 {main} thrown in
your SQL just before the line 31 is invalid. Try executing it against db directly.
from the manual:
mysqli_prepare() returns a statement object or FALSE if an error occurred.
...and thats why we don't suppress notices in dev enviroment! :)