TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-20-2008, 10:44 AM   #1 (permalink)
The Contributor
 
marxx's Avatar
 
Join Date: Sep 2007
Location: Finland
Posts: 45
Thanks: 3
marxx is on a distinguished road
Default Does it matter how to code query?

I've seen couble ways ppl to code query but now I just have to ask it from here.

Below is three examples and question is: Does it really matter how to do it?

example 1:
Code:
$query = "SELECT * FROM ´mytable´ WHERE slogan = 'kicksass'";
$do_it = mysql_query($query) or die(mysql_error());

$showme = mysql_fetch_array($do_it);
example 2:
Code:
$query = mysql_query("SELECT * FROM ´mytable´ WHERE slogan = 'kicksass'") or die(mysql_error());

$showme = mysql_fetch_array($query);
example 3: (my fav because can do it in one line)
Code:
$showme = mysql_fetch_array(mysql_query("SELECT * FROM ´mytable´ WHERE slogan = 'kickass'")) or die(mysql_error());
Thanks for all.. =)
Send a message via MSN to marxx
marxx is offline  
Reply With Quote
Old 03-20-2008, 01:13 PM   #2 (permalink)
The Acquainted
 
freenity's Avatar
 
Join Date: Feb 2008
Posts: 119
Thanks: 17
freenity is on a distinguished road
Default

I guess there is no difference.
I use the example 2 method.

But sometimes you need to use example 1 method, for example if you have lets say a news.php file and the sections are defined by ?s var.

you have news.php?s=1 ; news.php?s=2, etc

so to select the section a possibility is using a switch, assign to a var say $query the query.
case1: $query = "select * from science";
case2: $query = "select * from sports"; ,etc

and then you just do:
mysql_query($query);

So it depends on what are you using it for.

The example 3 just make a die() (exit and prints an error msg.)
Might be fine using it in a small script, but in bigger sites it's better make a custom error handler.
__________________
http://feudal-times.net - My PBB Game
http://gwphp.feudal-times.net - My Blog "Gaming With PHP"
freenity is offline  
Reply With Quote
Old 03-20-2008, 11:32 PM   #3 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 166
Thanks: 0
wGEric is on a distinguished road
Default

Doesn't matter how you do it. How you do it is personal preference and your coding style.
__________________
Eric
wGEric is offline  
Reply With Quote
Old 03-22-2008, 04:41 PM   #4 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default

Quote:
Originally Posted by freenity View Post
you have news.php?s=1 ; news.php?s=2, etc

so to select the section a possibility is using a switch, assign to a var say $query the query.
case1: $query = "select * from science";
case2: $query = "select * from sports"; ,etc

and then you just do:
mysql_query($query);
A better way would be:
PHP Code:
// It's not about the sprintf, just about $szSection
$szQuery sprintf("SELECT * FROM %s"$szSection);
$pResult mysql_query($szQuery); 
This way you're not repeating peaces of code.

Anyways, ontopic:
I prefer the way I did the query above. This way, if a query is not working you can just do:
PHP Code:
$szQuery sprintf("SELECT * FROM %s"$szSection);
echo 
$szQuery;
exit();
$pResult mysql_query($szQuery); 
And see what's wrong with the query!

:)
maZtah is offline  
Reply With Quote
Old 03-20-2008, 11:51 PM   #5 (permalink)
The Acquainted
 
Gareth's Avatar
 
Join Date: Jan 2008
Posts: 136
Thanks: 4
Gareth is on a distinguished road
Default

Make sure you keep what ever you do consistent...
Gareth is offline  
Reply With Quote
Old 03-21-2008, 05:38 AM   #6 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default

People code things different ways because they like the syntax. Aside from that it all about the least amount of typing. Example one is Copy and Paste for everything except for the query, so you could put it into a function, while example 2 is good for an inline look at the query (so you don't have to reference to the function). Example three is just...
__________________
Signatures are nothing but incriminating.
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 03-21-2008, 12:41 PM   #7 (permalink)
The Acquainted
 
Gareth's Avatar
 
Join Date: Jan 2008
Posts: 136
Thanks: 4
Gareth is on a distinguished road
Default

To expand what I said earlier, I personally like the following:

PHP Code:
<?php

    $qQuery 
mysql_query"    SELECT * FROM
                                    my_db
                                WHERE
                                    something = often_a_variable
                                " 
);
                                
    
$rQuery mysql_fetch_array$qQuery );

?>
I do the same style each time.
Gareth is offline  
Reply With Quote
Old 03-22-2008, 07:07 PM   #8 (permalink)
The Acquainted
 
Join Date: Nov 2007
Posts: 154
Thanks: 31
SOCK is on a distinguished road
Default

I agree with maZtah; I prefer style #1, which keeps the SQL statement separate from the actual query call. If you need to look at the query or data embedded in it, it's simple to display the SQL statement by itself. If it's trapped within a function call (or worse, a couple of function calls), troubleshooting why the data isn't coming out the other end becomes exponentially more difficult. You wind up having to dissect into several pieces anyway.

Using sprintf() is also a good way to go, but don't rely on it alone to protect your database from SQL injection. Parameterized queries are also useful, if your database interface extension provides them.
__________________
I reject your reality, and substitute my own.
SOCK is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 08:14 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design