A
A
Alexander Alexandrovich2014-03-05 09:47:15
PHP
Alexander Alexandrovich, 2014-03-05 09:47:15

Why does sql query with Like output an empty string?

Guys tell me please, such a problem
I make a request to the database of the form:
SELECT * FROM `streets` WHERE name like '%Катук%'
Everything works I write php

$q = "SELECT * FROM `streets` WHERE name like '%$b%'";
$result =mysql_fetch_array(mysql_query($q));
echo $result['name']";

It prints an empty string, what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

7 answer(s)
R
Rsa97, 2014-03-05
@Rsa97

MySQL does not have a FIRST() function. Instead, you can use
And move away from using mysql_ in php. use mysqli or PDO.

A
Alexander Zelenin, 2014-03-05
@zelenin

interesting: one request works. another request does not work. What is the question is not clear.
requests are different, there is no first() function.

A
Alexander Zelenin, 2014-03-05
@zelenin

en2.php.net/mysql_fetch_array

Y
Yakov Akulov, 2014-03-05
@jakulov

What if so

echo $b;
$q = "SELECT * FROM `streets` WHERE name like '%$b%'";
$result =mysql_fetch_array(mysql_query($q));
var_dump($result);

S
smoked, 2014-03-05
@smoked

Try to copy the query without pasting the variable and execute it i.e.

$q = "SELECT * FROM `streets` WHERE name like '%Катук%'"

R
rumkin, 2014-03-05
@rumkin

What does var_dump($result) output?
Use mysql_fetch_assoc if you are only using associative arrays. Or switch to pdo.
Remove the extra quotes at the end of the example.
Execute the query without substituting the value of the variable.
Make sure $b is not empty.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question