woodworks Posted August 18, 2008 Share Posted August 18, 2008 I'm learning PHP and ran into something I can't quite figure out if what I am doing is really the way I should or if there is a better way. Here's an example. If I use this code to get information and display the results: $time = mysqli_query($link, "SELECT UNIX_TIMESTAMP()"); print $time; My browser returns Object id #3 (I'm running PHP through Apache2). But, if I use this code: $time = mysqli_query($link, "SELECT UNIX_TIMESTAMP()"); while ($row = mysqli_fetch_array($time)) { print $row[0]; } I get the actual result of (i.e., the content of the object held by $time) the SELECT statement to print to the browser which is what I want. I understand from the online PHP manual (see http://us3.php.net/mysqli_query) that mysqli_result will return a result object (hence the reason for the "Object id #3" message?), but then it goes on to say the "resultmode" is mysqli_use_result or mysqli_store_result. I read about these in the online PHP manual and am confused. I've been reading the SAMS Teach Yourself "PHP, MySQL, and Apache" book and cannot find any detailed explanation as to: 1. What is the best way to print the results of a mysqli_query to a browser (I seriously doubt the above method I am currently using is the best). I have no problem understanding why using my above method might be appropriate when I have multiple rows of data being returned, but to do this when I know that only a single row, with one item, will be returned seems odd. 2. How do you use MYSQLI_USE_RESULT and MYSQLI_STORE_RESULT? Every Google search I do pretty much points me to a copy of what the online PHP manual says with no further explanation. Also, could somebody please recommend a more comprehensive book than the SAMS book I am reading? Thanks. TC Quote Link to comment Share on other sites More sharing options...
trq Posted August 18, 2008 Share Posted August 18, 2008 If your only expecting one result you can simply remove the while() loop. $time = mysqli_query($link, "SELECT UNIX_TIMESTAMP()"); $row = mysqli_fetch_array($time); print $row[0]; Quote Link to comment Share on other sites More sharing options...
woodworks Posted August 18, 2008 Author Share Posted August 18, 2008 If your only expecting one result you can simply remove the while() loop. $time = mysqli_query($link, "SELECT UNIX_TIMESTAMP()"); $row = mysqli_fetch_array($time); print $row[0]; Great tip. Thanks. Can you also recommend a good book for learning to use PHP with MySQL? TC Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 18, 2008 Share Posted August 18, 2008 1. What is the best way to print the results of a mysqli_query to a browser That depends COMPLETELY on the context in which you want to display the results. Personally I suggest creating a static HTML page with "test" data and then creating your PHP to match. In some cases I would just hard-code the functioality to write the data to the page. In other's (such as a report listing) I might create a function or class to display the data tomaintain consistency. 2. How do you use MYSQLI_USE_RESULT and MYSQLI_STORE_RESULT? Those are used in instances where you run multiple queries at once. I never do that myself. I would suggest sticking with running queries individually - at least initially. Because debugging will be more complicated otherwise. Once you are more familiar with PHP/MySQL, then you can venture into the more complicated. The examples int he manual seem to provide a pretty good explanation in my opinion. As for books, I would suggest looking for some good online tutorials. A google search for "PHP MySQL Tutoria" brings up some good results. If anything doesn'ty make sense to you then you can post on these forums. Quote Link to comment Share on other sites More sharing options...
woodworks Posted August 18, 2008 Author Share Posted August 18, 2008 1. What is the best way to print the results of a mysqli_query to a browser That depends COMPLETELY on the context in which you want to display the results. Personally I suggest creating a static HTML page with "test" data and then creating your PHP to match. In some cases I would just hard-code the functioality to write the data to the page. In other's (such as a report listing) I might create a function or class to display the data tomaintain consistency. 2. How do you use MYSQLI_USE_RESULT and MYSQLI_STORE_RESULT? Those are used in instances where you run multiple queries at once. I never do that myself. I would suggest sticking with running queries individually - at least initially. Because debugging will be more complicated otherwise. Once you are more familiar with PHP/MySQL, then you can venture into the more complicated. The examples int he manual seem to provide a pretty good explanation in my opinion. As for books, I would suggest looking for some good online tutorials. A google search for "PHP MySQL Tutoria" brings up some good results. If anything doesn'ty make sense to you then you can post on these forums. Thank you for the response. I've found several tutorials online covering all sorts of different examples. The problem I am having is finding information that provides the rationale behind the code. I don't want to be a code monkey that simply regurgitates some else's code. I want to understand what the code is doing and the reasoning behind it (e.g., performance, fewer lines of code, security issues) so that I can make good choices when writing my own. The tutorials and forum posts on this site have been some of the most informative yet. I can't believe my Google searches the past few weeks have not listed this site before. I had to go down several pages in my latest search to even find the one which took me here. Let's hope the O'Reilly online MySQL and PHP courses that I am going to take are just as good. Who knows, maybe someday I'll be able to provide assistance to users of this forum, too. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 18, 2008 Share Posted August 18, 2008 Don't forget to use teh search function in this forum. If you were to find a post of someone asking how to display data (in a specific situation) chances are the responses will include more than one response which hopefully explain the "why". There is no one way to do anything in programming. There are always alternatives and which is the best one may not always be the same in different situations. 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.