Submit Your Article Webforumz RegistrationAnnouncements Contact Webforumz StaffContact
Home Resources Blogs Meet the Team Contact Register
 

Go Back   WebForumz.com > The Code > PHP

Reply
 
LinkBack Thread Tools
Old March 24th, 2008, 03:06 PM   #1
Most Reputable Member
 

Join Date: May 2007
Location: Cornwall, England
Posts: 1,421
Blog Entries: 8
Thanks: 18
Thanked 14 Times in 14 Posts
Rep Altering Power: 0 Jack Franklin will become famous soon enough
Include PHP in MYSQL Function?

This might really confuse you, but I'll try my best.

I'm attempting to very slowly redo my blog into OOP. I'm creating a class that allows you to give it parameters. Those are then inserted into the MySQL query. EG
Code:
class content {
//START OF CONTENT CLASS
    
function connect_db($user$db) {
        
$conn mysql_connect("localhost"$user"") or die(mysql_error());
        
mysql_select_db($db$conn) or die(mysql_error());
    }

    function 
get($table$selector "*"$where ""$limit ""$orderby ""$order "") {... 
Basically I want the variables $where, $limit, $orderby and $order to not be needed. So, as you can see, I have set their defaults to nothing.

I need to check if the user has included them, and if they have, add them to the MySQL statement. My plan was to create functions like:
Code:
function where() {
if (
$where !== '') {
echo 
'WHERE ' $where .'';
}

So if where actually has a value, it ends up as (in this example)
Code:
echo WHERE id '1'
But if I'm right a function cannot be included in the query? Ideally it would be:
Code:
$query mysql_query("SELECT '$selector' FROM '$table' where();"); 
That incorporates the function where. I've tried it and it gave an error. Is it possible? Is there another way round it?

Jack

---

Also,
Code:
function get($table$selector "*"$where ""$limit ""$orderby ""$order "") {
    if (
mysql_query("SELECT '$selector' FROM '$table'")) {
    echo 
'Query Fine';
    } 
And then:
Code:
$all->get('content''id'); 
Is not working. I know nothing will display, but surely it should say 'Query Fine'. Any ideas anyone?
__________________
Yours is the Earth and everything that's in it
And - which is more - you'll be a Man my son!

Last edited by Jack Franklin; March 24th, 2008 at 03:11 PM..
Jack Franklin is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old March 24th, 2008, 03:16 PM   #2
Most Reputable Member
 

Join Date: May 2007
Location: Cornwall, England
Posts: 1,421
Blog Entries: 8
Thanks: 18
Thanked 14 Times in 14 Posts
Rep Altering Power: 0 Jack Franklin will become famous soon enough
Re: Include PHP in MYSQL Function?

Infact, here is the entire code. It is my first real go at something useful in OOP, so be nice

Code:
class content {

//START OF CONTENT CLASS
    
function connect_db($user$db) {
        
$conn mysql_connect("localhost""root""") or die(mysql_error());
        
mysql_select_db($db$conn) or die(mysql_error());
    }

    function 
get($table$selector "*"$where ""$limit ""$orderby ""$order "") {
      if (
mysql_query("SELECT '$selector' FROM '$table'")) {
    echo 
'Query Fine';
    }                
}

Code:
$all = new content;
$all->connect_db(roottest);
$all->get('content''id'); 
__________________
Yours is the Earth and everything that's in it
And - which is more - you'll be a Man my son!
Jack Franklin is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old March 24th, 2008, 04:23 PM   #3
Most Reputable Member
 

Join Date: Feb 2008
Location: In My Own Little World
Age: 16
Posts: 1,345
Blog Entries: 9
Thanks: 2
Thanked 45 Times in 45 Posts
Rep Altering Power: 0 CloudedVision is on a distinguished road
Re: Include PHP in MYSQL Function?

You gotta do something like this:

Code:
$query "SELECT `$selector` FROM `$table`";
if(
$where!='')
     
$query .= " WHERE `something`='bla'";
mysql_query($query); 
Something like that

hope it answers your question.
__________________
Theron Luhn
CloudedVision is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old March 24th, 2008, 04:35 PM   #4
WebForumz Member
 

Join Date: Feb 2007
Location: United States
Age: 16
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 Weird1993 is on a distinguished road
Re: Include PHP in MYSQL Function?

Also, in queries, don't use single quotes, as it will take it literally, not as a variable I believe
__________________
Daniel Thompson
danielwthompson.com
Weird1993 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Bookmarks

Tags
functions , mysql , php , problems


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Does using PHP include() function affect my SEO ? RohanShenoy Search Engine Optimization (SEO) 3 November 22nd, 2007 02:13 PM
Function parameters that include quotes loorp JavaScript 7 September 17th, 2006 12:18 AM
js include timmytots JavaScript 5 July 8th, 2006 08:43 AM
mysql query in a function Redempt1on PHP 6 May 18th, 2006 09:25 PM
Help need with php include!! allstar PHP 7 December 29th, 2005 11:09 AM


Search Engine Optimization by vBSEO 3.2.0 RC8