Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
| Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 25th, 2004, 03:48 PM
Authorized User
 
Join Date: Aug 2003
Location: Coeur d\'Alene, ID, USA.
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Tachyon
Default MySQL syntax in query

Code:
$query = "SELECT * FROM users WHERE userid = '" .
$_SESSION['userid']. "'";
That's a line from a PHP program that is working correctly but I'm wondering if there's a more concise way to type it. I'm still trying to figure out the right syntax for MySQL queries within PHP programs.

__________________
An overworked Web Developer who\'s expected to know everything yet given time to study nothing.
 
Old June 26th, 2004, 08:42 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

You can eliminate the creation of a $query variable and use curly syntax to reference the array in the query string.

mysql_query("SELECT * FROM users WHERE userid = '{$_SESSION['userid']}'");

You don't have to create a variable to run a query, you can just type it directly into the function. Remember from a resource management standpoint, memory has to be allocated for every variable that you create, not creating a variable eliminates that.

The $_SESSION variable can be included directly in the query using curly syntax, as I have done above.

Curly syntax is discussed in further detail here:
http://www.php.net/manual/en/language.types.string.php

The other thing is, unless you're using every field in the `users` table, you shouldn't use the wildcard selector, reference the fields that you are actually going to use. if you're just looking to see if the id exists in the field, then select only the users field. Here again, this just leads back to effective resource management, more resources have to be used to select every field, in contrast to only selecting what you need. Beyond that I don't see a problem with it.

Regards,
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::




Similar Threads
Thread Thread Starter Forum Replies Last Post
Ch3 Appendix MySQL Syntax Error amin7b5 BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 April 16th, 2008 01:49 PM
function syntax on Mysql robprell MySQL 0 May 15th, 2006 04:10 PM
Syntax Query digby_dog SQL Server 2000 2 September 8th, 2005 08:37 AM
Syntax error in query. Incomplete query clause. dispickle ADO.NET 3 April 16th, 2004 01:04 PM
QUERY SYNTAX singh_ginni Oracle 1 September 10th, 2003 07:38 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.