Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
php file is in my system and the database is in my server "192.168.1.25", so
i created my file name "conn.php"
PHP
<?php
$dbhost = '192.168.1.25:3306';
$dbuser = 'root';
$dbpass = '';
$database = 'sample';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
/*mysql_select_db($database, $conn);*/
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
mysql_select_db('sample', $conn);

$sql = 'select * from test';

$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_NUM))
{
    echo "EMP ID :{$row[0]}  <br> ".
         "EMP NAME : {$row[1]} <br> ".
         "EMP SALARY : {$row[2]} <br> ".
         "--------------------------------<br>";
}
mysql_free_result($retval);
echo "Fetched data successfully\n";
mysql_close($conn); 
?>


</body>
</html>
</html>


i got this error, given below

"Could not get data: No database selected"

please help me,,
thanks in advance...
Posted

Connect to the root user and Run a GRANT statement to grant access to your database user.
SQL
GRANT ALL PRIVILEGES ON *.* TO 'dbuser'@'localhost'
WITH GRANT OPTION;

Refer to the [LINK] for more info on grant.
 
Share this answer
 
Comments
sathish4303 6-Jul-13 6:20am    
how can Connect to the root user, can you give me the steps..
Prasad Khandekar 8-Jul-13 9:22am    
You can connect to your database as a root user from command prompt. Go to the bin folder of the MySQL installation and run following command.

mysql -u root

if it gives error then try

mysql -u root -p

Once connected simply issue the GRANT command

GRANT ALL ON sample.* TO 'dbuser'@'localhost';

Regards,
[no name] 31-Jul-18 6:37am    
You need add following code in your config file:

<?php

function getdb(){

$servername = "46.101.5.233"; // put your cloudways server IP here
$username = "qxxfumxxxbd";
$password = "xxxxbQxxmM";
$db = "qxxfumxxxbd";

try {

   $conn = mysqli_connect($servername, $username, $password, $db);

    //echo "Connected successfully";

   }

catch(exception $e)

   {

   echo "Connection failed: " . $e->getMessage();

   }

   return $conn;

}

Try remote mysql connection again with this code.
Hello Satish,

Ideally your mysql_select_db function should select the database. Please check the return value of the function. Alternatively you can programmatically issue the USE command.
PHP
<?php
mysql_query("USE sample", $conn);
?>

Still another way is to rewrite your query to include the database qualifier as shown below.
PHP
$sql = 'select * from sample.test';
$retval = mysql_query( $sql, $conn );

As a not of caution, I will encourage you to switch to MYSQLi[^] or PDO_MYSQL[^] extensions. The extension you are using is deprecated as of PHP 5.5.0, and will be removed in the future.

Regards,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900