need help with php and mysql

by 21 replies
24
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%'
#programming #mysql #php
  • What error message are you getting? Can you post it here?
    • [1] reply
    • Warning: mysql_num_rows() expects parameter 1 to be resource
  • 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.
    • [1] reply
    • 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
  • 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.
    • [1] reply
    • 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
  • 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.
    • [ 1 ] Thanks
  • $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';
    }
  • 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.
    • [ 1 ] Thanks
  • 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
  • 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());
    } 
    • [1] reply
    • same error . showing string value instead of string
  • Try it like this

    PHP Code:
    $query = 'select * from table where name like \'%'.$string.'%\' and keyword like \'%'.$string2.'%\''; 
  • Did you really name your table "table"?
    It's a reserved word. You'll need to put it within tick marks (`) to use it.
    Where did "add" come from?
    Are you posting your actual code or not?
    • [2] replies
  • Yes friend this should work for sure..
  • 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.
  • 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.

  • Did you try executing the SQL via Mysql Console or PHPMyadmin or something ? Do you get any errors there ?
  • have you tried to echo the your query? paste it in your console and see if it works

Next Topics on Trending Feed