How can I query a database in PHP and return results based on I / O matching?

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

Id like it if user input can be checked against the database and then return the result from the column mate if there is a row in the root column of the normal_verbs table at the input.

So, if the user input is something like “foobar” and the root column is “foo”, I would like it to see “foo” in “foobar” and return that “pairing” value on this line.

I cannot make the request work the way I want. The one I use below is basically just a placeholder. I do not quite understand why this is not working.

What I'm trying is:

function db_connect() { static $connection; if(!isset($connection)) { $connection = mysqli_connect('localhost','user','password','Verb_Bank'); } if($connection === false) { return mysqli_connect_error(); } return $connection; } function db_query($query) { $connection = db_connect(); $result = mysqli_query($connection,$query); return $result; } function db_quote($value) { $connection = db_connect(); return "'" . mysqli_real_escape_string($connection,$value) . "'"; } $m= db_query("SELECT `conjugation` from normal_verbs where `root` in (" . $y . ")"); if($m === false) { // Handle failure - log the error, notify administrator, etc. } else { // Fetch all the rows in an array $rows = array(); while ($row = mysqli_fetch_assoc($m)) { $rows[] = $row; } } print_r ($rows); 

This does not give me any errors, so I think it connects to the database.

EDIT2: I was wrong. I lost something obvious due to a misunderstanding of MySQLi and edited the code accordingly. So the above code works in the sense that it connects to the database and returns the result, but I still can’t say that I want it to do what I want.

+5
source share
2 answers

Please try the following:

 SELECT 'conjugation' FROM 'normal_verbs' WHERE " . $y . " LIKE CONCAT('%',root,'%') 

It selects all the lines where root contains $ y anywhere.

In addition, your code is vulnerable to SQL injection. Please see here for more information.

+4
source

Try this SQL query like this

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

Source: https://habr.com/ru/post/1242911/


All Articles