I need some specific help in php mysql issue

Discussion in 'PHP' started by hope2life, Aug 16, 2012.

  1. #1
    Hello,

    cat1 is a field in my mysql database table

    I want to check first if cat1 is single word. If its not a single word I want to add underscore(_) between each of the words and then retrieve it on the site and print.

    Please can anyone help with php code regarding this?
     
    hope2life, Aug 16, 2012 IP
  2. alamlinks

    alamlinks Well-Known Member

    Messages:
    992
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    140
    #2
    I can help you in case of asp.net using sql server, but sorry for php
     
    alamlinks, Aug 16, 2012 IP
  3. SitesTen

    SitesTen Active Member

    Messages:
    47
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    75
    #3
    As I understand you actually want to replace the spaces with underscores like e.g. "cat" will remain "cat" but "black cat" will become "black_cat". If this is the case you can do it both on mysql level using REPLACE and on PHP level using str_replace.
     
    SitesTen, Aug 16, 2012 IP
  4. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #4
    
    
    <?php
    
    // Retrieve your DB data here into $dbField var
    // $dbField = ...
    
    $dbField = trim($dbField);
    $dbField = str_replace(' ', '_', $dbField);
    print $dbField;
    
    ?>
    
    
    PHP:
    You really shouldn't work through gaps in your programming knowledge. Buy a second-hand book on PHP and SQL for a few dollars on Amazon.
     
    BRUm, Aug 18, 2012 IP