Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6781691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:39:14+00:00 2026-05-26T16:39:14+00:00

I have an array represents WHERE clause of my SQL query. $qWhere= array(cName =>

  • 0

I have an array represents WHERE clause of my SQL query.

$qWhere= array("cName" => "Turgay Metal", "cTelephone" => "5556162"); 

my function is: (simplified)

<?php
function getAll($tableName,$qWhere="") {
$qWhere = $qWhere == "" ? $qWhere : (" WHERE " . $qWhere);  
$sql = "SELECT * FROM $tableName $qWhere";}

So i need to write another function to kinda serialize array with foreach as $k = '$v' and then join ” AND ” then return the string. Finally supply the string into main function:

function arr2sql($qWhere) {
    foreach ($qWhere as $key=>$value) { $fields[] = sprintf("%s = '%s'", $key, secure($value));} 
    $field_list = join(' AND ', $fields); unset($qWhere);
    return $field_list;
}

Is this the correct way or may it be achieved more clean and profesionally?

Thanks for help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:39:15+00:00Added an answer on May 26, 2026 at 4:39 pm

    I think it looks pretty good; I like it. I assume secure() protects from sql injection. I don’t really see anything I would change.

    You could take braces out of the for loop since its one statement, but that’s no biggy

    foreach ($qWhere as $key=>$value)
        $fields[] = sprintf("%s = '%s'", $key, secure($value));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have an array that represents a set of points: x = [2,
Suppose I have an array that mimics a database table. Each array element represents
I have a byte array that represents a complete TCP/IP packet. For clarification, the
I have an array in the form of ' int[][] ' that represents the
I have a an array of byte, size n, that really represents an array
I have a 4D array of measurements in MATLAB. Each dimension represents a different
In MATLAB, I have an array of latitude and longitude pairs that represents locations
I have an array that represents a grid For the sake of this example
I have a string which represents a multidimensional array in the format: [[A, a],
I have a static large array-of-arrays, witch represents tree structure with about ~100k nodes,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.