Page 2 of 2 FirstFirst 12
Results 11 to 20 of 20

Thread: PHP and mySQL

  1. #11
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Cheers for that.

    Is it possible to also narrow down the search if a field contains something?

    e.g. there's features field has the values basfg, would i be able to just show the deals/data that have an "a" in that field? so it would just show smartphones?

    also how would i do the same using the a usual php command?
    e.g. if $feature CONTAINS a then .....

    (b = bluetooth
    a = smartphone
    g = 3g
    c = camera
    s = slider
    f = flip
    n = normal/candybar
    m = mp3
    v = video)

  2. #12
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I daresay it's possible, though my knowledge of MySQL doesn't stretch that far. In PHP you'd use the strstr() function, or stristr() for case-insensitivity. If you want to use regex, you can use preg_match().
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #13
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by nikomou
    Is it possible to also narrow down the search if a field contains something?
    If we're still discussing SQL here, one can use the LIKE string comparison operator. This allows values to be matched against a simple pattern. MySQL also has a regular expression syntax...

    e.g. there's features field has the values basfg, would i be able to just show the deals/data that have an "a" in that field? so it would just show smartphones?
    ...but that would be overkill for this situation. Include a WHERE clause in the SELECT statement along the lines of:

    Code:
    SELECT ... FROM ... WHERE columnName LIKE '%a%'
    This would return any tuple that contains an 'a' somewhere in its columnName attribute.

    also how would i do the same using the a usual php command?
    I'd use strpos (or stripos), which is quicker than strstr. It's the equivalent of indexOf methods in languages like Java and ECMAScript.

    That said, one wouldn't do this using PHP. The database will manage it much better.

    Mike

  4. #14
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    it doesnt seem to like this:

    Code:
    SELECT * FROM $table WHERE features LIKE '%$feature%'
    are there any alternatives?
    Last edited by nikomou; 11-12-2005 at 04:49 PM. Reason: Missed the quote out..

  5. #15
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You need to close that single quote.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #16
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Sorry, my bad - a simple copy and paste mistake there...

  7. #17
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by nikomou
    it doesnt seem to like this:

    Code:
    SELECT * FROM $table WHERE features LIKE '%$feature%'
    That isn't very helpful. What doesn't it like? Exactly what are you sending to the database server?

    I can assure you that used properly, it will work.

    Mike

  8. #18
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    I sorted that out, I accidently opened the php page in frontpage, and it messed up the code - briliant.

    When nothing is found in the sql database, could you set a defult value?!? Instead of recieving them horrible error thingies!!!


    Code:
    $username="xxxx";
    $password="xxxx";
    $database="xxxx";
    
    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    
    $query="SELECT * FROM $table WHERE handset='$hsnew'";
    $result=mysql_query($query);
    mysql_query($query);
    
    $handset=mysql_result($result,$i,"handset");
    $make=mysql_result($result,$i,"make");
    $id=mysql_result($result,$i,"id");
    $desc=mysql_result($result,$i,"description");
    
    $handsetclean = strtolower($handset);
    $makeclean = strtolower($make);
    $makeclean = str_replace(" ", "-", $makeclean);
    $url = str_replace(" ", "-", $handsetclean);
    
    
    mysql_close();

  9. #19
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You can prefix a function with @ to suppress error reporting. In this case,
    Code:
    @mysql_query($query);
    You must, however, include error checking:
    Code:
    $result=@mysql_query($query);
    if(!$result) {
      $handset="default handset";
      $make="default make";
      $id="default ID";
      $desc="default description"
    } else {
      $handset=mysql_result($result,$i,"handset");
      $make=mysql_result($result,$i,"make");
      $id=mysql_result($result,$i,"id");
      $desc=mysql_result($result,$i,"description");
    }
    
    $handsetclean = strtolower($handset);
    $makeclean = strtolower($make);
    $makeclean = str_replace(" ", "-", $makeclean);
    $url = str_replace(" ", "-", $handsetclean);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #20
    Join Date
    Jun 2006
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    using php and MySql, I am using PHP to call into place data groups one at a time. I want to put in next and back buttons for users to see the next group of information or the prev. I am having trouble with this, and wondering if you twey could help. you seem in this thread to understand mixing php and SQL well. please go to
    http://uorf.org/new/sponsor.php
    if you hit the refresh button a new child is called in. I would like to activate the next button to call the next child ID into place. not sure how this is done. and then the same with the prev button, to display the ID -1. Thank you for your help, I added it to this thread because it was dealing with a similar topic of PHP and SQL working and playing well together.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •