Hello friends.

I have 5 php files having bunch of Inline Queries. I am trying to pluck all the queries from those files and put them in the seperate php file.

When I tried to do so, the variables called in the queries are only defined in those files. So its giving me the error of unknown variables.

Can anyone suggest me the solution?

If you use prepared statements then it's not a problem, create the list of queries:

<?php

    return array(

        "query_cities"  => "SELECT * FROM cities WHERE name = ?",
        "query_address" => "SELECT * FROM addresses a JOIN cities c ON c.id = a.city_id WHERE street_name = ? AND zipcode = ?",

    );

Then require the list:

<?php

    $list = require './queries.php';

    # query cities
    $param = array(
        $_GET['city_id']
    );

    $stmt = $pdo->prepare($list['query_city']);
    $stmt->execute($param);
    $city = $stmt->fetchAll();


    # query addresses
    $param = array(
        $_GET['street_name'],
        $_GET['zipcode']
    );

    $stmt = $pdo->prepare($list['query_address']);
    $stmt->execute($param);
    $address = $stmt->fetchAll();
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.