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

  • Home
  • SEARCH
  • 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 9031763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:48:44+00:00 2026-06-16T07:48:44+00:00

In this code I am trying to search a database with two columns, retrieve

  • 0

In this code I am trying to search a database with two columns, retrieve 11 pairs of those based on one of the matches (companyname; this was sent from JavaScript using POST). Then I am trying to create a JSON data.

<?php
header("Content-Type: application/json");
$db = new PDO('mysql:host=localhost;dbname=pl;charset=UTF-8', 'user', 'password');
if(isset($_POST['companyname']) == true && empty($_POST['companyname']) == false) {

    $searchterm = $_POST['companyname'];
    $i=0;
    $jasondata = '{';

    $query = $db->query("SELECT companyname, axiscategory FROM axispl WHERE companyname LIKE '$searchterm%' LIMIT 11");
    $c = 1;
    while (($row=$query->fetchAll(PDO::FETCH_ASSOC)) !==false && $c<11) {
        $i++;
        $companyname = $row["companyname"];
        $axiscategory = $row["axiscategory"];
        $jasondata .='"combi'.$i.'":{"companyname":"'.$companyname.'","axiscategory":"'.$axiscategory.'"},';
        $n=$row['companyname'];
        $l=strlen($n);
        if($l>50){$c = $c+2;}else{$c = $c+1;}       

    }
    $jasondata = chop($jasondata, ",");
    $jasondata .= '}';
    echo $jasondata;

}   
?>
  • 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-06-16T07:48:46+00:00Added an answer on June 16, 2026 at 7:48 am

    What should you change? Well, I mentioned using json_encode earlier, and Charles quite rightly points out the security vulnerability.

    I’d also work on the code formatting as well – my recommendation is to work to a sensible margin. This used to be 80 characters for historical reasons, but 100-120 is quite a good limit now – it means you can have several code viewers stacked left-to-right on your screen without the need to scroll horizontally in each one. And you can post to StackOverflow similarly without wrapping issues!

    Consider this snippet of yours:

    $query = $db->query("
        SELECT
            companyname, axiscategory
        FROM
            axispl
        WHERE
            companyname LIKE '$searchterm%'
        LIMIT 11
    ");
    

    Also:

    $db = new PDO(
        'mysql:host=localhost;dbname=pl;charset=UTF-8',
        'user',
        'password'
    );
    

    Much more readable, eh? My motto: give your code room to breathe.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to search two fields as one from a MySQL database using PHP.
I am trying this code: entLoop:for(var i:*in entities) { for(var i2:*in ignoreEntities) { if(entities[i].type==ignoreEntities[i2])
I'm trying this code to access a property but it gives me an error:
I am new to Django and was trying this code in a tutorial. But
This code shows the structure of what I am trying to do. import multiprocessing
When trying to compile this code: #include <iostream> #include <vector> using namespace std; class
I'm trying to code this site (keyword trying) and I'm trying to figure out
I am trying to code this algorithm. I am stuck in the part of
I trying to compile this code: Int64 itag = BitConverter.ToInt64(temp, 0); itag &= 0xFFFFFFFFFFFFFC00;
I was trying to code this problem in fortran. The output file contains the

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.