need help with php and mysql

21 replies
hello,

i need 'like' function twins in mysql query.

tried this but getting error.

select * from table where snae like '%$string%' and uspw like '%$string2%'
#mysql #php
  • Profile picture of the author HonestCoder
    What error message are you getting? Can you post it here?
    Signature
    * LOOKING FOR A RELIABLE WEB DEVELOPER? * HAD ENOUGH OF SEARCHING FREELANCE SITES? *

    I build complete sites from back end to user interface with attention to detail and effective communication. If you want to get your project done CONTACT ME now to discuss your needs.


    {{ DiscussionBoard.errors[5140438].message }}
  • Profile picture of the author HonestCoder
    Ok that warning has nothing to do with your SQL, it's something in your PHP code.

    You are using mysql_num_rows to get a count of the number of results, but whatever variable you are using in there is not what it's expecting. I think you will have to post at least part of your code as it's just guesswork right now.
    Signature
    * LOOKING FOR A RELIABLE WEB DEVELOPER? * HAD ENOUGH OF SEARCHING FREELANCE SITES? *

    I build complete sites from back end to user interface with attention to detail and effective communication. If you want to get your project done CONTACT ME now to discuss your needs.


    {{ DiscussionBoard.errors[5140516].message }}
    • Profile picture of the author rahman.warrior
      Originally Posted by HonestCoder View Post

      Ok that warning has nothing to do with your SQL, it's something in your PHP code.

      You are using mysql_num_rows to get a count of the number of results, but whatever variable you are using in there is not what it's expecting. I think you will have to post at least part of your code as it's just guesswork right now.
      Originally Posted by HonestCoder View Post

      Ok that warning has nothing to do with your SQL, it's something in your PHP code.

      You are using mysql_num_rows to get a count of the number of results, but whatever variable you are using in there is not what it's expecting. I think you will have to post at least part of your code as it's just guesswork right now.
      am explaining what i need.

      there have two column on database. username , keywords

      values in this two column :

      username : user99

      keyword : green red black

      so, there is two textbox on user side. username, keyword. i want to show result when user searching like this ( any word on keyword ):

      user name : user99

      keyword : red
      {{ DiscussionBoard.errors[5140617].message }}
  • Profile picture of the author HonestCoder
    Yes I understand that, but you provided a PHP warning, so I'm trying to help with that. Your SQL might be indirectly causing the PHP warning because it's not returning any results. It would be helpful to see what MySQL is returning so....

    Something you could do, is look for the line of code that runs mysql_query() and change it to something like this:

    Code:
    $result = mysql_query($query) or die($query."<br/><br/>".mysql_error());
    That will show you what your generated query looks like and what, if any, error is coming back from MySQL.
    Signature
    * LOOKING FOR A RELIABLE WEB DEVELOPER? * HAD ENOUGH OF SEARCHING FREELANCE SITES? *

    I build complete sites from back end to user interface with attention to detail and effective communication. If you want to get your project done CONTACT ME now to discuss your needs.


    {{ DiscussionBoard.errors[5140697].message }}
    • Profile picture of the author rahman.warrior
      Originally Posted by HonestCoder View Post

      Yes I understand that, but you provided a PHP warning, so I'm trying to help with that. Your SQL might be indirectly causing the PHP warning because it's not returning any results. It would be helpful to see what MySQL is returning so....

      Something you could do, is look for the line of code that runs mysql_query() and change it to something like this:

      Code:
       = mysql_query() or die(."<br/><br/>".mysql_error());
      That will show you what your generated query looks like and what, if any, error is coming back from MySQL.
      You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'add like '%likee%'' at line 1
      {{ DiscussionBoard.errors[5140738].message }}
  • Profile picture of the author HonestCoder
    Ok and the query itself, can you post the whole SQL query that should have appeared above that error message? The reason I'm asking is we can see the whole query as it was provided to MySQL and then hopefully that SQL error will make sense, thanks.
    Signature
    * LOOKING FOR A RELIABLE WEB DEVELOPER? * HAD ENOUGH OF SEARCHING FREELANCE SITES? *

    I build complete sites from back end to user interface with attention to detail and effective communication. If you want to get your project done CONTACT ME now to discuss your needs.


    {{ DiscussionBoard.errors[5140756].message }}
  • Profile picture of the author rahman.warrior
    $result = mysql_query("select * from table where name like '%string%' and keyword like '%string2%' ") or die($query."<br/><br/>".mysql_error());

    if(mysql_num_rows($check)!=0){
    $stat='done';
    }
    else{
    $stat='pending';
    }
    {{ DiscussionBoard.errors[5140801].message }}
  • Profile picture of the author HonestCoder
    Thanks, we're getting there now

    PHP Code:
    $query "select * from table where name like '%string%' and keyword like '%string2%' ";
    $check mysql_query($query) or die($query."<br/><br/>".mysql_error());

    Change it to exactly that, copy and paste to replace that bit of the code. Then try your script and reply with exactly what you get in the browser, the error AND the whole SQL query that you see in the browser. That's the only way I can tell you what is wrong in the SQL without seeing your script.
    Signature
    * LOOKING FOR A RELIABLE WEB DEVELOPER? * HAD ENOUGH OF SEARCHING FREELANCE SITES? *

    I build complete sites from back end to user interface with attention to detail and effective communication. If you want to get your project done CONTACT ME now to discuss your needs.


    {{ DiscussionBoard.errors[5140853].message }}
  • Profile picture of the author rahman.warrior
    select * from table where name like '%string%' and keyword like '%string2%'

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'keyword like '%string2%'' at line 1


    this is now showing nothing else

    any other way to do this
    {{ DiscussionBoard.errors[5140880].message }}
  • Profile picture of the author HonestCoder
    Sorry the $$$ disappeared from my last post, should have been:

    PHP Code:
    $query "select * from table where name like '%$string%' and keyword like '%$string2%' ";
    $check mysql_query($query) or die($query."<br/><br/>".mysql_error());

    Signature
    * LOOKING FOR A RELIABLE WEB DEVELOPER? * HAD ENOUGH OF SEARCHING FREELANCE SITES? *

    I build complete sites from back end to user interface with attention to detail and effective communication. If you want to get your project done CONTACT ME now to discuss your needs.


    {{ DiscussionBoard.errors[5140917].message }}
  • Profile picture of the author HonestCoder
    Try it like this

    PHP Code:
    $query 'select * from table where name like \'%'.$string.'%\' and keyword like \'%'.$string2.'%\''
    Signature
    * LOOKING FOR A RELIABLE WEB DEVELOPER? * HAD ENOUGH OF SEARCHING FREELANCE SITES? *

    I build complete sites from back end to user interface with attention to detail and effective communication. If you want to get your project done CONTACT ME now to discuss your needs.


    {{ DiscussionBoard.errors[5140960].message }}
  • Profile picture of the author KirkMcD
    Did you really name your table "table"?
    It's a reserved word. You'll need to put it within tick marks (`) to use it.
    check the manual that corresponds to your MySQL server version for the right syntax to use near 'add like '%likee%'' at line 1
    Where did "add" come from?
    Are you posting your actual code or not?
    {{ DiscussionBoard.errors[5141128].message }}
    • Profile picture of the author joenineo
      Hi

      This also works

      $query = "select * from table where name like '%".$string."%' and keyword like '%".$string2."%'";
      {{ DiscussionBoard.errors[5142116].message }}
    • Profile picture of the author joenineo
      This will also work

      $query = "select * from table where name like '%".$string."%' and keyword like '%".$string2."%'";
      {{ DiscussionBoard.errors[5142121].message }}
  • Profile picture of the author seogame
    Yes friend this should work for sure..
    {{ DiscussionBoard.errors[5142167].message }}
  • Profile picture of the author HonestCoder
    Yes but as KirkMcD said, you need tick marks in there because if you are really are using 'table' as the table name it's a reserved word (like 'SELECT', 'FROM', 'AND' are reserved for obvious reasons).

    PHP Code:
    $query "SELECT * FROM `table` WHERE NAME LIKE '%$string%' AND `keyword` LIKE '%$string2%'"
    PHP Code:
    $query 'SELECT * FROM `table` WHERE NAME LIKE \'%'.$string.'%\' AND `keyword` LIKE \'%'.$string2.'%\''
    Either of those will work, and that will rule out an error caused by using 'table' as the table name. And note there is a difference between ` and '. This could happen a lot faster if you just post all the relevant PHP code.

    Another tip, when you do get the whole generated SQL string in your browser, try running the exact same SQL query in phpmyadmin if possible, it's a lot quicker to debug when you can just edit the query and try it again.
    Signature
    * LOOKING FOR A RELIABLE WEB DEVELOPER? * HAD ENOUGH OF SEARCHING FREELANCE SITES? *

    I build complete sites from back end to user interface with attention to detail and effective communication. If you want to get your project done CONTACT ME now to discuss your needs.


    {{ DiscussionBoard.errors[5142310].message }}
  • Profile picture of the author HonestCoder
    And are you sure you want to use 'AND' in there. If you want results where there is a match in either one or the other column, then use OR.

    PHP Code:
    $query "SELECT * FROM `table` WHERE `name` LIKE '%$string%' OR `keyword` LIKE '%$string2%'"
    Signature
    * LOOKING FOR A RELIABLE WEB DEVELOPER? * HAD ENOUGH OF SEARCHING FREELANCE SITES? *

    I build complete sites from back end to user interface with attention to detail and effective communication. If you want to get your project done CONTACT ME now to discuss your needs.


    {{ DiscussionBoard.errors[5142333].message }}
  • Profile picture of the author rockerzz
    Did you try executing the SQL via Mysql Console or PHPMyadmin or something ? Do you get any errors there ?
    {{ DiscussionBoard.errors[5177223].message }}
  • Profile picture of the author jcwebhole
    have you tried to echo the your query? paste it in your console and see if it works
    {{ DiscussionBoard.errors[5225625].message }}

Trending Topics