如何使用mysqli在不同页面上使用链接提取数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用mysqli在不同页面上使用链接提取数据相关的知识,希望对你有一定的参考价值。

我找到了链接到页面的方法,并设置了我想要调用的ID:

<a href="page.php?id=10">CLICK TEST</a> **(IS THIS RIGHT?)**

但后来我需要page.php来拉取id,这就是我现在用来手动拉取id的内容。我如何将以下代码从链接中拉出来?

<?php
$query = "select * from Drinklist where id = 10";

$result = mysqli_query($conn,$query);

while($Drinklist = mysqli_fetch_array($result)){
        echo "<head>";
        echo "<title>".$List['name']." - Site Name</title>";
}
?>

我尝试了以下(不起作用):

$query = "select * from List where id = . $id";

好像我只能找到使用MYSQL而不是MYSQLI的方式...任何帮助将不胜感激。

更新的代码:

<?php
$query = "select * from Drinklist where id = ?";
$result = mysqli_prepare($conn,$query);
mysqli_stmt_bind_param($result, 'i', $_GET['id']);
mysqli_stmt_execute($result);
while($Drinklist = mysqli_fetch_array($result)){
        echo "<head>";
        echo "<title>".$Drinklist['name']." - Mixed Drinks Station</title>";
}
?>

得到错误:

警告:mysqli_fetch_array()要求参数1为mysqli_result,第6行的public_html / page.com / test / inc / drink-page.php中给出的对象

答案

好的,所以我最终通过大量的反复试验来解决这个问题......

感谢chris85的早期支持,希望这可以帮助你一点点。这很好玩 ;)

<?php
$server = "localhost";
$user = "user";
$pass = "password";
$dbname = "database";

//Creating connection for mysqli

$conn = new mysqli($server, $user, $pass, $dbname);

//Checking connection

if($conn->connect_error){
 die("Connection failed:" . $conn->connect_error);
}

$article_id = $_GET['id'];

if( ! is_numeric($article_id) )
  die("Looks like you are lost!  <a href='#'>Back to Home</a> ");

$query = "SELECT * FROM `Whatever` WHERE `ID` =$article_id LIMIT 0 , 30";

$info = mysqli_query($conn,$query);

while($row = mysqli_fetch_array($info, MYSQL_ASSOC))
{

    $name = $Whatever['name'];
    $description = $Whatever['description'];
    $keywords = $Whatever['keywords'];
    $lrgpic = $Whatever['lrgpic'];
    $vid = $Whatever['vid'];

    $name = htmlspecialchars($row['name'],ENT_QUOTES);
    $description = htmlspecialchars($row['description'],ENT_QUOTES);
    $keywords = htmlspecialchars($row['keywords'],ENT_QUOTES);
    $lrgpic = htmlspecialchars($row['lrgpic'],ENT_QUOTES);
    $vid = $row['vid']; //Use <-- to be able to have HTML in your database otherwise the above would remove <, >, /, ', ", ect. 

        echo "<head>";
        echo "<title>$name - Site title</title>";
        echo "<meta name='description' content='$description'>";
        echo "<meta name='keywords' content='$keywords'>";
        include 'inc/head.php'; //includes already are in a php file ;)
        echo "</head>";
        echo "<body>";
        include 'inc/header.php';
        include 'inc/nav.php';
        echo "<div class='wrapper'>";
        echo "<div id='drink-name'>";
        echo "<h2>$name</h2>";
        echo "</div>";
        // AND SO ON
    }

?>

以上是关于如何使用mysqli在不同页面上使用链接提取数据的主要内容,如果未能解决你的问题,请参考以下文章

创建交互式数据表以从 MySQLi 数据库中提取数据 [关闭]

mysqli_fetch_array 未更新

如何从网页的不同超链接中提取数据

在Scrapy中递归折叠二级链接

如何使用汤从页面中提取列中的数据

小程序如何获取链接