As it is deprecated, I will need to change my all code which contain mysql_result. Since this may take long time, I am looking for a simplest possible option.
Normally I would use this:
$varname=mysql_result($result_query,0,"settingName");
now I found a function which may be a replacement:
$varname=mysqli_fetch_row($result_query)[0];
But in this case, I am unable to see which setting I am getting from database. Also, when I change an order in database, I will need to update all numbers.
How can use a function which I can use a similar syntax?
Try:
$varname = mysqli_fetch_assoc($result_query)['settingName'];
Related
Is it possible to add a removed/deprecated function to PHP5? Like session_is_registered, ereg, etc.
[update] solved for session_is_registered:
<?php
function session_is_registered($name) {
return isset($_SESSION[$name]);
}
thanks.
Of course you can do it by modifying and recompiling the PHP source code, however the first question you have to answer is Do I really need to this or I might be better to go for my IDE's find-and-replace function?
If there is a real need for this -- for whatever reason, maybe you can redefine those functions. I haven't test it yet, as I agree with others that functions and features get removed or deprecated for a good and mostly important reasons, so I'm not sure if it does work in a situation that the function is removed or depreciated, but you can try to redefine them either using runkit_function_redefine or
override_function.
In that case you have to simulate the functionality again -- probably with their good-to-go replacements, so again think twice before start doing that.
Is $row a predefined variable in php (or does it have some other built-in functionality)? I have several times seen it used without prior reference in the code, but haven't found any documentation or explanation about it yet.
This is just a convention, you can use any name you want, however is good to use something simple, clear that make sense...
No, there is no such a predefined variable, you've just seen a variable declaration/initialization.
Just a simple question. I have a contact form stored in a function because it's just easier to call it on the pages I want it to have.
Now to extend usability, I want to search for {contactform} using str_replace.
Example:
function contactform(){
// bunch of inputs
}
$wysiwyg = str_replace('{contactform}', contactform(), $wysiwyg);
So basically, if {contactform} is found. Replace it with the output of contactform.
Now I know that I can run the function before the replace and store its output in a variable, and then replace it with that same variable. But I'm interested to know if there is a better method than the one I have in mind.
Thanks
To answer your question, you could use PCRE and preg_replace_callback and then either modify your contactform() function or create a wrapper that accepts the matches.
I think your idea of running the function once and storing it in a variable makes more sense though.
Your method is fine, I would set it as a $var if you are planning to use the contents of contactform() more than once.
It might pay to use http://php.net/strpos to check if {contact_form} exists before running the str_replace function.
You could try both ways, and if your server support it, benchmark:
<?php echo 'Memory Usage: '. (!function_exists('memory_get_usage') ? '0' : round(memory_get_usage()/1024/1024, 2)) .'MB'; ?>
you may want to have a look at php's call_user_func() more information here http://php.net/call_user_func
$wysiwyg = 'Some string and {contactform}';
$find = '{contactform}';
strpos($wysiwyg, $find) ? call_user_func($find) : '';
Yes, there is: Write one yourself. (Unless there already is one, which is always hard to be sure in PHP; see my next point.)
Ah, there it is: preg_replace_callback(). Of course, it's one of the three regex libraries and as such, does not do simple string manipulation.
Anyway, my point is: Do not follow PHP's [non-]design guidelines. Write your own multibyte-safe string substitution function with a callback, and do not use call_user_func().
How to output functions that are stored in mysql database? When I try it the output looks like this:
<-- function();-->
What have you stored in your table:
function names? or complete functions?
if function names you can do:
$functionname()
if you have complete functions you probably could eval() it.
Please be very very careful with eval().
Why do you store functions in your mysql table? It just looks weird to me.
And perhaps there is a better solution.
Use the eval() function.
It evaluates a string as PHP code
What is the point of replace function in PHP memcache if you can just use set? Even if there is a variable, set automatically replaces it, right?
Can you give me an example where it's better to use replace instead of set?
Thanks.
According to PHP.net:
Memcached::replace() is similar to Memcached::set(), but the operation fails if the key does not exist on the server.
Following on from ZoFreX's answer if you look at the comments here:
http://www.php.net/manual/en/memcache.set.php
You will see the following:
Using set more than once for the same key seems to have unexpected results - it does not behave as a "replace," but instead seems to "set" more than one value for the same key. "get" may return any of the values.
This was tested on a multiple-server setup - behaviour may be different if you only have one server.
So really and truly replace() will look for an existing key first, and then replace it (if it exists), whereas set() will just add the key. I imagine it's always best to use replace() first considering it returns FALSE if the key is not found, in which case you'd use add() rather than set() since you know for sure the key doesn't exist. This ensures that you won't have any unintended mishaps. So you're code could be something like:
$replace = Memcached::replace($key, $var);
if ( ! $replace)
{
$set = Memcached::add($key, $var);
}
Generally, replace should be used if the key is likely to be used frequently. Set/add/etc creates a brand new entry, and can lead to fragmentation and a lot of cleanup. Replace reuses the memory already allocated (if possible), and can be more stable and efficient. If it fails, using add/set will still work.
nope that's entirely wrong.
If you want to check and set a value you must use GETS + CAS.