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

问题描述 投票:2回答:1

我找到了链接到页面的方法,并设置了我想要调用的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中给出的对象

php mysql mysqli
1个回答
0
投票

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

感谢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
    }

?>
© www.soinside.com 2019 - 2024. All rights reserved.