Special SQL query

Hi,

I have this SQL query:
           $query = $mysqli->prepare("SELECT * FROM ft WHERE Aar = ? AND AmtHerredSognGade LIKE '%".$sogn."%'");
            $query->bind_param("s", $start);

Open in new window


I need to be able to put in a constant in the search string before the result of $sogn.
E.g if the $sogn returns "Gudhjem" I need to put in a string before Gudhjem, in this case Bornholm, so that the total searchstring becomes "Bornholm Gudhjem".
The string I need to put in before the $sogn are represented in the table in a column with the headline "Amt" and I would naturally like to pull it from there.
I know that the string for searching for $sogn are broad with jokers but it have to be that because the "AmtHerredSognGade" field in the table is a combined field from where I need to pull out the part matching the query.

I hope that somebody have the golden idea to fix this :)
Peter KromanSales ExecutiveAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
Your query should look like below - Print your query and see if you are getting below or not?

SELECT * FROM ft
WHERE Aar = 'Value1' AND AmtHerredSognGade LIKE '%Bornholm Gudhjem%'
0
Peter KromanSales ExecutiveAuthor Commented:
No not quite .

I am pulling $sogn from the database in the table column "Sogn", and I would like to pull the corresponding value from the table column "Amt" and put it in front of the query with a space between the two.

What I don't want is to put in 'hard-coded' values, like "Bornholm" and "Gudhjem", into the query (the only 'hard-coded' value should be the space between the two values :) )
0
Pawan KumarDatabase ExpertCommented:
In the previous comment I am saying after replacing the parameters. anyways please try like below-

Please try like this -

$query = $mysqli->prepare("SELECT * FROM ft WHERE Aar = ? AND AmtHerredSognGade LIKE '%' ? '%' ");
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Pawan KumarDatabase ExpertCommented:
Updated--

$likeParam = "%" . $sogn . "%";
$stmt = $mysqli->prepare("SELECT * FROM ft WHERE Aar = ? AND AmtHerredSognGade LIKE ?");
$query->bind_param("s", $start);
$stmt->bind_param("s", $likeParam);

Open in new window

0
Peter KromanSales ExecutiveAuthor Commented:
I don't quite understand what you are doing here Kumar.

Here is my php code for the queries. I would if possible like to keep it in appr, the same syntax :)  I don't see where the Amt comes in in your suggestion??

  // Trim the input values
    $start = trim($_POST['query_start']); 
    $sogn = trim($_POST['sogn']);





    if (!empty($start) && empty($sogn)):

        // We have a 'start' value and no 'sogn' value


        $query = $mysqli->prepare("SELECT * FROM ft WHERE Aar = ?");
        $query->bind_param("s", $start);
        //print "query_gade= " .$gade;
  

    else:

        if (!empty($sogn) && empty($start)):
            // We have a 'sogn' value and no 'start' value
            $query = $mysqli->prepare("SELECT * FROM ft WHERE AmtHerredSognGade LIKE '%".$sogn."%'");
            //$query->bind_param("s", $start, $end);

        elseif (!empty($start) && !empty($sogn)):
            // We have a 'start' and a 'sogn' value
            $query = $mysqli->prepare("SELECT * FROM ft WHERE Aar = ? AND AmtHerredSognGade LIKE '%".$sogn."%'");
            $query->bind_param("s", $start);

        endif;

    endif;

    // Do we have a query to run
    if (isset($query) && $query):
        $query->execute();
        $results = $query->get_result();
    endif;

Open in new window

0
Peter KromanSales ExecutiveAuthor Commented:
I believe that the question here is not valid, so I am closing it, and will return with another (and hopefully better) one if needed.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Peter KromanSales ExecutiveAuthor Commented:
Question not valid
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.