0

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?

2
Contributors
1
Reply
8
Views
3 Years
Discussion Span
Last Post by cereal
0

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();
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.