SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
Thread: jokes.php, p77-79 PHP/MySQL book
-
Aug 1, 2007, 08:07 #1
- Join Date
- Feb 2007
- Location
- Southern California
- Posts
- 1,388
- Mentioned
- 1 Post(s)
- Tagged
- 1 Thread(s)
jokes.php, p77-79 PHP/MySQL book
I was studying the code line by line for jokes.php on pages 77-79 of your PHP/MySQL book.
I notice in one place you made the query into a variable $sql. Later on you did the query straight without a variable. Is there a reason you made the query a variable the first time?
Query is a variable:
Code:if (isset($_POST['joketext'])) { $joketext = $_POST['joketext']; $sql = "INSERT INTO joke SET joketext='$joketext', jokedate=CURDATE()"; if (@mysql_query($sql)) { echo '<p>Your joke has been added.</p>';
Code:$result = @mysql_query('SELECT joketext FROM joke'); if (!$result) { exit('<p>Error performing query: ' . mysql_error() . '</p>'); }
Steve
-
Aug 1, 2007, 08:43 #2
- Join Date
- Jul 2007
- Location
- London
- Posts
- 161
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
as far as i know if you define a variable you can use it later on without redefining it. As long as the variable is defined before you make a call for it then it should work
-
Aug 1, 2007, 08:52 #3
- Join Date
- Feb 2007
- Location
- Southern California
- Posts
- 1,388
- Mentioned
- 1 Post(s)
- Tagged
- 1 Thread(s)
I agree. However, both queries were at the end of the code. $sql was not referenced after it was introduced the first time. I suspect it was coded that way out of habit.
Could this $sql be accessed from another .php page? There's no $_POST linked to it, so I don't think so.
I'm going over everything the third or fourth time because I'm trying to understand every line of code I put down.
Thanks!
Steve
-
Aug 1, 2007, 16:43 #4
- Join Date
- Apr 2000
- Location
- Melbourne, Australia
- Posts
- 2,571
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
There is no particular reason to choose one over the other, Steve, although technically the approach that does not use a variable will be ever so slightly faster.
In the examples you cited, I'd say it just came down to the length of the SQL query—if I had tried to cram the first SQL query into the mysql_query call, it would have made the code more difficult to read, whereas the second query was short enough to fit comfortably.Kevin Yank
CTO, sitepoint.com
I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
Baby’s got back—a hard back, that is: The Ultimate CSS Reference
-
Aug 2, 2007, 07:26 #5
- Join Date
- Feb 2007
- Location
- Southern California
- Posts
- 1,388
- Mentioned
- 1 Post(s)
- Tagged
- 1 Thread(s)
Thanks, Kevin! You opted in favor of the reader!
Best wishes,
Steve
Bookmarks