javascript - Saving php data from mysql_fetch_array as script variables

113

been trying to save my data from my while loop to script variables but no success. Did an example if how I want to save my php data to script variables. This doesn't work for me. Anyone have any idea? Don't want to save all data manually. Very greatful for answers! Ask if you don't understand :) Right now it only saves the last variable in the row2.

$id = 0;
while($rows = mysql_fetch_array($data)){
$id = $id + 1;

$data = $rows['data'];

    $id2 = 0; 
    $sql = mysql_query("select * from table where id = '".$data."'");
    while($row2 = mysql_fetch_array($sql)){

    $id2 = $id2 + 1;
    $var = $row2['var']; //lets say this one is HT123
    }

$savethis = "var data" . $id . "_" . $id2 . " = '" . $var . "';"; 
}

echo "<script>";

   echo $savethis;

   //I want it to equal like "var data1_1 = 'HT123';
   //And then do this for each row, so:
   //var data1_2 = 'HW132';
   //var data1_3 = 'PQ542';
   //var data2_1 = 'SA210';
   //Etc

echo "</script>";
719

Answer

Solution:

I'd really recommend using PDO as mysql_fetch_array() will be deprecated in the near future.

Having that in mind, if you could do it with PDO (here is where you can learn to set up a PDO connection), I think this might work (just working off the top of my head as I don't have your data to work with:

// code to set up a PDO connection
$sql = "SELECT * FROM first_table";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$first_results = $stmt->fetchAll(PDO::FETCH_OBJ);

$result_array = array();

foreach ($first_results AS $first) {

    $newSQL = "SELECT * FROM second_table WHERE id = '{$first->id}'";
    $newStmt = $pdo->prepare($newSQL);
    $newStmt->execute();
    $second_results = $newStmt->fetchAll(PDO::FETCH_OBJ);

    // append
    $result_array[$first->id][$second_results->id] = $second_results->var; 
}

return json_encode($result_array);

Again, that is just a rough idea and might have kinks in it. But that should give you nested arrays keyed by the ids. So it might look (hopefully) like this:

[0]
  [0] => "var", (this would be equivalent to data0_0 = var)
  [1] => "var2"
[1]
  [0] => "foo"

You can have them interact by using AJAX to send the request to your PHP file, then you can expect the JSON array as the response.

278

Answer

Solution:

you can use json_encode to do the dirt work for you!

echo json_encode($data);

this will encode whatever you have ti json format(no matter array or object or variable)

258

Answer

Solution:

You need to place$varin Apostroff. Without it you will have error in js code.

$savethis = "var data" . $id . "_" . $id2 . " = '" . $var . "';"; 
344

Answer

Solution:

done this stuff before. just gonna give you a rough idea on how will this work.

add this before "while" then increment after while loop:

    $x=1;

set $var to array and count its value after while loop:

    $var[$x] = $row2['var'];

code will then look like this:

    $x=1;
    while($row2=mysqli_fetch_array($sql) )
    {
        $var[$x] = $row2['var'];
        $x++;
    }
    $array_length=count($var);

loop "script":

    $id=1;
    $id2=1;
    echo "<script>";
    for($i=1; $i<=$array_length; $i++)
    {
        $savethis = "var data" . $id++ . "_" . $id2++ . " = '" . $var[$i] . "';";
        echo $savethis;
    }
    echo "</script>";

AND THIS IS HOW YOUR COMPLETE CODE WILL LOOK LIKE:

    $x=1;
    while($row2=mysqli_fetch_array($sql) )
    {
        $var[$x] = $row2['var'];
        $x++;
    }
    $array_length=count($var);

    $id=1;
    $id2=1;
    echo "<script>";
    for($i=1; $i<=$array_length; $i++)
    {
        $savethis = "var data" . $id++ . "_" . $id2++ . " = '" . $var[$i] . "';";
        echo $savethis;
    }
    echo "</script>";

core solution revolves around array. hope this helps :)

People are also looking for solutions to the problem: php - move symfony from root directory to public_html

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.