#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    Check if column within MySQL database contains a string.


    Bizzet, on 20 Mar 2013 - 09:08, said:

    Okay, so basically what I want to is create an API using PHP which will allow my program to check if a column within a table contains a certain string.

    For example,
    Say I enter

    /api.php?word="ANY WORD"

    The script would echo true or false if the database contained that word or not.

    I am using a MySQL database and I have no idea how to go about this.

    =======================================================================


    PHP Code:
       <?php


        
    try {
     
    $username '******';
     
    $password '******';
          
    $conn = new PDO('mysql:host=******;dbname=**********'$username$password);
          
    $conn->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
       
        } catch(
    PDOException $e) {
        
        echo 
    'ERROR: ' $e->getMessage();
        
        }


          
    $stmt $conn->prepare('SELECT COUNT(*) FROM wordlist WHERE words LIKE :aword');
          
    $stmt->execute(array('aword' => "%{$_GET['word']}%"));
          return (
    $stmt->fetchColumn() != 0);
        
    ?>


    How exactly would I make it echo true or false if the database contains the string though?

    Please try to post code, its hard for me to translate text to PHP...

    I barley know PHP....
  2. #2
  3. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
    PHP Code:
    if ($whatever) {
        echo 
    "true";
    } else {
        echo 
    "false";

    Or ?: operator if you want.
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by Bizzet
    Bizzet, on 20 Mar 2013 - 09:08, said:
    When you copy and paste, at least remove the leftovers from the last forum and make sure to include all content. Where's the PDO exception from the other forums? Did you want us to guess?

    Since there's obviously something wrong with your PDO object, that's what you need to fix first.

    The code you gave us can't be the exact code -- or you're getting the error in some other file. So what is your actual code?

    C'mon, if you want us to help you, at least don't throw obstacles in our way.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by Jacques1
    When you C&P, at least remove the leftovers from the last forum and make sure to include all content. Where's the PDO exception from the other forums? Did you want us to guess?

    Since there's obviously something wrong with your PDO object, that's what you need to fix first.

    The code you gave us can't be the exact code -- or you're getting the error in some other file. So what is your actual code?

    C'mon, if you want us to help you, at least don't throw obstacles in our way.
    I am not getting any errors when running that code. I don't know what you mean the "PDO exception from other forums".

    I do not know what is wrong with my code and I do not know what else information you want. If you need more, please be more specific.

    This code is different from other forums...
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by Bizzet
    I am not getting any errors when running that code. I don't know what you mean the "PDO exception from other forums".
    I'm talking about the copypasta error with $pdo and $conn, but it looks like you've fixed it in the phpfreaks forum.

    By the way, be fair and tell them that you're using this forum now.



    Originally Posted by Bizzet
    I do not know what is wrong with my code
    I don't know that either. I didn't say that it's wrong, you did. So what's your reason for that? The code works fine on my server.



    Originally Posted by Bizzet
    and I do not know what else information you want.
    How about the result of code? You say that it's "wrong", so I guess you didn't get the expected result. What did you get instead?
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by Jacques1
    I'm talking about the copypasta error with $pdo and $conn, but it looks like you've fixed it in the phpfreaks forum.

    By the way, be fair and tell them that you're using this forum now.
    No need to do that, they are unhelpful anyways...
    Originally Posted by Jacques1



    I don't know that either. I didn't say that it's wrong, you did. So what's your reason for that? The code works fine on my server.
    Did you even read the original post? Let me copy and paste for you.

    Originally Posted by Bizzet
    How exactly would I make it echo true or false if the database contains the string though?
    Originally Posted by Jacques1


    How about the result of code? You say that it's "wrong", so I guess you didn't get the expected result. What did you get instead?
    I get no result, but I want a result as stated in the original post...


    SORRY FOR STATING THE TRUTH!

    Comments on this post

    • Jacques1 disagrees : Protip: Don't be an *sshole when you're waiting for help.
  12. #7
  13. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
    Quit it. Both of you. I hate having to break up arguments and today's one of those days I'd rather just hand out warnings.


    Bizzet, I made a reply earlier. Is that what you are looking for?
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by requinix
    PHP Code:
    if ($whatever) {
        echo 
    "true";
    } else {
        echo 
    "false";

    Or ?: operator if you want.
    Sorry for not getting back to you.

    Yes that is exactly what I want, I just do not know how to apply it to my code..
  16. #9
  17. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
    You've got that return in there already. That seems like the right place. So instead of making it return a boolean, output a string.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0
    you simply need to use is_string() function to check whether that column contains string or not.

IMN logo majestic logo threadwatch logo seochat tools logo