0

I checked with phpmyadmin and the query that I'm trying to do is working.

$user = to the username login. I see it because I echo it. So I don't think that's the error.

The error I get is

> PHP Warning:  mysql_query(): Can't connect to local MySQL server through socket      
'/var/run/mysqld/mysqld.sock' (2) in 
/hermes/bosweb/web161/b1615/ipg.jmdev/nmc/admin/user.php on line 14

> PHP Warning:  mysql_query(): A link to the server could not be established in 
/hermes/bosweb/web161/b1615/ipg.jmdev/nmc/admin/user.php on line 14

Line 14 is my query line (below), which is the piece of code that doesn't work...

Here is my code:

$host="link"; // Host name 
$username="username"; // Mysql username 
$password="password"; // Mysql password 
$db_name="nmc"; // Database name 
$tbl_name="account"; // Table name 

session_start();
$user = $_SESSION['myusername'];
echo $user . '<br>';
//[Working on PHPMyAdmin](SELECT `role` FROM `account` WHERE `username`='jean8mathieu') (Working)
$result = mysql_query("SELECT `role` FROM `account` WHERE `username`='$user'");//I would like to make this work...
//$result = mysql_query("SELECT role FROM account WHERE username='jean8mathieu'"); /I try this line and did not ever work...

if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
$row = mysql_fetch_row($result);
echo $row[0]; 

My problem is not to connect on the server it's the query. I am able to connect to it just my query doesn't seems to work... It working for all my other page...

  • "A link to the server could not be established" Is your password correct? – Paul Dessert Mar 06 '14 at 22:58
  • 3
    Where is database connection? It seems you are only declaring database details – Fabio Mar 06 '14 at 22:59
  • 3
    And on a sitenote: please don't use the mysql extension anymore. It is officially deprecated. You should be using either [PDO](http://www.php.net/PDO) or [MySQLi](http://www.php.net/MySQLi) instead. – Tularis Mar 06 '14 at 23:00
  • You don't connect to the database before querying it. – djot Mar 06 '14 at 23:00
  • [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Phil Mar 06 '14 at 23:03
  • Also see [this answer](http://stackoverflow.com/a/13944958/1557526) for information on the deprecation/change to PDO/MySQLi – kero Mar 06 '14 at 23:04

1 Answers1

4

Please don't use any mysql_*() function any longer, since the old MySQL library is deprecated (reached end of life).

Review MySQLi, PDO and this answer and choose one of the two alternatives to do MySQL related stuff.

That said...

You need to first of all tell PHP how to establish a connection to the MySQL server using mysql_connect():

// we connect to example.com and port 3307
$link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);

// we connect to localhost at port 3307
$link = mysql_connect('127.0.0.1:3307', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
Community
  • 1
  • 1
SteAp
  • 11,853
  • 10
  • 53
  • 88
  • 2
    Please please please explain him mysql_* are deprecated, he should start learning something else – Fabio Mar 06 '14 at 23:01
  • 2
    Also **from the manual** - *"This extension is deprecated as of PHP 5.5.0, and will be removed in the future."* – Phil Mar 06 '14 at 23:02
  • 1
    *"Hey, here's how you can do this with `eval` ... but don't use `eval`!"* - see how silly that looks – Phil Mar 06 '14 at 23:10