将 PHP MySQL 查询输出放入 SSH

发布于 2024-12-05 02:41:36 字数 611 浏览 1 评论 0原文

我的程序最初通过 http 调用 mySQL 查询。结果以 JSON 形式返回:

 $query  = "SELECT xaxis, yaxis FROM table ";
$result = mysql_query($query);

$rows = array();
$counter=1;

while ($r = mysql_fetch_array($result, MYSQL_NUM))
{

     echo json_encode($r[0]), "\n";
     echo json_encode($r[1]), "\n";

    $counter++;

} 


?>  

它工作正常(当然不是世界上最好的 PHP 脚本),但现在我在 SSH 中完成所有操作,我正在考虑通过 bash 进行 SQL 查询,然后我想,为什么不这样做我不是只是在 SSH 中调用 PHP 脚本吗?

但是我不知道如何做到这一点,因为 PHP 返回一个数组。如果它返回一个简单的文件,理论上我可以这样做:

spc username@hostname:filename  localfilename

我应该如何从 SSH 中的脚本返回获取数组?

My program originally called a mySQL query over http. The result was returned in JSON form:

 $query  = "SELECT xaxis, yaxis FROM table ";
$result = mysql_query($query);

$rows = array();
$counter=1;

while ($r = mysql_fetch_array($result, MYSQL_NUM))
{

     echo json_encode($r[0]), "\n";
     echo json_encode($r[1]), "\n";

    $counter++;

} 


?>  

It works ok (granted not the best PHP script in the world), but now I'm doing everything in SSH and I was thinking about doing the SQL query through bash and then I thought, why don't I just call the PHP script in SSH?

However I'm not sure how to do this, given that the PHP returns an array. If it returned a simple file I could, in theory just do:

spc username@hostname:filename  localfilename

How should I return get an array from a script in SSH?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

够运 2024-12-12 02:41:36

PHP 不返回数组。它返回一个 JSON 格式的字符串:记住,HTTP 只能给您文本

所以没有区别。只需在终端中调用 PHP 脚本,您就会看到相同的文本

(实际上,看起来你有两个不同的 JSON 结构,用换行符分隔。你可能想通过编码 $r 来解决这个问题:)

[user@host: ~]$ cat myscript.php
<?php
$query  = "SELECT `xaxis`, `yaxis` FROM `table`";
$result = mysql_query($query);

$rows = Array();
while ($r = mysql_fetch_assoc($result)) {
     $rows[] = $r;
}

echo json_encode($rows);
?>

[user@host: ~]$ php myscript.php
[
   { "xaxis" : 0, "yaxis" : 0 },
   { "xaxis" : 3, "yaxis" : 2 },
   { "xaxis" : 8, "yaxis" : 5 }
]

或者类似的东西。

The PHP does not return an array. It returns a JSON-format string: remember, HTTP can only ever give you text.

So there is no difference. Just invoke the PHP script in your terminal and you'll see the same text.

(Actually, it looks like you have two distinct JSON structures, separated by a newline. You might want to fix that by encoding $r instead:)

[user@host: ~]$ cat myscript.php
<?php
$query  = "SELECT `xaxis`, `yaxis` FROM `table`";
$result = mysql_query($query);

$rows = Array();
while ($r = mysql_fetch_assoc($result)) {
     $rows[] = $r;
}

echo json_encode($rows);
?>

[user@host: ~]$ php myscript.php
[
   { "xaxis" : 0, "yaxis" : 0 },
   { "xaxis" : 3, "yaxis" : 2 },
   { "xaxis" : 8, "yaxis" : 5 }
]

Or something like that.

冷弦 2024-12-12 02:41:36

1) 如果允许用户从外部服务器连接到 MySQL,则可以在所需的服务器上运行 PHP 脚本。这样你就不必传输文件或任何东西(看到你写了“spc”,而我猜它应该是“scp”。2

)如果你希望它输出一个单一的,请将 while 循环更新为以下代码array:

$output = array();

while ($r = mysql_fetch_array($result, MYSQL_NUM))
{
    $output[] = $r;
    $counter++; //Im guessing you need this :P
}

echo json_encode($output);

可以从任何地方调用此脚本,但如果您想从 SSH 运行它,您只需运行以下命令*:

php yourfile.php

*确保安装了 php-cli ;)

希望这对您有帮助;)

1) You could just run the PHP script on the server you want if you allow that user to connect to MySQL from an external server. That way you wont have to transfer the file or anything (Seeing that you wrote "spc" while I'm guessing it's supossed to be "scp".

2) Update the while loop to the following code if you want it to output a single array:

$output = array();

while ($r = mysql_fetch_array($result, MYSQL_NUM))
{
    $output[] = $r;
    $counter++; //Im guessing you need this :P
}

echo json_encode($output);

This script can be called from everywhere but if you want to run it from SSH you can simply run the following command*:

php yourfile.php

*Make sure that php-cli is installed ;)

Hope this helped you out ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文