繁体   English   中英

如何使用mysqli在另一个页面上的链接提取数据

[英]How to pull data with a link on a different page with 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
    }

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM