user3724476
user3724476

Reputation: 5130

Pulling a field from mysql table with php

This is my full page code:

<?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'];

?>

This is my table structure: http://prntscr.com/7we7ck

I am trying to make it echo all the records in the field "ip".

Upvotes: 2

Views: 49

Answers (3)

Hassaan
Hassaan

Reputation: 7662

Use while loop for it.

Example

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

Note

mysql extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.

Update

<?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 />";
}

?>

Upvotes: 2

Dhinju Divakaran
Dhinju Divakaran

Reputation: 893

Dont mix mysql and 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 = $conn->query("SELECT * FROM d82Kap3");
while($row = $result->fetch_assoc())
echo $row['ip'];

?>

Upvotes: 0

Sagar
Sagar

Reputation: 647

Use following code:

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

Upvotes: 0

Related Questions