Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 7 of 7

Thread: OOPHP + Relational MySQL DB

  1. #1

    OOPHP + Relational MySQL DB

    ok so I am trying to follow the two tutorials by BWH2 (hes best bet at helping) and have got this so far as my php code, I have changed some stuff from both tutorials though:
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>PHP Files and Projects</title>
    </head>

    <body>
    <?php
    mysql_connect
    ($_ENV['DATABASE_SERVER'], 'username''password');
    mysql_select_db('database');
    $query "SELECT * FROM tbl_works";
    $result mysql_query($query);
    $num mysql_num_rows($result);
    if (!
    $result) {
    $message  'Invalid query: ' mysql_error() . "\n";
    $message .= 'Whole query: ' $query;
    die(
    $message);
    }
    $ids = array();
    $i=0;
    while (
    $i $num) {
    $work_id=mysql_result($result,$i,'work_id');
    array_push($ids$work_id);
    $i++;
    }

    foreach (
    $ids as $newid) {
    $
    $newid = new project($newid);
    }
    mysql_free_result($result);
    echo $
    1->getID();
    class 
    project {
        private 
    $id;
        private 
    $title;
        private 
    $description;
        private 
    $category;
        private 
    $imageurl;
        function 
    setID($val) {
            
    $this->id $val;
            return;
        }
        function 
    getID() {
            return 
    $this->id;
        }
        function 
    getTitle() {
            return 
    $this->title;
        }
        function 
    __construct($val) {
            
    $this->id $val;
        }
    }
    mysql_close();
    ?>
    </body>
    </html>
    But everytime I run that I get:
    Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in /home/5900/domains/php.digitalcarnival.com/html/index.php on line 32

    P.S. I have changed my password, username and database but they are correct

  2. #2
    $1

    PHP variable names must not start with a number
    Last edited by JoshuaJonah; February 5th, 2007 at 02:17 PM.


    member #1 of the "XML-is-a-crutch-to-make-people-feel-elite-club"
    K-Emmys-06: Best Quote K-Emmys-06: Flash Genious

  3. #3
    you could also do that object grouping approach that i took in the OOP tutorial. i've actually thought about making a tutorial doing just this (integrating the two tutorials). but first the SQL joins tutorial, which is slowly progressing (but will be based primarily on the relational db tut).

  4. #4
    ah ok, thank you guys,

    and at bwh, that would be good, I get it all on its own with classess etc and in MySQL the queries its just joining them where I falter

  5. #5
    sorry to be a pain but I feel like I am getting somewhere and have this now:
    PHP Code:
    <?php
    mysql_connect
    ($_ENV['DATABASE_SERVER'], 'db5900''commisaryarickdata');
    mysql_select_db('db5900_work');
    $query "SELECT * FROM tbl_works";
    $result mysql_query($query);
    $num mysql_num_rows($result);
    if (!
    $result) {
    $message  'Invalid query: '.mysql_error()."\n";
    $message .= 'Whole query: '.$query;
    die(
    $message);
    }
    $ids = array();
    $i=0;
    while (
    $i $num) {
    $work_id=mysql_result($result,$i,'work_id');
    $work_title=mysql_result($result,$i,'work_title');
    $work_desc=mysql_result($result,$i,'work_desc');
    $work_img_url=mysql_result($result,$i,'work_img_url');
    array_push($ids$work_id);
    $projectarray 'projarray'.$work_id;
    $
    $projectarray = array('id' => $work_id'title' => $work_title'desc' => $work_desc'img_url' => $work_img_url);
    $i++;
    }

    foreach (
    $ids as $newid) {
    $name 'project'.$newid;
    $
    $name = new project('projarray'.$newid);
    }
    mysql_free_result($result);
    class 
    project {
        private 
    $id;
        private 
    $title;
        private 
    $description;
        private 
    $category;
        private 
    $imageurl;
        private 
    $alldetails = array();
        function 
    setID($val) {
            
    //
            
    return;
        }
        function 
    getID() {
            return 
    $this->id;
        }
        function 
    setTitle($val) {
            
    //
            
    return;
        }
        function 
    getTitle() {
            return 
    $this->title;
        }
        function 
    setDescription($val) {
            
    //
            
    return;
        }
        function 
    getDescription() {
            return 
    $this->description;
        }
        function 
    getDetail($choice) {
            
    $this->alldetails = array('id'=>$this->id'title'=>$this->title'description'=>$this->description'imageurl'=>$this->imageurl);
            return 
    $this->alldetails[$choice];
        }
        function 
    __construct($details) {
            
    $this->id $details['id'];
            
    $this->title $details['title'];
            
    $this->description $details['desc'];
            
    $this->imageurl $details['img_url'];
        }
    }
    mysql_close();


    echo 
    $project1->getTitle();
    ?>
    But everytime I try and get a detail it just displays:
    p
    But I dont know where it got that from as I havent type that and all my DB entries are right

  6. #6
    i can't look into the details now, but that 'p' looks like your first character in 'projarr'. i would try something like this:
    PHP Code:
    $grp = new ProjectsGroup'all' );
    foreach( 
    $grp->getGroup() as $project ) {
        echo 
    $project->getID().'<br />';
    }

    /* classes */
    class ProjectsGroup {
        private 
    $group = array();
        
        function 
    __construct$type ) {
            
    mysql_connect($_ENV['DATABASE_SERVER'], 'db5900''commisaryarickdata');
            
    mysql_select_db('db5900_work');
            
            if( 
    $type == 'all' ) {
                
    $query "SELECT * FROM tbl_works";
            }
            
            
    $result mysql_query($query);
            while( 
    $row mysql_fetch_array$resultMYSQL_ASSOC ) ) {
                
    $project = new Project$row );
                
    array_push$group$project );
            }
        }
        
        function 
    getGroup() {
            
    retun $this->group;
        }
    }

    class 
    Project {
        private 
    $id;
        
    // ... fill in the rest

        
    function __construct$details ) {
            
    $this->id    $details['id'];
            
    //... fill in the rest
        
    }
        
        function 
    getID() {
            return 
    $this->id;
        }

    ^ this hasn't been tested, so there might be syntax errors.
    Last edited by bwh2; February 5th, 2007 at 05:22 PM.

  7. #7
    Damnit, I just posted my username and password AGAIN, oh well its all safe, Thanks for the code bwh I'll try later today, I dont have FTP to my server here at school

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012