Jump to content

Check if column within MySQL database contains a string.


Bizzet

Recommended Posts

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 
 
    www.mywebsite.com/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


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


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



 

 

 
What did I do wrong? I get this error
 
     Fatal error: Call to a member function prepare() on a non-object in ***** on line 11
 
I barley know PHP....
Link to comment
Share on other sites

How corny. Might be rice to learn it.

 

$pdo is not defined. The error tells you. Look at your code carefully, what is $pdo?

 

 

 

<?php


try {
$username = '******';
$password = '*****';
    $conn = new PDO('mysql:host=5;dbname=*****', $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::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);
?>
 

 

Sorry for the obvious mistake...

 

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

Edited by Bizzet
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.