Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 14th, 2003, 07:05 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
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?
Reply With Quote
  #2 (permalink)  
Old July 15th, 2003, 01:04 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
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/
Reply With Quote
Reply


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

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


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



All times are GMT -4. The time now is 04:10 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.