How do I query a database in PHP and return results based on matching user-input?

时间:2016-02-12 21:09:04

标签: php mysql mysqli user-input

I’m trying to write a PHP script with MySQLi to query a database.

I’d like it if the user-input could be checked against the database and then return a result from the column ‘conjugation’ if the string in the column ‘root’ of the table ‘normal_verbs’ is in the input.

So if the user input is something like "foobar", and the root-column has "foo", I'd like it to see 'foo' in 'foobar' and return that value of 'conjugation' in that row.

I can’t seem to get the query to work like I want it to. The one I'm using below is basically just a placeholder. I don't entirely understand why it doesn't work.

What I’m trying, is this :

@Override
public boolean dispatchTouchEvent(MotionEvent ev) 
{   super.dispatchTouchEvent(ev);
    // TO DO
    return false; // or true
}

It’s not giving me any errors, so I think it’s connecting to the database.

EDIT2: I was wrong. I was missing something obvious due to misunderstanding MySQLi and have edited the code accordingly. So the above code does work in that it connects to the database and returns a result, but I'm still stumped on a viable SQL statement to do what I want it to do.

2 个答案:

答案 0 :(得分:4)

Please try this:

echo $row['count_options'];
echo $['options'];

It selects all rows where root contains $y anywhere.

In addition, your code is vulnerable to SQL injections. Please look here for more information.

答案 1 :(得分:0)

尝试这样的SQL查询

SELECT `conjugation` from normal_verbs where `root` like '%$y%'