简体   繁体   English

将mysql_fetch_array中的php数据另存为脚本变量

[英]Saving php data from mysql_fetch_array as script variables

been trying to save my data from my while loop to script variables but no success. 一直试图将我的数据从我的while循环保存到脚本变量,但没有成功。 Did an example if how I want to save my php data to script variables. 做一个例子,如果我想如何将我的PHP数据保存到脚本变量。 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. 询问您是否不了解:)现在,它仅将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>";

I'd really recommend using PDO as mysql_fetch_array() will be deprecated in the near future . 我真的建议使用PDO,因为mysql_fetch_array()将在不久的将来不推荐使用

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: 考虑到这一点,如果您可以使用PDO进行操作( 在这里可以学习建立PDO连接 ),我认为这可能会起作用(因为我没有数据去做,所以我会费力地工作)使用:

// 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. 但这应该为您提供以ID为键的嵌套数组。 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. 您可以使用AJAX将请求发送到您的PHP文件,使它们进行交互,然后可以将JSON数组作为响应。

you can use json_encode to do the dirt work for you! 您可以使用json_encode为您完成处理工作!

echo json_encode($data);

this will encode whatever you have ti json format(no matter array or object or variable) 这将对您具有的json格式进行编码(无论数组,对象还是变量)

You need to place $var in Apostroff. 您需要将$var放在Apostroff中。 Without it you will have error in js code. 没有它,您将在js代码中出错。

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

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: 在“ while”之前添加它,然后在while循环之后添加:

    $x=1;

set $var to array and count its value after while loop: 将$ var设置为array并在while循环后计算其值:

    $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 :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM