mysql查询中的php变量

时间:2021-04-10 00:08:24

I need to call a url using php variables stored inside a mysql table column. For example:

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

In Mysql:

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

Now, in php, i'll call this table like:

现在,在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']

It shows:

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

I want it to show:

我想要它显示:

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

What should I change in MySQL table to get the above result?

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

2 个解决方案

#1


1  

You need to substitute the variables in your URL (from MySQL) with the php variables set in your code.

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

Change the line:

换行:

ECHO $result_url = $res['url']

to something like this:

这样的事情:

$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;

#2


0  

I would work with sprintf in this case:

在这种情况下我会使用sprintf:

Save it in db like:

将其保存在db中:

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

And replace it later:

并在以后替换它:

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

#1


1  

You need to substitute the variables in your URL (from MySQL) with the php variables set in your code.

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

Change the line:

换行:

ECHO $result_url = $res['url']

to something like this:

这样的事情:

$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;

#2


0  

I would work with sprintf in this case:

在这种情况下我会使用sprintf:

Save it in db like:

将其保存在db中:

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

And replace it later:

并在以后替换它:

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