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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:14:21+00:00 2026-06-13T13:14:21+00:00

i m trying to convert php mysql_fetch_row result into javascript array so i write

  • 0

i m trying to convert php mysql_fetch_row result into javascript array so i write below code to do that. but i got problem , if there are multiple row in result, then only last row stored in javascript array

<script>
function sub()
{
alert("coming");
var a= new Array;
<?php
$dbhost ='localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'pupilcafe';
$conn = mysql_connect($dbhost,$dbuser,$dbpass) or die ('Error Connecting to mysql');
mysql_select_db($dbname,$conn);
$uid=1;
$semester='1stSemester';    
$cid=1;
$qry="select * from subject where cid='$cid'&& uid='$uid'&& semester='$semester'";
$res = mysql_query($qry);
while($data=mysql_fetch_row($res))
{
$i=0;
echo "a[$i]='".$data[1]."';";
$i++;
}
?>
alert(a[0]);
}
</script>

so here mysql code fetch two row frm db (Englishphp) but in result i got only php .. i want both of this row in javascript array
so how to do that?

  • 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-13T13:14:22+00:00Added an answer on June 13, 2026 at 1:14 pm

    Don’t concoct Javascript syntax yourself. Use json_encode:

    $a = array();
    while ($data = mysql_fetch_row($res)) {
        $a[] = $data[1];
    }
    
    ?>
    
    <script>
        var a = <?php echo json_encode($a); ?>;
    </script>
    

    Also, mysql_ functions are deprecated. Don’t use them for new code. Use mysqli or PDO.

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

Sidebar

Related Questions

I'm trying to convert my newsfeed result into an array, so that I can
I've been trying to convert a PHP code to Java, but its not working
I am trying to convert following table data into nested array using PHP. I
I'm trying to change some php code so that i get my values from
I am trying to convert a php/mysql generated table into a downloadable csv file.
I'm trying to convert some PHP code from Mysqli to PDO, and I need
I am trying to convert a pre-existing site that had html and php intermingled
I'm trying to convert jquery actions into variables that would eventually be sent off
Am trying to pass an array of values from PHP function to Javascript. Not
I'm trying to convert my procedural code to oop. <?php $dbc = get_dbc(); $info

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.