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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:35:04+00:00 2026-05-19T03:35:04+00:00

I am trying to return my SQL query array into a javascript array and

  • 0

I am trying to return my SQL query array into a javascript array and then display the info one at a time. I have found a few helpful posts already on here but I still cannot get it to work. I am new to ajax and so please forgive any stupid mistakes. Below is the php followed by a description.
php: this is in an external file from index.php

<?php
include('connection.php');

$query = "SELECT * FROM photos";
$queryresult = mysql_query($query);

while ( $line = mysql_fetch_array($result) ) {
    $path[] = $row[0];
}
$paths = json_encode($path);
header('Content-type: application/json');
echo $paths;
?>

This gets the results (they are file paths) array and json encodes them to pass to javascript. Connection.php is correct and it is working.

HTML/Javascript:

<html>
<head>
<script src="JavaScript/gjs.js" type="text/javascript"></script>
<script src="jquery/jquery-1.4.3.min.js" type="text/javascript"></script>
<script>
function imageload(){
var i = 1;
$.getJSON('phpfiles/getpiccode.php', function(data) {

    var can = document.getElementById('canvas').getContext('2d');

    $.each(data,function(idx, row){
    var img = new Image();
    img.onload = function(){
        can.drawImage(img, 0, 0, 1280, 800);
    }
    img.src = row;
            i++;
});

});

}
</script>
</head>

<body>
<div class="container">
<canvas id="canvas" class="canvas-one" width="1280" height="800">
<script>imageload();</script>This text is displayed if your browser does not support HTML5 Canvas</canvas>
</div>

</body>
</html>

I hope that makes sense. Thanks again!

  • 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-19T03:35:04+00:00Added an answer on May 19, 2026 at 3:35 am

    Use json_encode() to encode it as JSON.
    In recent browsers you can simply turn the string from that function into a JavaScript object using var obj = JSON.parse(yourstring); – but better use e.g. jQuery for AJAX and JSON parsing.

    Update: Your JavaScript should looke like that to iterate over the data from your query:

    $.getJSON('phpfiles/getpiccode.php', function(data) {
        // get canvas object. it's the same for all images so only do it once
        var can = document.getElementById('canvas').getContext('2d');
        // iterate over the elements in data
        $.each(data, function(idx, row) {
            var img = new Image();
            img.onload = function() {
                can.drawImage(img, 0, 0, 1280, 800);
            }
            img.src = row;
        });
    });
    

    However, it might not do what you want: It will draw all images pretty much at once at the same position.

    Also replace <script>imageload() </script> (assuming that’s the function containing your JavaScript) with <script type="text/javascript">imageload();</script> as that’s the correct/proper syntax.

    In your PHP code you’ll have to replace return $paths; with echo $paths; unless you are using some framework which relies on your file returning something. Additionally it’d be good to send a JSON header: header('Content-type: application/json');

    PS: SELECT * combined with MYSQL_NUM is a BadThing. It relies on the columns in the table having a certain order. If you just need one column use “SELECT columnName”; if you need all, use MYSQL_ASSOC to get an associative array.

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

Sidebar

Related Questions

I am trying to return an array Data Member from one smaller 2D Array
I am trying to return the result that I found in my query to
I'm trying to run a LINQ to SQL query that returns a result in
I'm trying to return a transparent GIF from an .aspx page for display within
im trying to return a string value from a method inside my script tag
I'm trying to return a dictionary from a function. I believe the function is
I'm trying to return an iterator for a vector in a templated class (I'm
I am trying to return the physical file path of a database's mdf/ldf files.
I'm trying to return a record from LINQ2SQL as a JsonResult to update my
I'm trying to return sequence.nextval to my program from a proc stored in a

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.