0
<?php 
include 'connect.php';
$query = "SELECT * FROM (
SELECT CONCAT(customer_name, ' ', address, ' ', street, ' ', phone_1, ' ', phone_2, ' ', phone_3, ' ', phone_4) as `mysearch`
FROM customers) base
WHERE `mysearch` LIKE '%102%'";

$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
echo "customer name: " . $row['customer_name'];    
}
?>

The query is working, but I am missing something for being able to echo the results. It returns the results when ran in phpmyadmin. There are 2 entries in the database that should be returned.
My actual output is as follows:

customer name: customer name:

indicating that it found the 2 entries, but I can't get them to echo. customer_name is a match to a field name in the database.

Eric Darchis
  • 24,537
  • 4
  • 28
  • 49
Rob Alston
  • 39
  • 6
  • 2
    [Little Bobby](http://bobby-tables.com/) says [your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Apr 15 '16 at 13:12
  • 3
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 15 '16 at 13:12
  • Add error reporting to the top of your file(s) right after your opening ` – Jay Blanchard Apr 15 '16 at 13:13
  • We don't know what's in here `connect.php` if it's the same API as the query etc. – Funk Forty Niner Apr 15 '16 at 13:14
  • in subselect you are selecting only `GROUP_CONCAT()`, so select there also `customer_name` as separated column, i.e. do `SELECT customer_name, GROUP_CONCAT()...` – mitkosoft Apr 15 '16 at 13:14

1 Answers1

2

If you want to have customer_name in your result, you must select it as separated column in your subquery:

SELECT 
    * 
    FROM (
        SELECT
            customer_name,
            CONCAT(customer_name, ' ', address, ' ', street, ' ', phone_1, ' ', phone_2, ' ', phone_3, ' ', phone_4) as `mysearch`
        FROM 
            customers
    ) base
WHERE 
    `mysearch` LIKE '%102%'
mitkosoft
  • 5,262
  • 1
  • 13
  • 31