Question

I am learning MYSQL. I have copied this code from a learning website.

The original code was

<?php
$conn = @mysql_connect('115.0.0.1','root','root');
if (!$conn) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydb', $conn);
?>

I have modified the following localhost address, username, password and dbname as below code. Actually I am using Appserv as a server.

My Current Code admin is my username admin is my password 115.240.6.251 is my localhost address users is my dbname

this is code where i am getting the users

<?php
include 'conn.php';
$rs = mysql_query('select * from users');
$result = array();
while($row = mysql_fetch_object($rs)){
    array_push($result, $row);
    }
echo json_encode($result);
?>

the above details i am using for appserv

after changed the original code when run the this application i am not getting any row from db

when i checked in sql i have rows for the given table.

Please guide me here.

Thanks in advance Karthic

Was it helpful?

Solution

You need to add something like:


$result = mysql_query("SELECT * FROM your_table");

while($row = mysql_fetch_array($result)) { echo $row['FirstColumnName'] . " " . $row['SecondColumnName']; }

mysql_close($con);

OTHER TIPS

You are using a IP address that needs the mysql server to be setup with allowing remote connections. Many MySQL servers only allow local connections. There are issues with remote connections, like privileges, firewalls and configuration.

I assume you having the PHP and MySQL on the same machine, so you should be connecting to:

$conn = mysql_connect('localhost', 'admin', 'admin');

I suggest you have a look at the examples at php.net You also find some hints there that mysql_* is deprecated and mysqli_* or pdo should be used.

Thank you very much

I got it work it was very simple. When i looked my code. I wrongly added the database name that is the only reason.

I didn't get it into it.

Thank you once again who all helped me.

Regards karthic

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top