Jump to content

Simple (but frustrating) PHP/PDO problem


gerryjuice

Recommended Posts

Hi im currently going through a fairly straight forward PHP tutorial. The tutorial consists of a simple insert into a table and a simple select from this table to view all records.

I am able to insert into the table easily, and can even select from the table when i use a selection which involes only selecting one of the columns of the table ie

SELECT name FROM attribute_values

 

But as soon as i try to select multiple columns, the program seems to time out, and i see the "Internet Explorer cannot ....etc" page. This seems a bit odd, i am using XAMPPLITE... are there any known issues surrounding doing selects from MYSQL databases within this package (im clutching at anything...) my the code is as follows, if anyone has any ideas.... ???

<?php
/*** mysql hostname ***/
$hostname = 'localhost';

/*** mysql username ***/
$username = 'username';

/*** mysql password ***/
$password = 'password';

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

    /*** set the error reporting attribute ***/
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
    
    /*** INSERT data ***/
    $count = $dbh->exec("INSERT INTO attribute_values(name, value) VALUES ('color', 'yellow')");

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

    /*** The SQL SELECT statement ***/
    $sql = "SELECT * FROM attribute_values";
    foreach ($dbh->query($sql) as $row)
        {
        print $row['name'] .'<br />';
        }

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

Link to comment
Share on other sites

Hi ober,

 

Thanks for your reply,

Is there a reason you are using PDO versus the default mysql libraries?  If you specify more than one column name (column1, column2 instead of *), does it work?

 

First i am only beginning to learning PHP and was beginning to try and interact with databases. I read somewhere that PDO's make interacting with databases alot easier and hence doing the tutorial. Or maybe i should just concentrate using the mysql libaries?

 

The select only works when i specify one column name, i tried putting in *, and (column1, column2 etc), both cases have caused the web page to seemingly "time out" and i get the "Internet Explorer cannot..." page

Link to comment
Share on other sites

I'm not sure where you read that, but I think the PDO methods are overly complicated compared to the native PHP MySQL functions.  I would concentrate on those.

 

I cannot be sure why you're getting a timeout.  Try using the MySQL extension functions and see if you get the same problem.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.