Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 22nd, 2005, 01:53 PM
Registered User
 
Join Date: Apr 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help with change postgreSQL connection to MySQL

I am currently reading Professional PHP 5 and I have only used MySQL and was wondering if anyone could help me figure out the connection line. I did it the way I thought would work but its not working.

Here is my code:

<?php

class Widget
{

    private $id;
    private $name;
    private $description;
    private $db;
    private $needsUpdating = false;

    public function _construct($widgetID)
    {
        //The widgetID parameter is the primary key of a record in the database
        //containing the information for this object

        //Create a connection handle and store it in a private member variable
        $this->db = @mysql_connect('localhost', 'root', '');
        //$this->db = mysql_connect('localhost', stgres password=root ");

        if (! is_resource($this->db))
        {
            exit('<p>Unable to connect to the ' . 'database server at this time.</p>');
          }

        if (!@mysql_select_db('practicephpdb'))
        {
            exit('<p>Unable to locate the content ' . 'database at this time.</p>');
        }


        $sql = "SELECT \"name\", \"description\" FROM mysql_temp_1.widget WHERE widgetid = $widgetID";

        $rs = mysql_query($this->db, $sql);

        if (! mysql_num_rows($rs))
        {
            throw new Exception("An error occured selecting from the database.");
        }

        $data = mysql_fetch_array($rs);
        $this->id = widgetID;
        $this->name = $data['name'];
        $this->description = $data['description'];
    }

    public function getName()
    {
        return $this->name;
    }

    public function getDescription()
    {
        return $this->description;
    }

    public function setName($name)
    {
        $this->name = $name;
        $this->needsUpdating = true;
    }

    public function setDescription($description)
    {
        $this->description = $description;
        $this->needsUpdating = true;
    }

    public function _destruct()
    {
        if (! $this->needsUpdating)
        {
            return;
        }

        $sql = 'UPDATE "widget" SET ';
        $sql .= "\"name\" = '" . mysql_escape_string($this->name) . "',";
        $sql .= "\"description\" = '" . mysql_escape_string($this->description) . "'";
        $sql .= "WHERE widgetID = " . $this->id;

        $rs = mysql_query($this->db, $sql);
        if (! is_resource($rs))
        {
            throw new Exception('An error occured updating the database.');
        }

        //Your done with the database. Close the connection handle.
        mysql_close($this->db);
    }
}
?>




chad
 
Old April 28th, 2005, 07:57 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 101
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Moharo
Default

howdy

if you provided some more examples of how it doesn't work. try removing username and password from mysql_connect(), just have the 'localhost' there....

another thing - don't use mysql_fetch_array() to fetch a single value, instead use mysql_result()

:D

www.campusgrind.com - college portal
 
Old July 7th, 2010, 12:37 PM
Registered User
 
Join Date: Jul 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Postgresql database to mysql

Hi,

I have the dump file of the postgresql database. We are switching to another server and also the current database is in postgresql and we need to change to mysql and so I have two questions:
1. Does the postgres database dump file have CREATE statements? My file has only create table, create sequence, alter and data details.
2. I converted from postgresql dump file to mysql and now it has only create table details. Is that how it is suposed to be?
2. Can anyone please explain step by step how to migrate the database to a new server.

I am new with the database stuff an so I would really appreciate any help/

Thanks,
Charmie





Similar Threads
Thread Thread Starter Forum Replies Last Post
C# and mySQL Connection naifwonder C# 5 March 8th, 2008 01:52 PM
Change database connection song Visual Basic 2005 Basics 4 November 30th, 2007 01:22 AM
Change in connection string x_ray Classic ASP Basics 2 February 21st, 2006 05:10 PM
Mysql connection catab C# 4 December 7th, 2003 11:25 AM
How can I change a field name in mysql? scifo Beginning PHP 2 August 1st, 2003 09:41 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.