简体   繁体   English

执行“ php -r” mysql_connect()不返回链接资源

[英]executing “php -r” mysql_connect() not returning link resource

I've been embedding some php in my shell scripts. 我已经在我的shell脚本中嵌入了一些php。 Stuff like this works: 像这样的东西:

SUBJECT_URL= php -r "echo rawurlencode('$SUBJECT');" SUBJECT_URL = php -r "echo rawurlencode('$SUBJECT');"

and sets my string variable SUBJECT_URL to the converted value of $SUBJECT 并将我的字符串变量SUBJECT_URL设置为$ SUBJECT的转换值

However, 然而,

I am trying to use this command: 我正在尝试使用以下命令:

link= php -r "mysql_connect('localhost', 'root', 'mypassword');" link = php -r "mysql_connect('localhost', 'root', 'mypassword');"

To test my connection to the SQL database. 测试我与SQL数据库的连接。 Unfortunately, the value for "link" is not getting set. 不幸的是,“链接”的值没有设置。 Therefore I can't test it for success and can't issue the: 因此,我无法对其进行测试,也无法发出以下命令:

php -r "mysql_close($link);" php -r“ mysql_close($ link);”

to release the resource. 释放资源。

Any help would be appreciated. 任何帮助,将不胜感激。

This is because mysql connection dies with php. 这是因为mysql连接与php断开。 Now if you really cannot just connect to mysql without using php from where you type, you could use something like : 现在,如果您真的不能不从输入的地方使用php而直接连接到mysql,则可以使用以下命令:

php -r "echo @mysql_connect('localhost', 'root', 'mypassword')? 'connected' : 'not connected';"

You don't need to release the connection as php will do it for you when it exits. 您不需要释放连接,因为php在退出时会为您完成连接。

Using php -r is almost certainly the wrong approach; 几乎可以肯定,使用php -r是错误的方法。 if you need to use PHP you'd be better off writing the whole script in PHP instead of a mix of Shell as well. 如果您需要使用PHP,则最好用PHP编写整个脚本,而不要同时使用Shell。

The reasons link=php -r "mysql_connect('localhost', 'root', 'mypassword');" 原因link=php -r "mysql_connect('localhost', 'root', 'mypassword');" doesn't work is that (a) mysql_connect returns a resource to the database connection, which is not something that can be coerced into a string, and (b) your one-line PHP script ( mysql_connect('localhost', 'root', 'mypassword'); doesn't echo that resource anyway, ie it doesn't even attempt to coerce the resource into a string. 不起作用是(a) mysql_connect将资源返回到数据库连接,该资源不能强制转换为字符串,并且(b)单行PHP脚本( mysql_connect('localhost', 'root', 'mypassword');无论如何都不echo该资源,即它甚至没有尝试将资源强制转换为字符串。

The mysql_connect function returns false on failure, so if you want your link variable to indicate success or failure you can assign a string to it accordingly. mysql_connect函数在失败时返回false ,因此,如果您希望link变量指示成功或失败,则可以为其分配一个字符串。 In a one-liner the ternary operator is a simple way to do that, as fab2s noted in his answer ( echo @mysql_connect('localhost', 'root', 'mypassword')? 'connected' : 'not connected' ). 单线模式中, 三元运算符是实现此目的的一种简单方法,如fab2s在其答案中指出的echo @mysql_connect('localhost', 'root', 'mypassword')? 'connected' : 'not connected' )。 In that code the @ (silence operator) prevents a failed connection from raising a Warning from PHP, and the use of the ternary operator conditionally returns "connected" if the call returned something "truthy" and "not connected" if it returned false . 在该代码中, @ (沉默运算符)可防止连接失败导致PHP发出警告,如果调用返回的是“ truthy”,则使用三元运算符有条件地返回"connected"如果返回false则使用"not connected"

As an aside, note that you really shouldn't use the old MySQL extension any longer. 顺便说一句,请注意,您真的不应该再使用旧的MySQL扩展。 In fact, it's generally best to use PDO: 实际上,通常最好使用PDO:

<?php
$dbConn = new \PDO(
    'mysql:dbname=my_db;host=localhost;charset=utf8',
    'user',
    'password',
    [PDO::ATTR_ERRMODE => PDO::ERR_EXCEPTION]
);

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

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