0

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.

2 Answers 2

4

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);
}
Sign up to request clarification or add additional context in comments.

6 Comments

okay thank you for the reply, I am reading into it now.. I am very new at this.. so struggling abit.. thanks for the reply however.
@HurkNburkS Basically in your code you are trying to make a connection using the right (PDO) function, but then you are trying to use depreciated functions (mysql_*) to fetch the data - it's a mismatch. You need to keep using the PDO functions to do everything with the database. The error messages you are seeing is from the old functions trying to get data without a connection established (in the old functions).
okay just looking through your second peice of code.. is the $dbh, $db from the first section? I'm taking its databasehost... but just double checking I have this right :)
not a problem :) still working through it getting errors in the area where you have declared $obj = $stmt->setFetchMode(PDO::FETCH_INTO, new userStructure); but sure I will figure out what each part means soon.. thanks very mcuh for your help.. I will accept your answer shortly
@HurkNburkS I use a class that I put data from the row straight into, you can however fetch into an array or an assoc array.
|
0

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.

Comments

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.