I want to parse this MongoDB error message. I can't manage to access the variables in the object. I assume I overlook something trivial, but I can't figure it out.
The var_dump var_dump($error);
object(MongoDB\Driver\WriteError)#11 (4) {
["message"]=>
string(362) "E11000 duplicate key error collection: database.collection index: c_address collation: { locale: "nl", caseLevel: false, caseFirst: "off", strength: 1, numericOrdering: false, alternate: "non-ignorable", maxVariable: "punct", normalization: false, backwards: false, version: "57.1" } dup key: { address: "0x", city: "0x" }"
["code"]=>
int(11000)
["index"]=>
int(0)
["info"]=>
NULL
}
I try to access message and print it out with echo.
echo $error->message;
Throws: Warning: Undefined property: MongoDB\Driver\WriteError::$message
echo $error["message"];
Throws: Uncaught Error: Cannot use object of type MongoDB\Driver\WriteError as array
The class documentation suggestion from El_Vanja did the trick.
MongoDB\Driver\WriteError has a method [getMessage][1].
My first assumption is to check with var_dump what I get back and it's impossible to see if those variables are protected/private or not (and if they have a function).
I did some more research, converting an object to json and echo that, at least shows which variables are public (in this case none).
echo json_encode($error);
Gives: {}.
Related
Iam getting a boolean from a webservices, var_dump:
object(stdClass)#2 (1) { ["CheckIfUserCridentialsAreValidResult"]=> bool(false) }
i tried to put it in a if statement, like this:
if($response)
echo "TRUE";
else
echo "FALSE";
because if i just do:
echo $response;
But then I get this message:
"Catchable fatal error: Object of class stdClass could not be converted to string".
I tried to strictly define the variable as a Boolean, but that also didn't work. I think i need to do an extra step, i just can not find that extra step on Google or on Stack overflow.
Try:
if ($response->CheckIfUserCridentialsAreValidResult) { ...
Update: It appears, upon further experimentation, that the problem is with $val. It may have been more helpful at describing the problem had I copied the full text of the error message: "Catchable fatal error: Object of class mysqli_result could not be converted to string in ...". Evidently, PHP (for some reason) can't convert $val to a string. Note that the error message states that it is a catchable error. I had hoped that the use of the "continue" parameter would bypass this error.
The code below works provided that none of the variables are themselves an array.
foreach ($_SESSION as $key=>$val)
{echo $key. ": ".$val. "<br>";}
Through experimentation, I figured out which "$key" was itself an array. Since I figured out which variable was causing the problem, the easy solution was simply to unset it. But that is not a good solution.
unset($_SESSION['issuenum_index']);
To get around the error, I attempted to use the "continue" statement. It did not work. See the code below.
foreach ($_SESSION as $key=>$val)
{if(is_array($key)) continue;
{echo $key. ": ".$val. "<br>";}}
How can the code above be revised to skip a variable when it is an array without triggering an error message?
Update continued: The "foreach" code failed and generated the following messages when the iteration reached the $key index "issuenum_index". Note that this "foreach" code correctly identifies the "$key" index as "issuenum_index" but hangs at this part of the code: "var_dump($val);"
foreach ($_SESSION as $key=>$val)
{ var_dump($key);
var_dump($val);
echo "<br>";
}
"Warning: var_dump(): Couldn't fetch mysqli_result in ..."
*Warning: var_dump(): Property access is not allowed yet in /var/www/sfmags/testform.inc.php on line 60
object(mysqli_result)#1 (5) { ["current_field"]=> NULL ["field_count"]=> NULL ["lengths"]=> NULL ["num_rows"]=> NULL ["type"]=> NULL } *
I have a solution that answers the question as posted. Essentially the variable $val associated with ($_SESSION['issuenum_index']) is an OBJECT.
In reading up on this, I found the following code snippet, that I applied to my case.
foreach ($_SESSION as $value) {
echo gettype($value), "\n";
}
The result: "integer string object integer integer integer integer". Which disclosed that one of my elements was an "object". The third element is the variable: $_SESSION['issuenum_index'].
I revised the original code to add "not object".
foreach ($_SESSION as $key=>$val)
if(!is_object($val))
{
echo $key. ": ".$val. "<br>";
} else {
echo "$key is an object <br>";
}
The code worked. I also revised the code to identify (echo) that "$_SESSION['issuenum_index']" was an object. This revision provides me a list of all my $_Session variables. My understanding of how this all goes together is still incomplete. I don't know why it works with $val? But that is another question.
$key, at one point in cycling through "$_SESSION" stands for "issuenum_index"
$val, I believed represented a value, such as "1". However, it is reported as being an "object". While this question is solved, I will still need to learn more.
I have met this Notice while programming php with CodeIgniter
code like this.Its used for showing line and filename while logging.
I tried this
//get debug messages such as functionname, linenum,etc.
if ($level == 'debug') {
$debug_info = debug_backtrace(!DEBUG_BACKTRACE_PROVIDE_OBJECT
&& DEBUG_BACKTRACE_IGNORE_ARGS);
$debug_info = $debug_info[1];
$debug_message = '('.
isset($debug_info['file'])?$debug_info['file']:''.
isset($debug_info['class'])?$debug_info['class']:''.
isset($debug_info['type'])?$debug_info['type']:''.
isset($debug_info['function'])?$debug_info['function']:''.
isset($debug_info['line'])?$debug_info['line']:''.
')';
$message = $debug_message.$message;
}
and use array_key_exist() to make the judge ,but it still cause NOTICE like this
A PHP Error was encountered
Severity: Notice
Message: Undefined index: file
Filename: core/Common.php
Line Number: 364
Many thanks for answering the question
here is the dumped data
array(4) {
["function"]=>
string(5) "index"
["class"]=>
string(7) "Welcome"
["type"]=>
string(2) "->"
["args"]=>
array(0) {
}
}
'(' . isset(...) will always evaluate to true, hence $debug_info['file'] will always be evaluated whether it's set or not. The problem is the way you're chaining the conditions and operations. You need to delimit and group them explicitly, otherwise it doesn't do what you think it does.
'(' . (isset(..) ? 'foo' : 'bar') . ( .. ? .. : .. ) . ...
Extension from https://stackoverflow.com/a/55191/547210
I am creating a validating function to check several attributes of string variables, which may or may not have been set. (One of the attributes which is checked)
What I am trying to do with the function is receive arguments an unknown number of arguments in the form (See below), and suppress errors that may be caused by passing an unset variable.
I'm receiving the variables like validate([ mixed $... ] ) by using func_get_args()
The previous post mentioned that it was possible by passing by reference, now is this possible when the variables are passed implicitly like this?
If you pass a variable that is not set in the calling scope, the array returned by func_get_args() will contain a NULL value at the position where the variable was passed, and an error will be triggered. This error is not triggered in the function code itself, but in the function call. There is, therefore, nothing that can be done to suppress this error from within the code of the function.
Consider this:
function accepts_some_args () {
$args = func_get_args();
var_dump($args);
}
$myVar = 'value';
accepts_some_args($notSet, $myVar);
/*
Ouput:
Notice: Undefined variable: notSet in...
array(2) {
[0]=>
NULL
[1]=>
string(5) "value"
}
*/
As you can see, the variable name notSet appears in the error, telling us that the error was triggered in the caller's scope, not that of the callee.
If we want to counter the error, we could do this:
accepts_some_args(#$notSet, $myVar);
...and prefix the variable names with the evil # operator, but a better solution would be to structure our code differently, so we can do the checks ourselves:
function accepts_some_args ($args) {
var_dump($args);
}
$myVar = 'value';
$toPassToFunction = array();
$toPassToFunction[] = (isset($notSet)) ? $notSet : NULL;
$toPassToFunction[] = (isset($myVar)) ? $myVar : NULL;
accepts_some_args($toPassToFunction);
/*
Ouput:
array(2) {
[0]=>
NULL
[1]=>
string(5) "value"
}
*/
I used var_dump on an object in my code. print var_dump( $$user);
result: object(stdClass)#35 (1) { ["user1_ready"]=> string(1) "0" } How do I get to this value (0 in this case).
I tried print $$user which resulted in Catchable fatal error: Object of class stdClass could not be converted to string in
I need something like if($$user == 0){echo "error message") else {
You need to use the property/method accessor "->". It generally follows:
$object->method() or $object->property
so yours would be
$$user->user1_ready