Wrox Programmer Forums
|
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 July 14th, 2003, 07:05 PM
Authorized User
 
Join Date: Jul 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default SQL insert syntax

Hi,

I have a database of articles. Articles are categorised by topic. I'm trying to produce a list of these articles, grouped by their respective topics.


/* FIRST LOOP, RETRIEVE TOPIC NAMES */
$sql="SELECT DISTINCT topicID, topicName FROM topics";
$result=mysql_query($sql);
while($query_data = mysql_fetch_array($result))
{
echo $query_data['topicName']."<br>";

/* SECOND LOOP, RETRIEVE ARTICLES WHICH RELATE TO THE TOPIC */
$sql2="SELECT articles.articleID, title FROM articles INNER JOIN
articles_topics ON articles.articleID = articles_topics.articleID
WHERE articles_topics.topicID=$query_data['topicID']";
$result2=mysql_query($sql2);

while($query_data2 = mysql_fetch_array($result2)) {
echo $query_data2['title'];
}
}


I've substituted numbers in the SQL statements and tested them in phpMyAdmin where they work fine. But when I try to use the code above, I get "mysql_fetch_array(): supplied argument is not a valid MySQL result resource in the line, "while($query_data2 = mysql_fetch_array($result2))".

I think the problem actually lies in $sql2 where I use "$query_data['topicID']" in the WHERE clause - but I'm not sure. Can anyone tell me what's wrong?
 
Old July 15th, 2003, 01:04 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Your hunch is correct -- you're parsing a variable within a string, so you don't need the single quotes.

"SELECT ... topicID = $query_data[topicID]";

Alternatively, you can use curly-brace syntax:

"SELECT ... topicID = {$query_data['topicID']}";

Read the manual at http://www.php.net/types.string


Take care,

Nik
http://www.bigaction.org/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Syntax error when using "Insert Into" Luis Access VBA 14 November 18th, 2008 03:09 PM
Syntax Error on Insert dalezjc Classic ASP Basics 29 June 20th, 2007 06:50 PM
Syntax error INSERT INTO ITladybug ADO.NET 2 January 31st, 2006 07:50 AM
Syntax error in INSERT INTO statement. kingleon Classic ASP Basics 1 May 10th, 2005 06:25 PM
INSERT syntax error ss2003 PHP Databases 1 October 7th, 2004 05:30 PM





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