用php从mysql表中拉出一个字段

时间:2015-07-24 09:30:14

标签: php mysql mysqli

这是我的完整页面代码:

<?php


$servername = "localhost";
$username = "root";
$password = "null";
$dbname = "logs";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 


$result = mysql_query("SELECT * FROM 'd82Kap3'");
$row = mysql_fetch_array($result);
echo $row['ip'];

?>

这是我的表结构:http://prntscr.com/7we7ck

我试图让它回显字段中的所有记录&#34; ip&#34;。

3 个答案:

答案 0 :(得分:2)

使用while循环。

示例

$result = mysql_query("SELECT * FROM 'd82Kap3'");
while($row = mysql_fetch_array($result))
{
     echo $row['ip']."<br />";
}

注意

自PHP 5.5.0起,

mysql扩展名已弃用,将来将被删除。相反,应该使用MySQLi或PDO_MySQL扩展。

<强>更新

<?php

$servername = "localhost";
$username = "root";
$password = "null";
$dbname = "logs";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 


$result = $conn->query("SELECT * FROM `d82Kap3`");

while($row = $result->fetch_array())
{
     echo $row['ip']."<br />";
}

?>

答案 1 :(得分:0)

不要混用mysqlmysqli

<?php


$servername = "localhost";
$username = "root";
$password = "null";
$dbname = "logs";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 


$result = $conn->query("SELECT * FROM d82Kap3");
while($row = $result->fetch_assoc())
echo $row['ip'];

?>

答案 2 :(得分:0)

使用以下代码:

$result = mysql_query("SELECT * FROM 'd82Kap3'");
while($row = mysql_fetch_array($result))    
{
 echo $row['ip'];
}