-5

I am new to PHP, I am trying to implement network communication with mysql database which I made through phpmyadmin over my Bluehost account. Now I have written the following php script to check whether I can connect but I am not able to connect:

          <?php  
          $mysql_host='localhost';
          $mysql_user='xxx';
          $mysql_pass ='xxx';


          mysql_connect ('localhost', '$mysql_user', '$mysql_pass')
          or die ('I cannot connect to the database.');
           //echo 'hello';

          ?>
Skynet
  • 7,820
  • 5
  • 44
  • 80
  • And your question is? – Pekka Mar 15 '13 at 11:19
  • 2
    remove quotes around variables - mysql_connect ('localhost', $mysql_user, $mysql_pass) – Waygood Mar 15 '13 at 11:20
  • My Question is that I am not able to understand where I might be going wrong, any suggestions? Please bear with me as I am new to PHP. – Skynet Mar 15 '13 at 11:20
  • mysql_connect ('localhost', $mysql_user, $mysql_pass) – Nirav Ranpara Mar 15 '13 at 11:21
  • Hey @Waygood thanks mate :) I am able to connect now! – Skynet Mar 15 '13 at 11:22
  • 2
    [My advice would be don't learn `mysql_*` functions](http://stackoverflow.com/q/12859942). They are no longer maintained and the deprecation process has begun, see the [red box](http://php.net/mysql-connect). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli); [this article](http://php.net/mysqlinfo.api.choosing) will help you decide which. If you choose PDO, [here is a good tutorial](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers). – vascowhite Mar 15 '13 at 11:22
  • vascowhite, I am trying to learn PHP purely to develop mobile applications, it would be helpful if you suggest in context of Mobile Applications. I am not sure if this is relevant. – Skynet Mar 15 '13 at 11:24
  • @Borniet explains why, and move towards pdo or mysqli like vascowhite suggests – Waygood Mar 15 '13 at 11:24
  • The only regret is the downvoting, I specified that I am new to this! – Skynet Mar 15 '13 at 11:31
  • Why do people compete with each other to give a vote down? – Amir Mar 15 '13 at 11:46

5 Answers5

2

Try this:

mysql_connect ('localhost', $mysql_user, $mysql_pass)

Instead of:

mysql_connect ('localhost', '$mysql_user', '$mysql_pass')

You do not need single quotes here.

Bogdan Burym
  • 5,482
  • 2
  • 27
  • 46
1

Variables between single quotes are not evaluated

 mysql_connect ('localhost', '$mysql_user', '$mysql_pass')

Try this:

  mysql_connect ('localhost', $mysql_user, $mysql_pass)
Borniet
  • 3,544
  • 4
  • 24
  • 33
1

the quotes make your vars to a string! and never post you pass and user... even its local use

mysql_connect ( $mysql_host, $mysql_user, $mysql_pass);

dont use die(), use a error log.

better would be a pdo connection, it give you more comfort and securety.

make shure pdo is enadbled in mysql config, mostly it is enabled by default.

you can set up a pdo connection like

$dsn = 'mysql:dbname=<databasename>;host=<hostname>';
  $user = '<user>';
  $password = '<passwd>';

try {                                                                         
    $dbh = new PDO($dsn, $user, $password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    $_SESSION['error'] .= 'Verbindung fehlgeschlagen: ' . $e->getMessage();
}     
Dv Blttmnn
  • 13
  • 1
  • 4
1

You have to remove quotes around variables in mysql_connect:

mysql_connect ('localhost', $mysql_user, $mysql_pass)
Marko D
  • 7,576
  • 2
  • 26
  • 38
Amir
  • 4,089
  • 4
  • 16
  • 28
1

You should understand diff bet single quotes and double quotes in php. single quotes output string as it is. and double quotes parse string before outputting.

Anon30
  • 567
  • 1
  • 6
  • 21