Boudewijn Smit
Boudewijn Smit

Reputation: 187

php mysql query connection error cannot select database

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)

Upvotes: 1

Views: 5764

Answers (1)

s.webbandit
s.webbandit

Reputation: 17000

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.

Upvotes: 3

Related Questions