1

I'm having some trouble making a connection to my database. As far as I'm concerned I'm using the same code as usual. But suddenly I can't seem to make a connection. I've tried the script locally using MAMP aswell as online on my server.

I use a config file to select and connect the database with the following code:

<?php
  $db_host = "localhost";
  $db_username = "root";
  $db_password = "root";
  $db_database = "dbname";
$link = mysqli_connect($db_host,$db_username,$db_password) or die("Cannot connect");
  mysqli_select_db($link, $db_database) or die("Cannot select database");
  ?>

Next I try to print info from my database on my page using the following code:

<?php
$query="SELECT * FROM ditdoenweblokken ORDER BY id DESC";
$result = mysql_query($query) or die(mysql_error()) ;
while($rij = mysql_fetch_array($result)){  
$ID = $rij['id'];
?>
<li><a style="height:150px;width:150px;" class="fancybox" href="#inline1">
    <p style="font-weight:bold;"><?php echo($rij['titel']);?></p>
    <?php echo($rij['quote']);?></a>
</li>
<?php
}
?>

I've been staring my eyes out and don't know how to move on. Keep coming up with the same problem. If someone could help me out, it would be so much appreciated.

That's the error message:

mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /public/sites/www.kernlab.nl/website/index.php on line 11

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /public/sites/www.kernlab.nl/website/index.php on line 12 Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

14
  • 1
    are those php codes in the same file? Commented Dec 5, 2012 at 11:19
  • 1
    Are you combining mysqli_ and mysql_ functions? Is that really possible? Also, what's the error message? MySQL is like to tell you why you cannot connect. Commented Dec 5, 2012 at 11:20
  • Check database service is running and any firewall settings is blocking data server. Commented Dec 5, 2012 at 11:20
  • I get Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /public/sites/www.kernlab.nl/website/index.php on line 11 Commented Dec 5, 2012 at 11:21
  • 1
    They are not in the same file. But the config file is included in the header of each page. So actually, yeah they are in the same file :P Commented Dec 5, 2012 at 11:22

1 Answer 1

3

In config you use mysqli_ (mysqli extenton) methods.

Then you use mysql_ (mysql extenton) methods.

You are using different extentions. Use same extention in both cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.