2013-05-17 10 views
0

我的问题是,如何使用url栏中的行ID来定位和回显特定行的内容。 Example: Row 4: name: bob email: [email protected]当有人访问让我们举一个例子:website.com/123.php?=4(不一定非常像这样)它的回声名称:bob电子邮件:[email protected]。如果您需要了解更多,请询问。用链接和Id调用行

More of an example: 
Row: 9999 
name: smith 
email: [email protected] 
password: smith123 

用户访问www.website.com/?9999www.website.com/123.php?=9999(或任何类型的链接 - 不具有行号/ ID多数民众赞成的链接),然后回声例如在页面上: 感谢您访问的页面。与他联系,谢谢。 基本上我希望能够通过在url栏中使用它的行号来调用某一行的信息。

+1

而不是使用www.website.com/123。 php?= 9999,请使用www.website.com/123.php?rowid=9999。然后在php中使用$ _GET ['rowid']函数来获得该ID为 –

+0

Rmemeber,“数据库中的行”*没有内在的顺序。“如果你想搜索”id = 4“...那么你的表需要有一个名为“id”的列,其中一个(并且只有一个)行的值应该是“4”,这称为“主键”。相应的SQL查询将类似于'select * from mytable其中id = 4;'。 – paulsm4

回答

0

简单地查询字符串:

www.url.com/page.php?id=121 

获得ID与$ _GET

if (isset($_GET['id'])) 
{ 


// fetch and display the information with  database Query 

$con=mysqli_connect("example.com","peter","abc123","my_db"); 
// Check connection 
if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 

$result = mysqli_query($con,"SELECT * FROM Persons where id = " . $_GET['id']); 

while($row = mysqli_fetch_array($result)) 
    { 
    echo $row['name'] . " " . $row['email']; 
    echo "<br>"; 
    } 

mysqli_close($con); 



} 
+0

非常感谢你! – TheCod3r

+0

你至少应该将id转换为int来防止注入 – Orangepill

1

使用$_GET[]方法来从URL中获取价值,后来从数据库读取该行,例如

www.domain.com/page.php?id=4 

所以现在page.php使用这段代码

if(!empty($_GET['id'])) { 
    $id = $_GET['id']; //Don't Forget to sanitize it before using it in your query 
    //Check whether id exists like 
    $is_valid_id = mysqli_query($connection, "SELECT id FROM table_name WHERE id = $id"); 
    if(mysqli_num_rows($is_valid_id) != 1) { 
     //Redirect 
    } 
} else { 
    //Redirect or throw some error 
} 

现在使用此ID使用查询获取一行

$query = mysqli_fetch_array(mysqli_query($connection, "SELECT * FROM table_name 
                 WHERE id = $id")); 

现在y OU可以轻松echo出来的数据,如

/* Assuming first_name and last_name as column names */ 
echo $query['first_name']; //Bob 
echo $query['last_name']; //Doe 
/* And so on... */