I have written a function that is supposed to be able to generate a number of input fields which will be commonly used in a private web application.
Details of these fields are stored in the mysql database. There is a varchar field which holds the name of the function that generates the related text for a dropdown, as well as the associative key to retrieve the right string from the function's result.
The variable function name does not call the function. It worked in an earlier simplified iteration using only mysqli_fetch_row rather than mysqli_fetch_assoc.
I'd like to get it working with associative keys. (I can see no reason why associative keys could be the problem).
I've tried various forms of eval and exec. I can get a lite version to work with mysqli_fetch_row.
I have followed instructions here: Store function name in database and then execute it
I haven't been able to find anything else that directly adresses the problem.
$funcResult = $func($fldNmValue)[$funcKey];
doesnt't work
$funcResult = rank($fldNmValue)[$funcKey];
works
'rank' is one of the function names in the database
it is pulled from the database by:
$query = "SELECT fieldFunction,fieldFunctionKey FROM fieldType WHERE fieldTypeId='$fldTypeId'";
$result = db_query($query);
$data = db_fetch_assoc($result);
$func = $data['fieldFunction'];
I have verified that $func contains valid function names like 'rank' or 'castDetails'.
printing "$func($fldNmValue)" gives, for example:
castDetails(2)
as expected.
PHP refuses to execute $funcResult = $func($fldNmValue)[$funcKey]; and simply exits.
I had expected it to function like it did when using mysqli_fetch_rows.
Related
Why is the following code failing to store the entire strings from the "name" column into my output array? The code did work at some point.
$contacts = "";
$sql = "SELECT * FROM s_issue.t_contact WHERE orgid='$orgid' ORDER BY id";
$results = db_query($db, $sql, "psql");
while($row = pg_fetch_assoc($results)){
$contacts[$row["id"]] = $row["name"];
}
return $contacts;
The database column id contains integers, and the name column contains strings. The expected output was a list of contact names, but in my initial trials, I was getting a blank list.
I was asked to look at some very old legacy code. It was non-obvious as to the recency of the upgrade to PHP7, and when I started looking I wasn't even sure it was PHP7.
In case you come across some legacy code like this, and find yourself wondering why the output array does not contain what you expect, have you recently upgraded to PHP7? Because that's the cause.
The initial line in PHP7 now coercively (default) types $contacts as a string. Further, a string can be indexed numerically to access each character in that string. That is:
$foo = "abcde";
echo $foo[1]; // outputs 'b'
So when $row["id"] is numeric, then $contacts[$row["id"]] becomes a string with a numeric index. Hence, only one character can be stored at that location, and it is the first character of the string $row["name"].
The proper fix is to either remove the wrongly-typed initialization line of $contacts = "" completely, or correctly initialize it to the type actually desired, an array:
$contacts = array();
The code in question worked fine before PHP7. It will fail in mysterious ways in PHP7.
While retrieving values from array without index in PHP I am getting nothing.
From my database value is stored like ["SCHOOL"] and I want to get just SCHOOL from this.
It's not treated as array instead it's treated as a string.
What will be the solution if want to treat this as array and get value of that array.
My Code is as follows :
$stream_arr = $res_tbl['streams'];
which gives result as ["SCHOOL"]
and I want just SCHOOL from this.
I am using code as $stream = $stream_arr[0];
and I get '[' this as result.
If I assign manual value to $stream_arr = ["SCHOOL"],
above code works and returns SCHOOL as expected.
But for results from my database is not working where result is same as the one I manually assigned.
To eliminate braces from the string you can use below code.
$stream_arr = '["SCHOOL"]';
$temp=explode('"',$stream_arr);
$val=$temp[1];
echo $val; //it will dispaly 'SCHOOL'.
But check the insert query(from the place you store data into db) that why it is storing with braces? if it is done by you as required then proceed else first fix that issue instead of eliminating braces in php code.
I simply want to know how to access array elements retrieved from a database. I have the following code to get the names of each item in my database.
$plat_options = $this->db->get('tblplatform_options')->select('name')->result();
How do I go about accessing the name from the array $plat_options? Typically I would do $plat_options[0] for the first element in C#, how is this done in php/codeigniter?
In PHP/Codeigniter, can be done in the same way:
$plat_options[0] //if you have this element, usually is better to check if exists.
You can retrieve all the elements with foreach($plat_options as $option){...}
You can cast to object: https://www.kathirvel.com/php-convert-or-cast-array-to-object-object-to-array/
Or use a Codeigniter Helper (assuming you are using CI3): http://www.codeigniter.com/user_guide/helpers/array_helper.html
I recomend to know which is your array format and retrieve that way (if you don't know, you can do a: var_dump($plat_options) ) to know if is an associative array.
You can use the result_array() function:
$data = $plat_options->result_array();
echo($data[0]['name']);
or:
$data = array_shift($q->result_array());
echo($data['name']);
I extracted this last part from: Codeigniter $this->db->get(), how do I return values for a specific row? that you could check too.
If you don't know a lof of CI, the best you can do is do a simple tutorial to understand how the data + ActiveRecord works.
Hope it helps!
I want to insert a WKT format polygon to the PostGIS. It works with the PostGIS SQL. Now I want to use PHP call this function but it fails. It should be something wrong with the reference to the variable. My code is as following:
<?php
$data = A string format data;
$con="host=localhost dbname=database user=postgres password=great";
$dbcon= pg_connect($con);
$sql="INSERT INTO polygons(geometry) VALUES (ST_GeomFromText(('$data'))";
$result= pg_query($dbcon,$pgsql);
?>
There should be something wrong with the $data, it doesn't refer to my data. But I don't know how to fix it.
What every PHP user have to understand, that it is not into pg_query variable being inserted but into regular PHP string.
And thus it is very simple to verify if it was inserted or not - by just echoing the string out:
echo $sql;
This way you can see if it was inserted or not.
if it was - the problem somewhere else.
if won't - you have to verify variable itself.
I have firePHP so i know exactly what the variables are, but I can't figure out why this code doesn't change it.
I receive from a mySQL call $query (which if returned produces [{"type":"2"}])
I have 4 potential types, and things can be multiple types (i.e. [{"type":"1"},{"type":"2"}])
Now I want to read this data and run various other functions based on the type it has, that is: if it's only type 2, call function TWO, if it's type 1 and 2 call function ONE and function TWO. I thought this would be easiest if i moved all the data into another array.
Here is the code I currently have:
$result = array('message'=>false, 'money'=>false, 'glasses'=>false, 'exclamation'=>false);
if (in_array('1',$query)) {$result['message'] = true;}
if (in_array('2',$query)) {$result['money'] = true;}
if (in_array('3',$query)) {$result['glasses']=true;}
if (in_array('4',$query)) {$result['exclamation']=true;}
echo json_encode($result);
This however does not update the $result array (as I can tell all of the values of $message are false in firePHP.... Thus I assume something is wrong with my if statements, but what?
I´m not sure about the value of $query, but if it is something like:
array [0 => '{"type":"2"}']
You would have to use:
in_array('{"type":"2"}',$query)
as that is the value of your variable.
Is it because the results returned in $query are arrays of arrays, and thus in_array is only searching at the top level and not sub-levels? It seems like what you want is to recursively search $query.