简体   繁体   English

PHP 显示来自 MySQL 数据库的数据

[英]PHP Displaying data from MySQL database

I wanted to display a page content with PHP and MySQL.我想用 PHP 和 MySQL 显示页面内容。 But i don't know how to select and display data from PHP.但我不知道如何从 PHP 中选择和显示数据。

$name = $_GET['title'];
$query = "SELECT * FROM pages WHERE name = $name";
$result = mysql_query("$query");

But i don't know how to display data.但我不知道如何显示数据。 I want to get the string value from content in sql table row where name = $name and display it.我想从 sql 表行中name = $name content中获取字符串值并显示它。

If you can, please help me如果可以,请帮助我

You may try and include this in your code:您可以尝试将其包含在您的代码中:

$name = mysqli_real_escape_string($_GET['title']);
$query = "SELECT * FROM pages WHERE name = $name";
$result = mysqli_query($link, $query);

while ($row = mysqli_fetch_array($result)){
    echo $row['content'];
}

mysqli_free_result($result);

Here I have assumed $link as the handle to connect to the database.这里我假设$link作为连接到数据库的句柄。

NB: You may consider passing the $_GET values through mysqli_real_escape_string() to avoid sql injections which may prove fatal to the database and its tables.注意:您可以考虑通过mysqli_real_escape_string()传递$_GET值以避免 sql 注入,这可能对数据库及其表造成致命影响。 You also need to consider the usage of mysqli_* functions because mysql_* functions are deprecated and will be discontinued.您还需要考虑mysqli_*函数的使用,因为mysql_*函数已被弃用并将停止使用。

You have error in your sql, change您的 sql 中有错误,请更改

$query = "SELECT * FROM pages WHERE name = $name";
$result = mysql_query("$query");

to

$query = "SELECT * FROM pages WHERE name = '".$name."'";  // as name is char it should be enclosed in quotes
$result = mysql_query($query);   // using quotes inside this will just display it without executing the query

You can fetch the results by this (if the result is only single record):你可以通过这个获取结果(如果结果只有单条记录):

$row=mysql_fetch_array($result);   // fetch the result as an array with subscript as the field name

echo $row['content'];   // echo the value of the field content

If the query result contains multiple records then you have to do this inside a while loop like this:如果查询结果包含多条记录,那么您必须在 while 循环中执行此操作,如下所示:

while($row=mysql_fetch_array($result))   // fetch the result as an array with subscript as the field name
{
    echo $row['content'];   // echo the value of the field content
}
$query = mysql_query("SELECT * FROM table WHERE name = $name");
$result = mysql_fetch_array($query);

//you'll get value in var
$var=$result['content'];

you have to do like this你必须这样做

$name = $_GET['title'];
$query = "SELECT * FROM pages WHERE name = '".$name."'";
$result = mysql_query($query);

//get values returned from query
$row=mysql_fetch_array($result);

//display required content
echo $row['content'];

Also mysql_* function are deprecated.You have to stop using this.不推荐使用mysql_*函数。您必须停止使用它。 Start using PDO or prepared statements or mysqli_* function开始使用PDOprepared statementsmysqli_*函数

First of all the mysql_ is deprecated, use mysqli_首先将mysql_已过时,使用mysqli_

<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query = "SELECT Name, CountryCode FROM City ORDER by ID LIMIT 3";
$result = mysqli_query($link, $query);

/* numeric array */
$row = mysqli_fetch_array($result, MYSQLI_NUM);
printf ("%s (%s)\n", $row[0], $row[1]);

/* associative array */
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);

/* associative and numeric array */
$row = mysqli_fetch_array($result, MYSQLI_BOTH);
printf ("%s (%s)\n", $row[0], $row["CountryCode"]);

/* free result set */
mysqli_free_result($result);

/* close connection */
mysqli_close($link);
?>

Source: http://www.php.net/manual/en/mysqli-result.fetch-array.php来源: http : //www.php.net/manual/en/mysqli-result.fetch-array.php

You can do this using Prepared Statements :您可以使用 Prepared Statements 做到这一点:

$query = "SELECT whatever1, whatever2 FROM pages WHERE name = ?";

$stmt = $connection->prepare($query);
$stmt->bind_param("s", $name);
$stmt->execute();
$stmt->bind_result($value_you_want, $value_you_want_as_well);
while($stmt->fetch()){
echo $value_you_want . $value_you_want_as_well;
}
$stmt->close();

Or you can do this using PDO :或者您可以使用 PDO 执行此操作:

$query = "SELECT whatever1, whatever2 FROM pages WHERE name = :name";

$stmt = $connection->prepare($query);
$stmt->bindParam(':name', $name, PDO::PARAM_STR, 20);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
foreach ($result as $page) {
    echo $page->whatever1 . $page->whatever2;
}
$stmt = null; // Set to null to destroy connection

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

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