Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Use PDO when scripting in PHP?

Codes here !

Moderators: macek, egami, gesf

Use PDO when scripting in PHP?

Postby Tryah85 » Tue Nov 13, 2012 12:46 am

:?
PDO that connects, but INSERT(ing) data does not work:
Code: Select all
<?php

include 'include/connect.php';

try {
    $dbh = new PDO("mysql:host=$hostname;dbname=animals", $username, $password);
    /*** echo a message saying we have connected ***/
    echo 'Connected to database<br />';

    /*** INSERT data ***/
    $count = $dbh->exec("INSERT INTO animals(animal_type, animal_name) VALUES ('kiwi', 'troy')");

    /*** echo the number of affected rows ***/
    echo $count;

    /*** close the database connection ***/
    $dbh = null;
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
?>


The includes file sucessfully connects to the database with the PDO code, but does not add specific data into the animals table. I get the Error:

SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'localhost' (111)

I have done some research and all I could find is that either the mysqld.sock file is in the wrong location (which it isn't) or a PDO field is disabled in php.ini (isn't either).

ugh. I could go on forever. If no one has a solution please feel free to explain what the mysld.sock file does, or why using PDOs is prefered. I assume PDOs help developers avoid making PHP queries/field that are vunerable towards SQL Injection??

:D
Tryah85
New php-forum User
New php-forum User
 
Posts: 10
Joined: Wed May 30, 2012 12:20 am

Re: Use PDO when scripting in PHP?

Postby Tryah85 » Tue Nov 13, 2012 10:18 pm

I discovered a few things today. For starters the mysqld.sock file is meta data of how databases are read locally on the machine (that is not readable as sudo in gedit or pico btw). There is an option to use TCP/IP (in my.cnf LAMP/my.ini XAMP)... so the fact using TCP/IP allows a developer to have a test enviroment outside of the local machine and mimics the loading time of a live site.

In other words I don't understand why any web developer would work with sock files.

Good resource here:
http://dev.mysql.com/doc/refman/5.1/en/can-not-connect-to-server.html

PDOs are new concepts introduced in PHP5. Data objects are just another type of variable data stored as a list. I am sticking to learn old school PHP. I can't jump ahead or I get fusterated and get lost. :?
Tryah85
New php-forum User
New php-forum User
 
Posts: 10
Joined: Wed May 30, 2012 12:20 am

Re: Use PDO when scripting in PHP?

Postby johnj » Thu Nov 22, 2012 11:09 pm

johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron