debug_backtrace - long parameter - php

I have the following function:
function backtrace($Object=false)
{
$x = 0;
foreach((array)debug_backtrace($Object) as $aVal)
{
$row[$x]['file'] = $aVal['file'];
$row[$x]['line'] = $aVal['line'];
$row[$x]['function'] = $aVal['function'];
$row[$x]['class'] = $aVal['class'];
$row[$x]['args'] = $aVal['args'];
++$x;
}
return $row;
}
But when I use it, I'm getting an error like below:
Warning: debug_backtrace() expects parameter 1 to be long, string given in /mypath/ on line 717 ---> foreach((array)debug_backtrace($Object) as $aVal)
What's causing the error? How can I fix it?

The first parameter of debug_backtrace() is a bitmask of options (i.e. a long). It is a simple boolean true/false in PHP versions prior to 5.3.6.
To fix it, either don't pass in the $Object variable you're currently passing in or update it to be any combination of the supported options that you want to be used.
Example:
$Object = DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT;
If you want to add a pre-condition to your current block of code that will set a default value if $Object is invalid, you could try something like:
function backtrace($Object = false) {
if (!is_long($Object) || (!($Object & DEBUG_BACKTRACE_PROVIDE_OBJECT) && !($Object & DEBUG_BACKTRACE_IGNORE_ARGS))) {
$Object = 0;
}
$x = 0;
foreach((array)debug_backtrace($Object) as $aVal) {
$row[$x]['file'] = $aVal['file'];
$row[$x]['line'] = $aVal['line'];
$row[$x]['function'] = $aVal['function'];
$row[$x]['class'] = $aVal['class'];
$row[$x]['args'] = $aVal['args'];
++$x;
}
return $row;
}

for php >= 5.3.6, you should use bitmask options
function backtrace($Object=false) {
$x = 0;
foreach((array)debug_backtrace($Object ? DEBUG_BACKTRACE_PROVIDE_OBJECT : 0) as $aVal)
{
$row[$x]['file'] = $aVal['file'];
$row[$x]['line'] = $aVal['line'];
$row[$x]['function'] = $aVal['function'];
$row[$x]['class'] = $aVal['class'];
$row[$x]['args'] = $aVal['args'];
++$x;
}
return $row;
}

Related

I receive the following error : " Can't use method return value in write context "

<?php
include("Emp.php");
$Email = $_GET["id"];
User::FileLoader();
$content = "";
foreach (User::$userlist as $user) {
if ($user->get_Email() == $Email) {
if ($user->get_State() == 1) {
$user->set_State() = 0;
}
else if ($user->get_State() == 0) {
$user->set_State() = 1;
}
}
}
header("location:liste.php");
The error message may be a little obtuse, but it's not that hard to understand. The return value of a function call is just that -- a value. Unlike a variable, it is not backed by persistent storage, so you cannot write to it by, for example, using it as the left-hand operand of an assignment.
Thus, both this ...
$user->set_State() = 0;
... and this ...
$user->set_State() = 1;
... are wrong.
You have not presented your User class to inform an answer, but surely the set_State() method expects you to specify the new state via an argument. For example,
$user->set_State(0);

Laravel Trying to Get property of non object (from eloquent model)

Already read some of questions about this problem on stackoverflow and none of that answers apply to me.
When I run:
$item_price = ItemPrice::where('item_name',$itemname)->first();
and then
$item_price->price
I get Trying to get property of non-object but when I run:
dd($item_price = ItemPrice::where('item_name',$itemname)->first());
It's returning object with attributes name, price etc. I don't really understand what is happening here.
Full code:
foreach ($inventorydecoded->assets as $asset) {
$i = 0;
$a = 0;
while ($a < 1) {
if ($inventorydecoded->descriptions[$i]->classid == $asset->classid) {
$a = 1;
$classid = $inventorydecoded->descriptions[$i]->classid;
$itemname = $inventorydecoded->descriptions[$i]->market_hash_name;
$tradable = $inventorydecoded->descriptions[$i]->tradable;
$name_color = $inventorydecoded->descriptions[$i]->name_color;
;
}
$i++;
} // end of while
if ($tradable === 1 && strpos_arr($itemname, $blacklist) == false ) {
$item_price = ItemPrice::whereItemName($itemname)->first();
// dd(ItemPrice::where('item_name',$itemname)->first());
$items[] = ['assetid' => $asset->assetid,'classid'=> $classid,'itemname'=>$itemname,'name_color'=>$name_color,'price'=> $item_price->price];
$serialized_inventory = serialize($items);
}
} // end of foreach
You're using this query in loop, so one of those is empty and returns null. So you need to do simple check:
if (is_null($item_price)) {
// There is no price for this item, do something.
}
Try this:
$item_price = ItemPrice::whereItemName($itemname)->first();

mysqli_num_rows from class function error

This is the first time I learn class in PHP, I tried to make a simple search in database.
here is some script from my class:
class DB {
...
function list_query($query) {
$ns = array();
$q = mysqli_query($this->con_(), $query);
while($n = mysqli_fetch_assoc($q)) {
$ns[] = $n;
}
return $ns;
}
...
function num_query($q) {
$num = mysqli_num_rows($q);
return $num;
}
...
}
search script :
$key = "foo";
$qsearch = $db->list_query("SELECT * FROM posts WHERE content LIKE '%".$db->escape_query($key)."%'");
$num = $db->num_query($qsearch);
if ($num == 0) {
echo "<h2>not found</h2>";
} else {
echo "<h2>result for : ".$key."</h2>";
foreach($qsearch as $val) {
echo "<h4>".$val['title']."</h2>";
echo strip_tags($val['content']);
}
}
but there is an error with the num_query() function.
with warning :
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, array given in class/db.php on line 30.
I have checked it with manual mysqli_query() then use the num_query() function, it's work well.
sorry for my english
Your list_query() returns array. And your num_query expects mysqli_result as a parameter.
So, when you write $qsearch = $db->list_query(), you're getting an array; and then you pass that array to num_query.
Perhaps, your num_query should be like:
function num_query($result) {
return count($result);
}

strlen() expects parameter 1 to be string, array given

I have an older site using phorm. When the form is submitted I get this error:
Warning: strlen() expects parameter 1 to be string, array given in /home/user/public_html/form/phorm.php on line 2015
I know this is because of an upgrade to php 5.3, is there an easy method to fix that line of code to make my forms work again?
if (strlen($PHORM_ALERTTO) && !strlen($PHORM_TO) && !$PHORM_INFONLY && !$ph_GotData) {
$PHORM_TO = $PHORM_ALERTTO; //THIS IS LINE 2015
Read the mail template file(s) and mail it (them) to the user */
$ph_section = "user template";
if (strlen($PHORM_ALERTTO) && !strlen($PHORM_TO) && !$PHORM_INFONLY && !$ph_GotData) {
$PHORM_TO = $PHORM_ALERTTO; //THIS IS LINE 2015
settype($PHORM_TO, "array");
}
if (isset($PHORM_TMPL) && isset($PHORM_TO) && !$ph_Abort) {
if ($ph_debug2) echo "<B>JS:</B> Mail Template(s)<BR>";
if (count($PHORM_TMPL) > $ph_MaxTMPL) $ph_Alerts['120'] = ph_Message("A120");
list(,$fPHORM_TO) = each($PHORM_TO);
list(,$fPHORM_SUBJECT) = each($PHORM_SUBJECT);
while ($ph_MaxTMPL-- && list($ph_key, $lPHORM_TMPL) = each($PHORM_TMPL)) {
if ($lPHORM_TMPL == ph_GENERIC) $lPHORM_TMPL = "$ph_root/files/generic.txt";
else $lPHORM_TMPL = "$ph_tpd/$lPHORM_TMPL";
$ph_Message = "";
$ph_Headers = "";
$ph_NonHeader = "";
$lPHORM_TO = "";
$lPHORM_FROM = "";
$lPHORM_SUBJECT = "";
$lPHORM_HEADERS = "";
$ph_TemplateHeaders = false;
if (ereg("(.+) +\+h$", $lPHORM_TMPL, $ph_regs)) {
$lPHORM_TMPL = trim($ph_regs[1]);
$ph_TemplateHeaders = true;
}
if ($ph_debug8) echo "Mail Template <B>$lPHORM_TMPL</B><BR>";
if (!$ph_Template = #implode("", file($lPHORM_TMPL))) {
$ph_Alerts['005'] = ph_Message("A005");
if ($php_errormsg) $ph_Alerts['005P'] = "%%%: $php_errormsg";
continue;
}
This is because your are using strlen() to count an array while it is supposed to use when you need to count strings. Your error is quite clear.
If you need to count an array or more just use count or sizeof functions.

Define value edit by another php file

I have 2 questions
1.) how to write update_defile($array_value){...} function?
define_file.php
<?php
define("FIST_NAME", "something1");
define("LAST_NAME", "something2");
define("ADDRESS", "something3");
?>
"something" is not a constant value that can be change every method Call(update_defile($array_value)
value set
$array_value = ("FIST_NAMe" => "duleep", "LAST_NAME" => "dissnayaka", "AGE" => "28" );
after call method(update_defile($array_value){.....}) "define_file.php"
file want to be look like bellow
<?php
define("FIST_NAME", "duleep");
define("LAST_NAME", "dissnayaka");
define("ADDRESS", "something3");
define("AGE", "28");
?>
2).
My datbase is Oracle. I already saved configuration value in the data base but frequently use these configuration value for my application. So i get value form database and save in the define_file.php as increase performance(down rate database call) but I'm not sure i can increase performance keep configuration value in the PHP file please explain. what is the best way increase performance my application and other alternative solutions welcome.
Why cant u use session to store such values , then u can access and modify from anywhere
in the script.
<?php
session_start();
$_SESSION["FIST_NAME"]= "something1";
$_SESSION["LAST_NAME"]= "something2";
$_SESSION["ADDRESS"]= "something3";
?>
public function update($form_config_arr)
{
if( (is_readable($config_file_path)) && is_writable($config_file_path))
{
if(!$config_old_file_content = file_get_contents($config_file_path))
{
throw new Exception('Unable to open file!');
}
$i = 0;
$config_old_arr = array();
$config_new_arr = array();
foreach ($form_config_arr as $constant => $value){
$config_old_line = $this->getLine($constant);
$config_old_arr[$i] = $config_old_line;
if(($value == 'true') || ($value == 'false')){
$config_new_arr[$i] = "define ( '$constant', $value );\n";
}else{
$config_new_arr[$i] = "define ( '$constant', '$value' );\n";
}
$i++;
}
$config_new_file_content = str_replace($config_old_arr, $config_new_arr, $config_old_file_content);
$new_content_file_write = file_put_contents($config_file_path, $config_new_file_content);
foreach ($config_new_arr as $constant => $value)
{
echo $value.'<br/>';
}
return true;
}else{
throw new Exception('Access denied for '.$config_file_path);
return false;
}
}
/**
*
* #param string $constant
* #return string
*/
private function getLine($constant)
{
$match_line = '';
$config_file = fopen($config_file_path, "r");
if($config_file)
{
//Output a line of the file until the end is reached
$i = 0;
while(!feof($config_file))
{
$i++;
$config_old_line = fgets($config_file);
$pos = strpos($config_old_line, $constant);
if( $pos !== false )
{
$match_line= $config_old_line;
}
}
fclose($config_file);
return $match_line;
}else{
throw new Exception('Unable to open file!');
}
}
What you are trying to do is edit a file.
Simply create another php script: updater.php
It should poll the database, fetch the values and update the values in define_file.php
Look for php file handling functions here: http://www.w3schools.com/php/php_file.asp

Categories