PHP - MySQL


  1. » Marius
    Hello
    I want to make a Select SQL query that sorts ascending by a column, but the rows with NULL values to come last.
    This is the query I use (but...
    Last post » Admin
    Hello
    You could use the ISNULL() function:
    $sql =  SELECT * FROM mp_tab WHERE col < 50 ORDER BY (ISNULL(col) OR col =''), col ASC ; 
    Or, in the...


  2. » PloMar
    Hello,
    Let say I have records into a MySQL table with IDs: 3, 5, 8, 9, 13; and I want to be able to go from one to another by navigation via...
    Last post » MarPlo
    Hi,
    Try this SQL code that combines two sql statements into one.
    SELECT * FROM table_name
    WHERE (
    id = IFNULL((SELECT MIN(id) FROM table_name...


  3. » PloMar
    How can I add a method to a stdClass object?
    For example I try this code:
    $ob = new stdClass;
    $ob->prop = 'val';...
    Last post » Admin
    You cannot dynamically add a method to the stdClass and execute it in the normal way.
    But you can assign that object method to a variable, then...


  4. » PloMar
    I have the following object in php:
    $ob = new stdClass;
    $ob->name = 'some-name';
    $ob->email = 'email-address';
    $ob->site = '
    How can I...
    Last post » Admin
    Hi,
    Just use the: (array) $objectName construction.
    $ob = new stdClass;
    $ob->name = 'some-name';
    $ob->email = 'email-address';...


  5. » PloMar
    Hello,
    I have a script that the user can upload and delete images on server. The issue I am running into is that when the user changes his image on...
    Last post » Admin
    Hi,
    The simpliest way to force a refresh /reload of that image after upload is to add an unique ?hash to the URL /SRC adress (src=...


  6. » Marius
    Hi,
    I have this php exercise:
    - Calculate the final sum of the digits of a number, till get a number with one digit.
    For example:
    $nr = 458;
    //...
    Last post » sandy
    Another example with a recursive function:
    <?php
    function add($nr) {
      $nr = array_sum(str_split($nr));
      if(strlen($nr) == 1) return $nr;...


  7. » PloMar
    Hello,
    I have the following method to update data in MySQL table.
    public function update($table, $rows){
      foreach($rows as $k => $v) {...
    Last post » Admin
    Hi,
    Try this code:
    public function update($table, $rows){
      $set = [];
      foreach($rows as $k => $v) {
        $set[] =  $k='$v' ;
      }
      $sql = ...


  8. » Marius
    Hi
    I have a table in mysql database, with 3 columns: id | title | description .
    The id column is INT AUTO_INCREMENT .
    Does anyone know how to...
    Last post » sandy
    Hi,
    If you're using PDO, use PDO::lastInsertId();// To Display the last Inserted value
    If you're using MySql, use mysql_insert_id();// To Display...


  9. » PloMar
    Hi,
    I have the following code that uses DOMDocument to get some data from a html string in PHP.
    // string with HTML content...
    Last post » MarPlo
    Hi,
    The nodeValue attribute gives you the combined text of all the child nodes.
    To get the text value of the first child text node only, use:...


  10. » PloMar
    I wrote a PHP code to insert some data in to MySQL database. I want to display a message indicating whether the records are successfully added to the...
    Last post » sandy
    try {
          $db_user = 'root';
              $db_pass = 'pass'; 
              $db = new PDO( 'mysql:host=localhost;dbname=the_db', $db_user, $db_pass );...


  11. » PloMar
    Hello,
    I have the following code that saves on the server a png image generated with php.
    In that image I add a text (using imagettftext()), but...
    Last post » MarPlo
    Hi,
    You can use the imagettfbbox() function, it returns an array with 8 elements representing four points making the bounding box of the text....


  12. » PloMar
    Hello,
    How can I save the content of the current php page into a html file, only after a form is submitted?
    For example, I have the following code...
    Last post » MarPlo
    If you want to save the page content after a form is submitted, another way is to add the page code into a hidden field in that form, with JavaScript...


  13. » PloMar
    I have the following Select in MySQL:
    $sql = SELECT * FROM table_1 WHERE rank = 2 AND id IN(2, 5, 8) ;
    But the IDs (2, 5, 8, ...) must be from a...
    Last post » MarPlo
    You can use multiple Select statements in the same SQL query.
    Try this query, using a sub-select that selects the IDs.
    $sql = SELECT * FROM table_1...


  14. » PloMar
    I have two text fields, one for the date in 08/21/2012 format and a second field containing the time in the format 09:41am .
    How can I end up...
    Last post » MarPlo
    On the database side, you can use:
    - STR_TO_DATE() to convert into a database friendly format.
    SELECT STR_TO_DATE(CONCAT('08/21/2012', '09:41am'),...


  15. » PloMar
    I want to insert a DateTime object in database (created in PHP) where the column type is DateTime. How can I do?
    I am using this code:
    $cdate = new...
    Last post » MarPlo
    Use the Y-m-d H:i:s date-time format in PHP, it is the format of the DateTime column in MySQL.
    $cdate = new DateTime('now');...


  16. » PloMar
    Hello
    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.
    Example:
    $str = 'First line
    Second row
    Other line ...';
    $str = str_replace(PHP_EOL, ' ', $str);
    echo $str; ...


  17. » 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) {...


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


  19. » 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 ;
    echo $request;

    if($request === 'GET') {
      // ...
    }
    else if($request === 'POST') {
      // ...
    }...


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


  21. » MarPlo
    What is the best way to validate an email address in PHP?
    I currently use preg_match() with a RegExp:
    $email = 'some_name@domain.com';...
    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 = 'some_name@domain.com';...


  22. » 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: .
    SQL:
    SELECT * FROM table WHERE column REGEXP ' '


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


  24. » MarPlo
    In login.php it is set a session ID for the logged user. Something simple:
    <?php
    session_start();

    // check login data received from a form .....
    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 the same...


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

Forum permissions

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum