簡體   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