Discuss coding issues, and scripts related to PHP and MySQL.

  1. » PloMar
    How can I replace the new lines with a single space in a string in PHP?
    For example I have this $str variable with a string content with...
    Last post » Admin
    You can use str_replace() with PHP_EOL.
    $str  =  'First line Second row Other line ...' ;...

  2. » PloMar
    What's the best way to delete a directory with all its files in it?
    I'm using rmdir(PATH . '/' . $value); to delete a folder, however, if there are...
    Last post » Admin
    This function will allow you to delete any folder (as long as it's writable) and it's files and subdirectories:
    function  deleteDir ( $path...

  3. » PloMar
    I try to parse a JSON file using PHP. The data in this JSON is in a two-dimensional object, like this:
    "John": {...
    Last post » Admin
    To iterate over a multidimensional array, you can use RecursiveArrayIterator.
    $string  =  file_get_contents ( '/dir/test.json' );...

  4. » MarPlo
    How can I detect which request type was used (GET, POST, PUT or DELETE) in a php script?
    Last post » Admin
    Simple use $_SERVER .
    $request  =  $_SERVER [ 'REQUEST_METHOD' ]; echo $request ; if( $request  ===   'GET' )   {    // ... }...

  5. » MarPlo
    I have a PHP array as follows:
    $aray  = array( 312 ,  'string' ,  1599 ,  'other val' ); $del_val  =  1599...
    Last post » Admin
    You can use array_search() and unset(), like in this example:
    $aray  = array( 312 ,  'string' ,  1599 ,  'other val'...

  6. » MarPlo
    What is the best way to validate an email address in PHP?
    I currently use preg_match() with a RegExp:
    $email  = ...
    Last post » Admin
    The easiest and safest way to check whether an email address is well-formed is to use the filter_var() function:
    $email  = ...

  7. » MarPlo
    I am trying to select all rows from a table where column contains a character from a list: Something like:
    SELECT * FROM table WHERE column...
    Last post » Admin
    Try with REGEXP, and escape everything with backslash. Result: .
    SELECT * FROM table WHERE column REGEXP '[\£\$\%\^\&\*\?]'

  8. » MarPlo
    I want to search in mysql database with columns containing some specified names and the column has values like:
    "Varsus GmbH (default)"...
    Last post » Admin
    Try this Select:
    suppliername = 'Varsus'
    OR (
    suppliername LIKE '%Varsus%'
    AND suppliername LIKE '%default%'
    - The...

  9. » MarPlo
    In "login.php" it is set a session ID for the logged user. Something simple:

    // check login data received from...
    Last post » Admin
    Store a timestamp in the session.
    In "login.php", register a session with the expired timestamp (when you want to expire that session) in...

  10. » MarPlo
    I have two array one is 1 to 8 number of cards and four colors of cards.
    $no  = array( 1 ,  2 ,  3 ,  4 ,  5...
    Last post » Admin
    Use nested foreach() loops:
    $no  =  array ( 1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ); $color  =  array ( 'K' , 'L' , 'F' , 'C' ); $result  =...

  11. » Marius
    I am trying to write a Regular Expression that makes sure the characters are all Alpha Numeric, and that accepts other characters as well. I would...
    Last post » MarPlo
    Your regex ^ *$ won't match the accented characters. That is \w would match only the English alphabets, digits 0-9 plus the underscore symbol.

  12. » Marius
    I facing a problem about a select query in MySQL.
    I have table user, It contains id_user, username and user_time. And user_time is a Date-Time for...
    Last post » Admin
    YEAR returns a number like 2015. That will never be equal to a time. You need to test whether the year of "user_time" is the same as...

  13. » Marius
    I have a table with the following columns: id | url | title | company .
    Now, I need to delete rows having same url and title.
    I want to know if this...
    Last post » Admin
    An easy way to do this is to add a UNIQUE index on the columns with duplicate values, using the ALTER statement.
    When you write the ALTER statement,...

  14. » Marius
    I have SQL a query like this:
    SELECT c.type, SUM( u.account )
    FROM contracts c, u_data u
    WHERE c.user_id =
    GROUP BY c.type;...
    Last post » Admin
    You just have the wrong order for GROUP BY and ORDER BY.
    ORDER BY should be added after GROUP BY.
    GROUP BY c.type;
    ORDER BY...

  15. » Marius
    I have an array in php:
    $arr = array(3, 'value 1', 'other value');
    And a mysql table with these columns: id | name | data
    I want to store...
    Last post » Admin
    You can store an array in a database into a string with JSON format.
    Use json_encode() when you want to insert the array. For example:
    $arr  =...

  16. » Marius
    I'm trying to SELECT from a PHP array inside a mysql query.
    $sql = "SELECT * FROM table WHERE id = $ids ";
    $ids has been created by...
    Last post » Admin
    Try with IN with implode of the array $id like:
    $sql = "SELECT * FROM table WHERE id IN (". implode(',', $ids) .")";

  17. » Marius
    I've written following code to compare a date in a variable with today's date. If the date in a variable is greater than today's date the error...
    Last post » Admin
    You may be meaning 11th Decemeber with 12-11-2014, but strtotime isn't seeing it that way.
    If you need to use strtotime() then change the input...

  18. » Marius
    I have two tables: A and B. I need to compare their columns to get values of one table that are not in other table.
    Table A columns : ID,...
    Last post » Admin
    You can use not in :
    SELECT *
    FROM table_A
    WHERE table_id NOT IN (SELECT id from table_B)

  19. » Marius
    I have a subscription system in my website. When the users subscribe, their start date and time is added in mysql table using now() function.
    I also...
    Last post » Admin
    The date_add() function is what you're looking for:
    DATE_ADD(date,INTERVAL expr unit)
    INSERT INTO users
    (username, registration_date,...

  20. » Marius
    While executing an INSERT statement with many rows, I want to skip duplicate entries that would otherwise cause failure. After some research, my...
    Last post » Admin
    If you just want to skip rows with duplicate values, and no update needed, I think that is proper to use INSERT IGNORE .
    If you use INSERT...

  21. » Marius
    I have this Select query, I want to select one row from mysql table, where id = $id, and has "x_name" or "y_name" 'yes'....
    Last post » Admin
    The problem is that you didn't separate the conditions with the OR from the id=$id, so to select just the row which has that $id AND 'yes' in any...

  22. » Marius
    I have this array, and I want to return it as JSON from a PHP script.
    $ar = array(
    'k0'=> array('a', 'b', 'c'),
    'k1'=> array(1, 2, 3)
    Last post » MarPlo
    Use json_encode() to encode the data and set the content-type with header('Content-type: application/json');
    Anyway, it works fine if you just...

  23. » Marius
    I have two arrays $a1 and $a2 with various elements.
    How can I check which item from $a2 is in $a1? Is there any function, or I have to traverse...
    Last post » Admin
    The array_intersect($a2, $a1) function returns the elements of $a2 which are found in $a1.
    Also, with the in_array() function you can check if a...

  24. » Marius
    I have 2 tables with same column names in my database which are supposed to provide content in 2 languages.
    This is how I query for one table (this...
    Last post » MarPlo
    The mysql is confused when you select columns with same name. The solution is to use Alias ( AS ).
    $sql = "SELECT en.col1 AS c1en,...

  25. » Marius
    Does anyone has a php code to display the elapsed time?
    Like: one hour ago, 1 day and 3 hours ago, ...
    Thank you.
    Last post » Admin
    You can use the timeElapsed() function, from this example:
    function  timeElapsed ( $t1 ,  $t2  =  null ) { /*...