HenryC Posted June 15, 2011 Share Posted June 15, 2011 I don't use functions to often but i want to clean up my code to make less bandwidth and load so i have been making some, what i am stuck at is say i want a function to hold querys <?php function query() { $sql = mysql_query("SELECT * FROM users"); $row = mysql_fetch_assoc($sql); } ?> How do i execute that when i need it? the usual query(); doesnt seem to work with it, neither does return; Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 15, 2011 Share Posted June 15, 2011 That is because your function only runs the query. It doesn't DO anything with the results and it doesn't return the results to wherever you called the function. First of all, give your functions useful names. A name like query(0 doesn't tell you anything about WHAT it is querying. Here is an example of a revision and how it would be used: function getUserData() { $sql = mysql_query("SELECT * FROM users"); $result = mysql_fetch_assoc($sql); $users = array() //Dump results into an array while($row = mysql_fetch_assoc($result)) { $users[] = $row; } return $users; } //In another section/script you need to display a list of users. //You would do the following $userList = getUserData(); foreach($userList as $userRecord) { echo "Username: {$userRecord['username']}<br>\n"; } Quote Link to comment Share on other sites More sharing options...
sunfighter Posted June 15, 2011 Share Posted June 15, 2011 but i want to clean up my code to make less bandwidth PHP is run at the server. What it outputs goes to your browser and that is what creates bandwidth. You can run a thousand line PHP script but if it only output a single line of HTML it really don't use much bandwidth. Running MYSQL queries will not mess up your bandwidth. Consolidating them in functions will not help nor hinder the BW. Functions for queries are unnecessary if you get all the info you need with a single query instead of two or three; it makes your code more efficient and uses less server time. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 15, 2011 Share Posted June 15, 2011 but i want to clean up my code to make less bandwidth PHP is run at the server. What it outputs goes to your browser and that is what creates bandwidth. You can run a thousand line PHP script but if it only output a single line of HTML it really don't use much bandwidth. Running MYSQL queries will not mess up your bandwidth. Consolidating them in functions will not help nor hinder the BW. Functions for queries are unnecessary if you get all the info you need with a single query instead of two or three; it makes your code more efficient and uses less server time. @sunfighter: You are referring to "bandwidth" in only one sense - internet upload/download bandwidth. But, just about any aspect of computing has "bandwidth". There is bandwidth specific to hardware: CPU, Memory and hard Drive. There is also bandwidth specific to software: Database and Web Server would apply here. All of these (and others) will have an impact on the performance of a web based application. For an example of how server-side bandwidth can affect the client browser you only need to have a script that runs queries in a loop. As the number of loops increases the time for the page to display will become longer and longer until the request will time out. There may be only a few bytes of actual data being transmitted between the server and client, but it is due to "bandwidth" issues on the server that affect the client side performance. Quote Link to comment Share on other sites More sharing options...
sunfighter Posted June 16, 2011 Share Posted June 16, 2011 @mjdamato, I would like to explain what I wrote and stand by. I will go ahead and except your expanded diffenition of Bandwidth. And yes I was talking about internet upload/download, but what I said goes for CPU usage and file size also. I answered HenryC's question => I don't use functions to often but i want to clean up my code to make less bandwidth and load so i have been making some, what i am stuck at is say i want a function to hold a query Well his question was how do you call a php function. But this is his reason for doing that. And what he is doing is wrong and is what I addressed. He basically wants to put a query into a function and call the function. That's not good. You only need to make a query ONCE! and that was my point. You don't write functions that are only used once so why put queries into a function? That adds to the program size and does increase the CPU usage. Henry needs to add lines to make a function and then more lines to call it and that adds to the code and increases the time in executing the program. Just doing the query is the way to go. He just has to get all of the info he needs with that one query and not do a query to the same table a second time. There are times when functions help out tremendously, but doing a query is not one of those times. Your examples are valid, but your looking at the big picture and I am looking at this one instance (functions to do a single query). Now that being said, you probably help Henry with queries in general - well Yeah you did and I just addressed a single instance. But that's how i roll, I answer what is asked or what I perceive to be asked. And in my mind they are the same, reality and perception. Peace Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 16, 2011 Share Posted June 16, 2011 @sunfighter I see what you are saying. But, I have learned to "read between the lines" of what people post here. Sometimes they are asking the opposite of what they really mean. I took the OPs post as needing to make his code more efficient. To me that just doesn't mean execution time but also the basic complexity of the code. As to your statement: You only need to make a query ONCE! and that was my point. You don't write functions that are only used once so why put queries into a function? I have to disagree for several reasons. A function to get a list of users is very common. There is a high probability that an application will have many pages that need to get a list of users: a page of registered users, populate a select list to assign something to a user, etc. Typically I would have a function like that with an optional WHERE clause to give it more flexibility, e.g. I could use the same function to get a list of admins, disabled users, whatever. That way I only need ONE function to perform multiple tasks. In fact, I typically create a class to handle all the data abstraction within my applications. I may have several functions that are used throughout the application (such as getting a list of users) but I may also have functions that are only used by a single page. By separating the data abstraction you gain great flexibility in maintainability and portability of your application. If you need to change to a different database model, want to change table/database names, etc. You only have to go find the functions used for accessing data and make the changes there instead of finding each and every script that touches the database. If the OP is planning on duplicating that function in multiple pages where he needs to get a list of users, yes, that is a poor implementation. If, however, he is going to put that function in a common include file or a class, then it is a good implementation - even if he only calls the function on one page. But, creating functions for data abstraction is not the only reason you may write a function for something you only use one. The standards of structured programming and OOP both support the modularization of code. Quote Link to comment Share on other sites More sharing options...
sunfighter Posted June 17, 2011 Share Posted June 17, 2011 OK. Quote Link to comment Share on other sites More sharing options...
xyph Posted June 17, 2011 Share Posted June 17, 2011 You both contributed much to the thread. I'm not sure why you feel you're on opposing points when you're on differing points. The OP is lucky to have both sides presented to him in such a way. Kudos to both of you, but next time you should support each others response with your own, rather than conflict with each other Also, sunfighter, in programming, 'better', 'worse', 'right', 'wrong', etc are subjective. Two completely differing code blocks can perform the same function, though one might be more efficient than the other, one may be more clean and easy to read. Try to use words like 'less efficient' or 'messy' rather than wrong Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.