Displaying all posts with the tag 'PHP'
When developing applications its inevitable at some point you'll be checking to see if something exists and if it doesn't; creating it in the database.
The way you've most likely been doing this is running a select query to see if it exists, grabbing the ID if it does exist then executing an UPDATE or noting it doesn't exist then executing an INSERT. It's time consuming and adds more unnecessary code you feel you probably don't need.
We have an answer!
There is a way to run an update within MySQL and make a note as to how many records it has updated, you can check to see if this is zero then run the insert code if need be, simple eh? Check out my example below and start counting up those saved seconds for another tea and biscuit break!
$result = mysql_query("UPDATE table SET notes = '' WHERE date = '$date'");
if (mysql_affected_rows() == 0) {
$result = mysql_query("insert into table () values ();");
}
In some projects you sometimes need to print out the alphabet either for a search facility or maybe because you like letters, either way we found a solution today which we thought was worthy of sharing!
In PHP, there are more than 700 built-in functions. The one function im going to be sharing with you today is called "range". The range() function creates an array containing a range of elements that can be returned from low to high.
This function is mostly used with numbers like this: $number = range(0,3); but it can also be used with letters $letters = range('a','e'); Take a look at our examples and outputs below.
$number = range(0,3); print_r($number);
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
$letters = range('a','d'); print_r($letters);
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
If you've got a tip, correction or comment regarding our coding tips, get in touch from the contact page!