hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : Function with Dynamic MySQL Query - HELP
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

Function with Dynamic MySQL Query - HELP

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 12-29-2003, 05:18 PM
PaddysPlace PaddysPlace is offline
WHT Addict
 
Join Date: Nov 2003
Posts: 100

Function with Dynamic MySQL Query - HELP


Hey guys.

First off, here's the function:
PHP Code:
##### Begin function get_cat($function_id)
// Selects all categories from database classified by ID
function get_cat($function_id)
{
  global 
$DB,$selected_cat,$selected_cats,$total_cats;
  
db_connect();

 if(
$function_id != '')
 {
   
$select_cat "SELECT * FROM categories WHERE ID=$function_id";
   
$selected_catmysql_query($select_cat) or die ("Couldn't select categories");
 }
 else
 {
   
$select_cats "SELECT * FROM categories";
   
$selected_catsmysql_query($select_cats) or die ("Couldn't select categories");
   
$total_cats mysql_num_rows($selected_cats);
 }
}
// End function get_cat 
As you can see I made it so if there's an ID specified, call it and save row in $selected_cat.

If there is no ID specifed, get them all and put in an array $selected_cats.

Right now I have to call it with:
get_cat($CATID) (To select specific)
get_cat('') (To select all)

Anyone know a better way to clean up my code?

Meh, I also feel so behind because everyone is using a DB class and I've been trying to read up on it but just can't get it down so I have to use old fashioned settings... What's the advantage?

Regards,
Patrick

AIM: WrldsGreatestOne (If you can help/explain how to optimize my database functions, please help!)


Last edited by PaddysPlace; 12-29-2003 at 05:49 PM.
Reply With Quote


Sponsored Links
  #2  
Old 12-30-2003, 02:36 AM
Burhan Burhan is offline
Community Guide
 
Join Date: Jul 2003
Location: Kuwait
Posts: 5,100
If you don't want to pass an argument, don't pass ''. Just call the function.

Secondly, you function won't return any results because you are never fetching them. mysql_query() just runs the query and returns a handle. You then have to fetch your results.

Here is another way to write your same function

PHP Code:
//Set up a default value for the parameter.
function get_cat($function_id NULL)
{
    global 
$DB,$selected_cat,$selected_cats,$total_cats;
  
db_connect(); 

  
$sql "SELECT * FROM categories ";
  if (isset(
$function_id)) { $sql .= "WHERE ID = $function_id"; }
   
$result mysql_query($sql);
   if (!
$result)
   {
        return 
mysql_errno()." : ".mysql_error();
    }
     while (
$row mysql_fetch_assoc($result))
    {
        
$return[] = $row;
     }
      
$return[] = mysql_num_results($result);
      return 
$return;
}

//Sample call -- all categories

$categories get_cat(); 

/*Since valid results back are an array,
    we can use this check to see if our function worked or not. Just one way of doing things */
if (!is_array($categories))
{
    echo 
"Sorry, there was an error<br />";
    echo 
$categories;
} else { echo 
"<pre>"print_r($categoies); echo "</pre>"; } 
Quote:
Meh, I also feel so behind because everyone is using a DB class and I've been trying to read up on it but just can't get it down so I have to use old fashioned settings... What's the advantage?
Well, the advantage is that you don't have to repeat your code, and you can just start using the database without having to worry about connectiing, fetching, etc. Plus, most DB classes work with more than one type of database, which adds portability to your code.

__________________
In order to understand recursion, one must first understand recursion.
If you feel like it, you can read my blog
Signal > Noise

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
Web Host Rackspace Launches Private Beta for MySQL Cloud Database Web Hosting News 2011-12-01 21:09:51
Cloud Infrastructure Firm Morphlabs Launches New Technology to Improve Cloud Bursting Web Hosting News 2011-11-09 19:08:35
Web Host 1&1 Internet UK Adds Business Hosting Plan, Cloud Server Features Web Hosting News 2011-09-13 15:50:08
Web Host CSC Awarded 10-Year, $900M Managed Hosting Contract Web Hosting News 2011-09-09 17:33:57
Web Host FireHost Partners with Cloud Security Firm Gazzang for Data Encryption Web Hosting News 2011-08-16 20:33:43


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

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 Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?