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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:42:24+00:00 2026-05-27T10:42:24+00:00

The goal: Connect to database using php to extract results Loop through results Store

  • 0

The goal:

  1. Connect to database using php to extract results
  2. Loop through results
  3. Store results in a way such that the are accessible during run-time

How I tried to do this:

  1. SQL query to get data
  2. Create function with inputs of column data that outputs this data as a javascript object
  3. Store javascript object in javascript array (not yet done)

For some reason (which may or may not be obvious to a more trained eye) I am running into a bug.

Note: database connection works perfectly, and the proper results are fetched with no issues. The issue is 100% the way I am trying to execute my javascript via php. I am assuming the php is seeing the var etc and is compiling it.

Please advise on the code below and if more specifics are needed I will happily expand:

        <?
        if (!isset($_COOKIE['authPw'])){
        echo '<script>window.location="http://www.google.com"</script>';
        }
        ?>
        <html>
            <head>
            </head>

            <body>

            <?

            function jsEcho($i,$cN,$fN,$lN,$pN,$eM){
                $jString="var appObj = new Object();";
                $jString+="appObj.id=" + $i + ";";
                $jString+="appObj.companyName=\'" + $cN + "\';";
                $jString+="appObj.firstName=\'" + $fN + "\';";
                $jString+="appObj.lastName=\'" + $lN + "\';";
                $jString+="appObj.phoneNumber=\'" + $pN + "\';";
                $jString+="appObj.eMail=\'" + $eM + "\';";  
                //echo "1ONE1".$jString."2TWO2";
                return $jString;
            }

            $host="localhost"; // Host name 
            $username="abc"; // Mysql username 
            $password="def"; // Mysql password 
            $db_name="ghi"; // Database name 
            $tbl_name="jkl"; // Table name





            $link=mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
            mysql_select_db("$db_name")or die("cannot select DB");

            $mySQL="select * from ".$tbl_name.";";
            echo $mySQL;
            $result=mysql_query($mySQL);
            $jS="var aApps=new Array();";

                while ($row = mysql_fetch_array($result))
                {

                    $iD=$row["id"];
                    $companyName=$row["companyName"];
                    $firstName=$row["firstName"];
                    $lastName=$row["lastName"];
                    $phone=$row["phone"];
                    $email=$row["email"];

                    $jS+=jsEcho($iD,$companyName,$firstName,$lastName,$phone,$email);
                         }

            ?>


            </body>
            <?
            echo '<script>';
                    echo $jS;
            echo '</script>';
            ?>
        </html>
  • 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-27T10:42:25+00:00Added an answer on May 27, 2026 at 10:42 am

    Your script is after the body element closes. That’s not valid html. Possibly it is not being evaluated. I suspect the output is also not valid javascript either.

    Also, your jsEcho method is verbose, unsafe, and unnecessary. Use this pattern:

    <?php
    // prepare all your data as a single PHP object or array
    $jsdata = array();
    while ($row = mysql_fetch_object($result)) {
        $jsdata[] = $row;
    }
    
    // now encode to JSON
    $jsenc = json_encode($jsdata);
    
    // now html-escape it
    ?>
    <script>
    var allMyData = <?php echo htmlspecialchars($jsenc, ENT_NOQUOTES, 'utf-8')?>;
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My goal is to connect to my database either manually or using an sqladapater,
I'm using entity framework to connect with the database. I've one little problem: I've
The goal is that I would like to cycle through ALL of the .CSV
I'm using entity framework to connect the database. I've a table(Let's call it File)
Goal: Create Photomosaics programmatically using .NET and C#. Main reason I'd like to do
my goal is to write a stored proc that can collect all field values
My goal is to connect to an OWA page (Microsoft Office Outlook Web Access
How to read macro variable as a character in SAS SQL pass-through My goal
I have a Sybase ASE server that I'm able to connect to with ASE
My goal is to connect to an Oracle 9i instance from my OS X

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.