Jump to content


Photo

Check if column within MySQL database contains a string.


  • Please log in to reply
3 replies to this topic

#1 Bizzet

Bizzet

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 20 March 2013 - 11:01 AM

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....


#2 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 20 March 2013 - 11:13 AM

I barley know PHP....

How corny. Might be rice to learn it.

$pdo is not defined. The error tells you. Look at your code carefully, what is $pdo?
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#3 Bizzet

Bizzet

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 20 March 2013 - 11:53 AM

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, 20 March 2013 - 11:54 AM.


#4 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 20 March 2013 - 01:00 PM

I think you need to tell it what column to fetch. Give you count an alias (like count_words) then you can fetch that column.
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com