Include PHP in MYSQL Function?

This is a discussion on "Include PHP in MYSQL Function?" within the PHP Forum section. This forum, and the thread "Include PHP in MYSQL Function? are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Mar 24th, 2008, 18:06
Jack Franklin's Avatar
Moderator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,405
Blog Entries: 8
Thanks: 18
Thanked 14 Times in 14 Posts
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
PHP: Select all

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:
PHP: Select all

function where() {
if (
$where !== '') {
echo 
'WHERE ' $where .'';
}

So if where actually has a value, it ends up as (in this example)
PHP: Select all

echo WHERE id '1'
But if I'm right a function cannot be included in the query? Ideally it would be:
PHP: Select all

$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,
PHP: Select all

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

$all->get('content''id'); 

Is not working. I know nothing will display, but surely it should say 'Query Fine'. Any ideas anyone?
__________________
Jack Franklin - Webforumz Moderator
(x)HTML | CSS | PHP | MySQL | JQuery (Javascript)
Contact: My Blog | Twitter | Delicious
Want Lessons? PM me.
If you think I've helped, please press the 'Thanks' Button.
Last Blog Entry: A Week with VBulletin (Aug 28th, 2008)

Last edited by Jack Franklin; Mar 24th, 2008 at 18:11.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Mar 24th, 2008, 18:16
Jack Franklin's Avatar
Moderator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,405
Blog Entries: 8
Thanks: 18
Thanked 14 Times in 14 Posts
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

PHP: Select all

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';
    }                
}

PHP: Select all

$all = new content;
$all->connect_db(roottest);
$all->get('content''id'); 
__________________
Jack Franklin - Webforumz Moderator
(x)HTML | CSS | PHP | MySQL | JQuery (Javascript)
Contact: My Blog | Twitter | Delicious
Want Lessons? PM me.
If you think I've helped, please press the 'Thanks' Button.
Last Blog Entry: A Week with VBulletin (Aug 28th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Mar 24th, 2008, 19:23
CloudedVision's Avatar
Moderator
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 1,265
Blog Entries: 9
Thanks: 2
Thanked 40 Times in 40 Posts
Re: Include PHP in MYSQL Function?

You gotta do something like this:

PHP: Select all

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

hope it answers your question.
__________________
Web Design And Development: Other Road Design | Problems with IE6?: KApp | My Blog: Only Nerds Allowed | Learning PHP? Lessons
Last Blog Entry: Hilarious Rapper (Jul 29th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Mar 24th, 2008, 19:35
Up'n'Coming Member
Join Date: Feb 2007
Location: United States
Age: 15
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
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
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

Tags
functions, mysql, php, problems

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 Nov 22nd, 2007 18:13
Function parameters that include quotes loorp JavaScript Forum 7 Sep 17th, 2006 04:18
js include timmytots JavaScript Forum 5 Jul 8th, 2006 12:43
mysql query in a function Redempt1on PHP Forum 6 May 19th, 2006 01:25
Help need with php include!! allstar PHP Forum 7 Dec 29th, 2005 15:09


All times are GMT. The time now is 16:20.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42