SELECT * FROM admin where company='test & test'
I am running this query but getting error due to &.while printing Its showing
SELECT * FROM admin where company='test ',not accepting & in sql query

Can we run using & in query
need suggestion.

Dani AI

Generated

For : MySQL will happily store and match strings that contain an ampersand β€” as and observed, the same SELECT works in phpMyAdmin. When the value prints as truncated at the ampersand inside a PHP script (showing "test " only) the ampersand is being handled before the SQL runs β€” typically while building a URL/query string or due to incorrect encoding/escaping in PHP/HTML rather than a MySQL limitation (as suspected).

Quick diagnostic checklist and fixes:

  • Confirm what PHP actually received: add a debug dump (var_dump/print_r) of $_GET/$_POST/$_REQUEST or the variable used in the query.
  • If the value is passed in a URL or a header redirect, build the query string safely (use urlencode/http_build_query) so ampersands are not treated as parameter separators.
  • Avoid HTML-escaping input before putting it into SQL. Escape for SQL (or better: use prepared statements) and escape for HTML only when outputting to the browser. If the DB already contains entities like &, use html_entity_decode when matching or better, normalize the stored data.

Minimal examples:

# build URL safely
$url = 'search.php?' . http_build_query(['company' => $company]);
header('Location: ' . $url);

# use a prepared statement (PDO)
$pdo = new PDO($dsn, $user, $pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$stmt = $pdo->prepare('SELECT * FROM admin WHERE company = :company');
$stmt->execute(['company' => $company]);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

Notes: if not using prepared statements, use mysqli_real_escape_string before interpolation. For a quick confirmation of the problem source, log the raw incoming request and the exact URL being generated; if the ampersand is lost there, the solution is URL-encoding (or switching to POST / using http_build_query).

Recommended Answers

All 4 Replies

I just tried a query in phpmyadmin with a '&' in the query and it accepted it just fine. I don't have any data with an & in the data so I didn't get any results but it did accept it.

I tried your both query. It works fine.

SELECT * FROM admin where company='test & test'
I am running this query but getting error due to &.while printing Its showing
SELECT * FROM admin where company='test ',not accepting & in sql query

Can we run using & in query
need suggestion.

I've had this issue before. It would work doing a simple SQL statement in phpMyAdmin for example but wouldn't work within my PHP script. Is this your problem as well?

I don't know the solution as I haven't encountered it again lately.

its not working with in PHP script.but working fine in my sql.
SELECT * FROM admin where company='test & test'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.