HurkNburkS
HurkNburkS

Reputation: 5510

having problems connecting to mysql db with php

I have a test db on godaddy, I have had a similar issue before but with the help of stackoverflow it was solved.. So I have actually used the same solution however now I am having a host of errors when I try to load my php file from the browser.

Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/97/8603797/html/countryQuery.php on line 14

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/content/97/8603797/html/countryQuery.php on line 14

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/content/97/8603797/html/countryQuery.php on line 16

Warning: mysql_close(): no MySQL-Link resource supplied in /home/content/97/8603797/html/countryQuery.php on line 18
Database Output

I have read what the problem is and some of the things I have read suggest I am not connecting to the DB correctly.. but I am only doing what I have read/been told and dont really know what else I can do to solve the issue I am having...

this is my php code, if anyone has and suggestions or solutions I would greatly appreciate hearing from you.

<?

 $hostname = 'mydatabasename.db.6666666.hostedresource.com';
  $database = 'mydatabasename';
  $username = 'myusername';
  $password = 'secretsecret';

  // establish a connection
  $db = new PDO("mysql:host=$hostname;dbname=$database",$username,$password);


$query="SELECT * FROM `Countries`";

$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$country=mysql_result($result);

echo "<b>$country<br>";

$i++;
}

?>

the out put should just be a list of the country names I have in the countrys table of my database.. which has values in it.

Upvotes: 0

Views: 177

Answers (2)

Fluffeh
Fluffeh

Reputation: 33502

You are mixing PDO and mysql_* functions.

Try something like this:

$db = new PDO("mysql:host=$hostname;dbname=$database",$username,$password);
$db->setAttribute(PDO::ATTR_AUTOCOMMIT, true);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql="SELECT * FROM `Countries`";

$numRows=$dbh->exec($sql);
echo "There were $numRows selected with:<b> $sql </b><br><br>";
$db=null;
unset($db);

When it comes to getting data from the PDO connection, I normally do something like this using the FETCH_INTO option:

$stmt = $db->query($sql);
$obj = $stmt->setFetchMode(PDO::FETCH_INTO, new userStructure);
// I do normally have a class matching the row I wan:

foreach($stmt as $userStructure)
{
    $someObject->year=$userStructure->year;
    $someObject->month=$userStructure->month;
    unset($stmt);
}

Upvotes: 4

mroesler
mroesler

Reputation: 98

Make sure that you include the port as well when using a PDO connection. This is looking like you are doing this in a hosted environment. Check the installation guide regarding the port used.

Upvotes: 0

Related Questions