如何从数据库中获取数据并在PHP中显示?[英] How to fetch data from database and display it in PHP?

本文是小编为大家收集整理的关于如何从数据库中获取数据并在PHP中显示?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

如何使用PHP从数据库中获取数据并显示?

数据库表具有列,标记为ID&Number. ID是唯一且固定的,而数字只是一个非唯一的数字.如果有人访问 http://example.com/show.php?ID=32和show.php应该获取Number&display "Your number is XXX"

请提供代码样本.

推荐答案

<?php
//Connect to DB
$db = mysql_connect("localhst","user","pass") or die("Database Error");
mysql_select_db("db_name",$db);

//Get ID from request
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

//Check id is valid
if($id > 0)
{
    //Query the DB
    $resource = mysql_query("SELECT * FROM table WHERE id = " . $id);
    if($resource === false)
    {
        die("Database Error");
    }

    if(mysql_num_rows($resource) == 0)
    {
        die("No User Exists");
    }

    $user = mysql_fetch_assoc($resource);

    echo "Hello User, your number is" . $user['number'];
}

这是非常基本的,但应该看到您.

其他推荐答案

首先获取用户的ID(可以在访问时给出或根据访问时给出的详细信息给出)

然后将"选择查询"写入包含"数字"字段的表格.例如

SELECT number FROM table1 WHERE table1.ID=IDFromtheuser;

其他推荐答案

尝试

SELECT number from numberTable nt
JOIN idTable it ON  it.ID = nt.ID
WHERE it.ID = `your given id`

我认为您的两个表都用id

引用

本文地址:https://www.itbaoku.cn/post/597337.html

问题描述

How do I get data from a database using php and show it?

The database table has columns, labeled as ID & Number. ID is unique & fixed whereas Number is just a non-unique number. If someone visits http://example.com/show.php?ID=32, and show.php should fetch the Number & display "Your number is XXX”

Please provide the code-samples.

推荐答案

<?php
//Connect to DB
$db = mysql_connect("localhst","user","pass") or die("Database Error");
mysql_select_db("db_name",$db);

//Get ID from request
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

//Check id is valid
if($id > 0)
{
    //Query the DB
    $resource = mysql_query("SELECT * FROM table WHERE id = " . $id);
    if($resource === false)
    {
        die("Database Error");
    }

    if(mysql_num_rows($resource) == 0)
    {
        die("No User Exists");
    }

    $user = mysql_fetch_assoc($resource);

    echo "Hello User, your number is" . $user['number'];
}

This is very basic but should see you through.

其他推荐答案

First get the id of the user(it may given while visiting or based on the details given while visiting)

Then write select query to the table which contains 'number' field.like

SELECT number FROM table1 WHERE table1.ID=IDFromtheuser;

其他推荐答案

try

SELECT number from numberTable nt
JOIN idTable it ON  it.ID = nt.ID
WHERE it.ID = `your given id`

as i think your both tables are referenced with id