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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6094552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:20:26  来源:igfitidea点击:

How to fetch data from database and display it in PHP?

phpmysqldatabase

提问by lisa

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

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

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.phpshould fetch the Number& display "Your number is XXX”

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

Please provide the code-samples.

请提供代码示例。

回答by Deepika

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

首先获取用户的id(可能是访问时给出的,也可能是访问时给出的详细信息)

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

然后将选择查询写入包含“数字”字段的表中。

SELECT number FROM table1 WHERE table1.ID=IDFromtheuser;

回答by RobertPitt

<?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.

这是非常基本的,但应该能让你看透。

回答by rajiv

<?php
$host="localhost"; 
$username=""; 
$password=""; 
$db_name="multiple_del"; 
$tbl_name="test_mysql"; 

// Connect to server and select databse.
mysql_connect("$host", "root", "")or die("cannot connect");
mysql_select_db("multiple_del")or die("cannot select DB");

$sql="SELECT * FROM test_mysql";


$result=mysql_query($sql);

$count=mysql_num_rows($result);

?>

<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
</tr>

<?php
}
?>

<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>

<?php


if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM test_mysql WHERE id='$del_id'";
$result = mysql_query($sql);
}


}

?>

</table>
</form>
</td>
</tr>
</table>



its my code but its not working and error are show in this code:-

<?php


if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM test_mysql WHERE id='$del_id'";
$result = mysql_query($sql);
}


}

?>

please help me.

回答by diEcho

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

因为我认为你的两个表都引用了 id