繁体   English   中英

mysql查询中的php变量

[英]php variable in a mysql query

我需要使用存储在mysql表列中的php变量来调用url。 例如:

在Mysql中:

Table name:  geturldata
columns:    srno |  url
contents:   1 | http://www.google.com/number=$mobno&user=$username

现在,在php中,我将这样称呼该表:

include (db.php)... 

$mobno = 91123456789;
$username = 'HELLOWORLD';

$QRY = "Select * from TBLNAME where srno=1";
$doqry = mysql_query($QRY);
$res = mysql_fetch_array($doqry);

echo $result_url = $res['url']

表明:

 http://www.google.com/number=$mobno&user=$username

我希望它显示:

http://www.google.com/number=91123456789&user='HELLOWORLD'

我应该在MySQL表中更改什么才能获得上述结果?

您需要用代码中设置的php变量替换URL中的变量(来自MySQL)。

更改行:

ECHO $result_url = $res['url']

像这样:

$result_url = $res['url'];
$result_url = str_replace("$username", urlencode($username), $result_url);
$result_url = str_replace("$mobno", urlencode($mobno), $result_url);
echo $result_url;

在这种情况下,我将使用sprintf:

将其保存在数据库中,如下所示:

http://www.google.com/number=%d&user=%s

并在以后替换:

echo sprintf('http://www.google.com/number=%d&user=%s', $mobno, $username);

暂无
暂无

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

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