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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:30:29+00:00 2026-05-28T05:30:29+00:00

I am attempting to get all the data from a MySQL db with PHP,

  • 0

I am attempting to get all the data from a MySQL db with PHP, initialise a 2D java array and populate it with the PHP data.

I am having trouble embedding JS in the PHP. I have marked up what is working and what isn’t.

As you will see, some of the embedded java works but not all.

Any thoughts?

<body>

<?php
    $con = mysql_connect("XXXXXX.COM","guest","password");
    mysql_select_db("HHG", $con);

    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }
    else 
    {
        $result = mysql_query("SELECT * FROM articles", $con);
        $numrows = mysql_num_rows($result);
        echo "DB connection OK <br/>";
        echo "Found ";
        echo $numrows;
        echo " records <br/><br/>";
    } // EVERYTHING WORKS UP TO HERE
?> 


<script type="text/javascript">

document.write("THIS IS THE FISRT JS DOING SOMETHING"); // THIS DOES NOTHING
numrows = <?php echo $numrows; ?>; // THIS DOES NOTHING
string [][] hhgdata = new string[numrows][4]; // THIS DOES NOTHING
document.write("Records = " + numrows + "<br/>"); // THIS DOES NOTHING

</script>

<?
    $counter = 1;
    while ($row = mysql_fetch_assoc($result))
    {
        echo $row["idimg"]; echo "<br/>";  //THIS WORKS
        $hhgtitle = $row["hhgtitle"]; //THIS WORKS
        echo $hhgtitle; echo "<br/>"; //THIS WORKS

        ?>

        <script type="text/javascript"> //THIS WORKS
            counter = <?php echo $counter; ?>; //THIS WORKS
            document.write("counter = " + counter + "<br/><br/>"); //THIS WORKS

            hhgtitle = <?php echo $hhgtitle; ?>; // THIS DOES NOTHING
            document.write("Title: "); // THIS DOES NOTHING
            hhgdata[counter][1]= hhgtitle; // THIS DOES NOTHING
            document.write(hhgdata[counter][1]); // THIS DOES NOTHING
        </script>

        <?
        $counter++; // THIS WORKS
    }
?>

</body>
  • 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-28T05:30:29+00:00Added an answer on May 28, 2026 at 5:30 am

    You are mixing up Java and JavaScript. For example this is Java syntax, you can’t write this within a script tag which should only contain JavaScript:

    string [][] hhgdata = new string[numrows][4];
    

    The JavaScript arrays are dynamic, this should be enough:

    var hhgdata = [];
    

    When you want to add another array into it, as you seem to be doing later in your code, just do this:

    hhgdata[counter] = [];
    

    And then assign to the inner array:

    hhgdata[counter][1] = hhgtitle;
    

    You are also creating multiple assigning an unquoted string literal to variable with this (assuming $hhgtitle contains a string):

    hhgtitle = <?php echo $hhgtitle; ?>;
    

    It should be something like this:

    hhgtitle = <?php echo '"' . $hhgtitle .'"'; ?>;
    

    Finally, while it’s not incorrect, your PHP while loop is creating multiple script elements in your HTML.

    EDIT

    I have made the changes described above as well as in comments, copy-paste exactly and see how it goes:

    <body>
    
    <?php
        $con = mysql_connect("XXXXXX.COM","guest","password");
        mysql_select_db("HHG", $con);
    
        if (!$con)
        {
            die('Could not connect: ' . mysql_error());
        }
        else 
        {
            $result = mysql_query("SELECT * FROM articles", $con);
            $numrows = mysql_num_rows($result);
            echo "DB connection OK <br/>";
            echo "Found ";
            echo $numrows;
            echo " records <br/><br/>";
        } // EVERYTHING WORKS UP TO HERE
    ?> 
    
    <script type="text/javascript">
        document.write("THIS IS THE FISRT JS DOING SOMETHING"); // THIS DOES NOTHING
        numrows = <?php echo $numrows; ?>; // THIS DOES NOTHING
        var hhgdata = new Array(numrows); // THIS DOES NOTHING
        document.write("Records = " + numrows + "<br/>"); // THIS DOES NOTHING
    </script>
    
    <?php
        $counter = 1;
        while ($row = mysql_fetch_assoc($result))
        {
            echo $row["idimg"]; echo "<br/>";  //THIS WORKS
            $hhgtitle = $row["hhgtitle"]; //THIS WORKS
            echo $hhgtitle; echo "<br/>"; //THIS WORKS
    ?>
    <script type="text/javascript"> //THIS WORKS
        var counter = <?php echo $counter; ?>; //THIS WORKS
        document.write("counter = " + counter); //THIS WORKS
        hhgtitle = <?php echo '"' . $hhgtitle . '"'; ?>; // THIS DOES NOTHING
        document.write("Title: "); // THIS DOES NOTHING
        hhgdata[counter] = [];
        hhgdata[counter][1]= hhgtitle; // THIS DOES NOTHING
        document.write("<br />hhgdata[counter][1]: " + hhgdata[counter][1]); // THIS DOES NOTHING
    </script>
    <?php
            $counter++; // THIS WORKS
        }
    ?>
    
    </body>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to extract all data from a file with one regex expression.
I am attempting to use JSONP to return an array from PHP to JavaScript.
I am attempting to get the information from one table (games) and count the
I have been attempting to get log4net logging in my asp.net web application with
I am attempting to get some information from a website, the info that I
I am attempting to get all records where and Id field exists more than
I'm attempting to create a SQLite db and populate it with data initially. When
Using php coding, I'm attempting to make a script which will grab content from
I have not done much database programming at all. I am working from some
I am attempting to create a small application to collect data received from an

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.