-3

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

karthic
  • 175
  • 1
  • 1
  • 9
  • Does the user admin have permissions for the database and tables you're trying to access? – andrewsi Sep 20 '12 at 19:09
  • 1
    You say that your user name is 'admin', but you're trying to connect as 'root'. Also, the IP address doesn't match what you've shown. Finally, it would be impossible to "get any row" because you're not selecting any. – Alain Collins Sep 20 '12 at 19:11
  • Where do you try getting a row from the database? – Conrad Lotz Sep 20 '12 at 19:11
  • Can you please post the code you're using to display the data? – Max Sep 20 '12 at 19:11
  • hi all thank you very much here there is a slight change just now i checked my config.inc.php file there host was localhost user was root and there is no password i modified my file accordingly but still i am not getting the row please help me – karthic Sep 20 '12 at 19:15
  • Are you getting any error message? Because the code, as posted, should work. Show us the code you're ACTUALLY using – Marc B Sep 20 '12 at 19:15

3 Answers3

1

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.

JvdBerg
  • 21,777
  • 8
  • 38
  • 55
0

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);

Max
  • 2,529
  • 1
  • 18
  • 29
0

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

karthic
  • 175
  • 1
  • 1
  • 9