Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4019512
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:06:48+00:00 2026-05-20T10:06:48+00:00

Into a MYSQL query, I am trying to insert a WHERE clause (the one

  • 0

Into a MYSQL query, I am trying to insert a WHERE clause (the one that appears in the PHP comment below). Whenever I place this clause in the MYSQL query statement, it always fails (I’ve tried both before the LEFT JOIN and before the ORDER BY clauses).

Any ideas on how I can get my statement to work?

<?php
$result = mysql_query("SELECT * 
                         FROM Items 
                    LEFT JOIN Topics on Topics.TopicID = Items.FK_TopicID 
                     ORDER BY TopicSort, TopicName ASC, ItemSort, ItemTitle");
/* WHERE FK_UserID=$_SESSION[user_id] */
$topicname = false;

while($row = mysql_fetch_array($result)) {
    if (!$row['TopicID']) {
            $row['TopicName'] = 'Sort Me';
    }
if ($topicname != $row['TopicName']) {
    echo '<ul><li>' . $row['TopicName'] . '</li><ul>';
    $topicname = $row['TopicName'];
}
echo '';
echo '<li>' . $row['ItemTitle'] . '</li>';
echo '';
}
    if ($topicname != $row['TopicName']) {
    echo '</ul>';
    $topicname = $row['TopicName'];
}

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:06:49+00:00Added an answer on May 20, 2026 at 10:06 am

    SQL stands for Structured Query Language — you can’t put a WHERE clause wherever you want in the query. The WHERE clause has to be after the FROM/JOIN clauses, and before the GROUP BY, HAVING, and ORDER BY clauses. IE:

       SELECT * 
         FROM ITEMS i
    LEFT JOIN TOPICS t ON t.TopicID = i.FK_TopicID 
        WHERE FK_UserID = $_SESSION[user_id]
     ORDER BY TopicSort, TopicName ASC, ItemSort, ItemTitle
    

    A better option, to insulate yourself from SQL injection attacks, is to use sprintf:

    $sql = sprintf("SELECT * 
                      FROM ITEMS i
                 LEFT JOIN TOPICS t ON t.TopicID = i.FK_TopicID 
                     WHERE FK_UserID = %u
                  ORDER BY TopicSort, TopicName ASC, ItemSort, ItemTitle",
                   mysql_real_escape_string($_SESSION[user_id]));
    
    $result = mysql_query($sql);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a mysql insert like this one here <http://www.w3schools.com/php/php_mysql_insert.asp >
im trying to insert this query with mysql_query INSERT INTO um_group_rights (`um_group_id`,`cms_usecase_id`,`um_right_id`) VALUES (2,1,1)
I am trying to convert a mySQL query into a JSON object. This works:
I'm trying to mesh the below mysql query results into a single json object,
I have this MySQL query: $SQLquery = INSERT INTO `experimental`.`comments` (`comment_id`, `comment_dt`) VALUES (NULL,
I'm trying to INSERT INTO a mySQL database and I'm getting this error on:
I am trying to insert a mysql query itself into a table field(Executing a
I am trying to write a function that will insert data into a MySQL
I'm using code similar to the one below to insert into a mysql DB
I'm trying to insert text into a MySQL DataBase from a form using PHP.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.