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 8262365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:44:03+00:00 2026-06-08T03:44:03+00:00

Hello fellow StackOverflowers! I have some information in a database that I need pulled

  • 0

Hello fellow StackOverflowers!

I have some information in a database that I need pulled out and put into javascript context.

I am successfully getting the data with mysqlli:

$mysqli = new mysqli("localhost", "censored", "censored", "censored");

/* check connection */
if (mysqli_connect_errno())
{
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query = "SELECT * FROM foodstuff";

The foodstuff_categories contain:

  • id, category_id, category and language.
  • example: 1, 1, vegetables, en

The foodstuff contain:

  • id, category, name and language
  • example : 1, 1, potato, en

category in foodstuff is the same as category_id in foodstuff_categories.

foodstuff_categories contains the name of the category whilst foodstuff contains the name of the foodstuff.

Problem:
I need to make an object out of the database data that is compliant with jQuery UI’s autosuggestion widget which looks like this:

 var availableTags =
 [
    { label: "veal"  , category: "meat" },
    { label: "potato", category: "vegetables" }
 ];

I am currently calling the php script with:

$.get('php/dbFoods.php', function(data)
{
    //gotta do something with that data! :)
});

my thoughts
I think i need to join some information in the select statement and then find a smart way to turn that data into an object compliant with the jquery UI widget.

appended inoformation after answer has been given

Use the alteriative answer in the accepted answer.

The jquery ui autocomplete from database requires more information than is immediately apparent! Refer to this page: http://www.simonbattersby.com/blog/jquery-ui-autocomplete-with-a-remote-database-and-php/

  • 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-08T03:44:04+00:00Added an answer on June 8, 2026 at 3:44 am

    Untested:

    ...
    $query = "
      SELECT fc.`category` AS category, f.`name` AS label
      FROM `foodstuff` AS f 
      INNER JOIN `foodstuff_categories` AS fc 
      ON fc.`category_id` = f.`category`
    ";
    $res = $mysqli->query($query);
    $rows = array();
    while ($data = $res->fetch_assoc()) {
        $rows[] = $data;
    }
    // convert iso to utf8 data 
    function encodeArray($array) {
        foreach ($array as $index => $row) {
            $array[$index] = array(
                'category' => mb_check_encoding($row['category'], 'UTF-8') ? $row['category'] : utf8_encode($row['category'])
                , 'label' => mb_check_encoding($row['label'], 'UTF-8') ? $row['label'] : utf8_encode($row['label'])
            );
        }
        return $array;
    }
    echo json_encode(encodeArray($rows));
    

    Alternative:

    ...
    $mysqli->query("SET NAMES 'utf8'", $connection); // from now on return data in utf8 format (only)
    $query = "
      SELECT fc.`category` AS category, f.`name` AS label
      FROM `foodstuff` AS f 
      INNER JOIN `foodstuff_categories` AS fc 
      ON fc.`category_id` = f.`category`
    ";
    $res = $mysqli->query($query);
    $rows = array();
    while ($data = $res->fetch_assoc()) {
        $rows[] = $data;
    }
    echo json_encode($rows);
    

    The query builds an associative array with the given structure. json_encode() builds an JSON for the client. jQuery can automatically convert the JSON to an object which could be parsed by javascript. Possibly you should add the dataType parameter (here ‘json’) to your jQuery get:

    $.get('php/dbFoods.php', function(data)
    {
        //gotta do something with that data! :)
    }, 'json');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello fellow programmers, I have made a function that looks up the database if
Hello fellow StackOverflowers. I'm have a brain fart right now, and I cannot seem
Hello fellow programmers. I have a SQL Server 2005 query that is taking a
Oh hello there, fellow SO members, I have a web service that returns XML
Hello fellow earthlings! For some time I'm fantasizing possible solutions, but i'm out of
Hello Fellow stackoverflowers, I´m stuck writing a piece of code. I have application with
Hello fellow earthlings, I have an image as my background that I created in
Hello fellow programmers. I want to make a browser extension that will put a
Hello fellow computer people :) I have a shell script that I will use
hello fellow java developers. I'm having a bit of an issue here. I have

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.