• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 55
  • Last Modified:

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 :)
0
Peter Kroman
Asked:
Peter Kroman
  • 4
  • 3
1 Solution
 
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 KromanSenior Proposal SpecialistAuthor 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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
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 KromanSenior Proposal SpecialistAuthor 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 KromanSenior Proposal SpecialistAuthor 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
 
Peter KromanSenior Proposal SpecialistAuthor Commented:
Question not valid
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now