I am trying to access a mysql server on a remote machine using php. I tried viewing data in table, I am using below php script. Please let me know if I am missing something.
<?php
$conn = mysql_connect("192.168.1.1","root","password");
$db = mysql_select_db("charan", $conn);
?>
<?php
echo "<ul>";
$sql = "select * from arista";
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
echo "<li>Username:$row[0]</li><li>DOB:</li><br/>";
}
?>
For PHP >= 5.5 you will need to use MySQLi connector.
// this connect with the database
$con = new mysqli("192.168.1.1", "root", "password", "charan");
// here is the SQL query
$sql_query= "select * from arista";
// now execute the query
$result = $con->query($sql_query);
echo "<ul>";
while($row = $result->fetch_assoc()) {
echo "<li>Username:" . $row['username'] . "</li><li>DOB:</li><br/>";
}
echo "</ul>";
It may works.
[Note: Please stop using mysql* functions
.]
Use mysqli* functions
or Prepared statement
. This way it is impossible for an attacker to inject malicious SQL.
Using procedural style:
<?php
$conn = mysqli_connect("92.168.1.1","root","password","charan");
echo "<ul>";
$query = mysqli_query($link, "select * from arista");
while ($row = mysqli_fetch_array($query, MYSQLI_NUM))
{
echo "<li>Username:".$row[0]."</li><li>DOB:</li><br/>";
}
echo "</ul>";
?>
Using object oriented style:
<?
$conn = new mysqli("192.168.1.1", "root", "password", "charan");
echo "<ul>";
$result = $conn->query("select * from arista");
while($row = $result->fetch_assoc()) {
echo "<li>Username:" . $row['username'] . "</li><li>DOB:</li><br/>";
}
echo "</ul>";
?>
Final code that worked for me. After I did $apt-get install php5-mysql
<?php
error_reporting(E_ALL);
ini_set('display_errors' , 1);
$conn = mysqli_connect("192.168.1.1","root","password","charan");
$sql_query = "select * from Boidata";
$result = $conn->query($sql_query);
echo "<ul>";
while ($row = $result->fetch_assoc())
{ echo "<li>Username:" . $row['Username'] . "</li><li>DOB:" .$row['DOB'] ."</li><br/>";}
echo "</ul>";
?>