在PHP中使用echo发送URL中的变量

在PHP中使用echo发送URL中的变量

本文介绍了在PHP中使用echo发送URL中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法通过Google搜索找到正确的方法来逃避URL中的变量,因此我找不到真正的指导原则.基本上,我是从表中的MySQL查询中打印出一堆结果,并且我希望每一行中的一项是指向该结果页面的链接.我认为这很容易,因为我只是在某个地方缺少撇号或反斜线,但我无法弄清楚.这是导致错误的行:

I can't really find good guidelines through Google searches of the proper way to escape variables in URLs. Basically I am printing out a bunch of results from a MySQL query in a table, and I want one of the entries in each row to be a link to that result's page. I think this is easy, that I'm just missing a apostrophe or backslash somewhere, but I can't figure it out. Here's the line that's causing the error:

echo "<a href = \"movies.php/?movie_id='$row['movie_id']'\"> Who Owns It? </a> ";

这是我得到的错误:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

如何解决此错误?

此外,在URL中使用回显和变量的一些通用准则是什么?

In addition, what are some general guidelines for working with echo and variables in URLs?

推荐答案

echo "<a href = \"movies.php/?movie_id='$row['movie_id']'\"> Who Owns It? </a> ";

这里有两件事需要更改.一件事,在movies.php之后不应有一个/.第二个是url变量周围没有撇号.应该是movie_id = $ row ['movie_id'].每当我使用php变量时,我通常会将其括起来而不是将其嵌入引号中.所以最后我会做这样的事情:

There are two things here that should be changed. For one thing there shouldn't be a / after movies.php. The second is that there aren't any apostrophies around url variables. It should be movie_id=$row['movie_id']. Whenever I use a php variable I usually concatonate it instead of embed it in the quotations. So in the end I'd do something like this:

echo "<a href=\"movies.php?movie_id=" . $row['movie_id'] . "\"> Who Owns It? </a>";

这篇关于在PHP中使用echo发送URL中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 13:14