PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 20th, 2013, 03:44 PM
Bizzet Bizzet is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 4 Bizzet New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 20 m 33 sec
Reputation Power: 0
PHP-DB - 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....

Reply With Quote
  #2  
Old March 20th, 2013, 04:08 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,701 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 5 h 23 m 54 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
PHP Code:
if ($whatever) {
    echo 
"true";
} else {
    echo 
"false";


Or ?: operator if you want.

Reply With Quote
  #3  
Old March 20th, 2013, 04:12 PM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,867 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 22 h 33 m 7 sec
Reputation Power: 813
Quote:
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.

Reply With Quote
  #4  
Old March 20th, 2013, 04:26 PM
Bizzet Bizzet is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 4 Bizzet New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 20 m 33 sec
Reputation Power: 0
Quote:
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...

Reply With Quote
  #5  
Old March 20th, 2013, 04:43 PM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,867 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 22 h 33 m 7 sec
Reputation Power: 813
Quote:
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.



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



Quote:
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?

Reply With Quote
  #6  
Old March 22nd, 2013, 01:24 PM
Bizzet Bizzet is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 4 Bizzet New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 20 m 33 sec
Reputation Power: 0
Quote:
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...
Quote:
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.

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


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

Reply With Quote
  #7  
Old March 22nd, 2013, 02:09 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,701 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 5 h 23 m 54 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
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?

Reply With Quote
  #8  
Old March 22nd, 2013, 11:24 PM
Bizzet Bizzet is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 4 Bizzet New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 20 m 33 sec
Reputation Power: 0
Quote:
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..

Reply With Quote
  #9  
Old March 23rd, 2013, 12:23 AM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,701 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 5 h 23 m 54 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
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.

Reply With Quote
  #10  
Old March 23rd, 2013, 06:03 AM
satsou19 satsou19 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 satsou19 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 19 m 46 sec
Warnings Level: 5
Reputation Power: 0
you simply need to use is_string() function to check whether that column contains string or not.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP-DB - Check if column within MySQL database contains a string.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap