Jump to content

Recommended Posts

Hi everyone,


Until now i didnt use classes and functions for sql related actions and now i would like to start.

I started out with this :

class.php
---------------------------------------------------

class db 
    
    public function 
connect($database,$user,$password) { 
        
$db mysql_connect("localhost",$user,$password) or die (mysql_error());
        
mysql_select_db($database);
    }
    
    
    static function 
fetch() {
    
         function 
getresults($query) { 

        
$resultArray = array();
        
$fetch mysql_query($query) or die (mysql_error());
        
        while (
$row mysql_fetch_assoc($fetch)){
            
$resultArray[] = $row;
          
            }
        
            return 
$resultArray;
    
        
mysql_free_result($fetch);
                
        }
        

    }

        
}

 

 

 

 

and index.php
----------------------------

 

<?php
require('class.php');

$connection = new db();
$connection->connect("testdb","root","root");

$connection->fetch()->getresults("SELECT * FROM users");

foreach (
getresults() as $user) { 
    echo 
"<div>{$user['user_first_name']}</div>";
}

 

 

 

 

BUt it doesnt work. Actually the only way it works is :

 

$connection::fetch();

foreach (
getresults("SELECT * FROM users") as $user) { 
    echo 
"<div>{$user['user_first_name']}</div>";

 

 

 

Now my ultimate goal is :

- I want to access db and get results anyware in my script to use :
$connection->fetch("SQL QUERY");
and then foreach to echo the results.

- I want to put more functions in fetch. For example if i want to fetch number of results to use
$connection->fetch()->results();

Im sorry if i confused you!

Basically i want to make everything tidy but im not familiar with the logic. I know basic class/function use.

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/288801-question-on-classes-functions/
Share on other sites

Hi,

 

this class has already been written for you: PDO.

 

Note that the old mysql_* functions are long obsolete and will be removed in one of the next PHP releases. Didn't you see the big red warnings in the PHP manual? When you switch to PHP 5.5, you'll also get tons of deprecation warnings for your script. So using this dying extension as the basis for your new database class really isn't a good idea.

Thanks for your replies.

 

Hmm ok i will study PDO, seems to be a must!

 

by the way PDO is just for databases, answeing my initial post will help me understand classes/functions in general!

 

I cannot use class->fuction->function?

 

im ive seen scripts using that :|

Edited by BlazeMike

by the way i have a question. I have developed some web applications in the past based on old mysql functioning. If i was asked to make changes or aditions to that code should i migrate ALL the code to PDO or should i just make the requested changes in Mysql?

 

 

I cannot use class->fuction->function?

It is possible to do that, it is called method chaining. Here is an example of how this is done.

 

by the way i have a question. I have developed some web applications in the past based on old mysql functioning. If i was asked to make changes or aditions to that code should i migrate ALL the code to PDO or should i just make the requested changes in Mysql?
If you want your current code to compatible with future versions of PHP then now would be a good time to do so.
Edited by Ch0cu3r

I cannot use class->fuction->function?

 

You can, but you need to understand what this expression actually means.

 

This has nothing to do with nested methods. The inner method simply returns an object, and then you call a method of that returned object:

<?php

// the short form:
$base_object->some_method()->some_other_method();

// what this actually does:
$returned_object = $base_object->some_method();
$returned_object->some_other_method();

Of course you can continue the chaining if the outer method again returns an object etc.

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.