SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: mySQL errors

  1. #1
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mySQL errors

    I've got no clue why this piece of code is generating an error. Everything else on the site works fine.

    Hope you all can help, and thanks in advance.

    Code:
    <?
    	###Get database functions and connect to DB
    	require("admin/sub.php");
    	db_connect();
    	
    	###Query the database and store retrieved data in $comment
    	$query = "SELECT * FROM data WHERE id = '$id' AND approved = '1'";
    	$result = mysql_query($query);
    	$comment = mysql_fetch_array($result);
    	
    	###Assign variables from data retrieved
    	$id = $comment["id"];
    	$title = $comment["title"];
    	$text = $comment["text"];
    	
    	###Display the data
    	if($id) {
    		echo "<b>$title</b><br /><br />";
    		
    		echo "$text<br /><br />";
    	} else {
    		echo "Error retrieving record<br /><br />";
    	}
    ?>

  2. #2
    SitePoint Addict
    Join Date
    Jul 2004
    Location
    The Caribbean
    Posts
    267
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What error is it outputting?

  3. #3
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $query = "SELECT * FROM data WHERE id = '$id' AND ...
    'data' is a MySQL Reserved Word so either renmae that column or backquote it, `data` ... i'd just rename it to avoid any confusion.

  4. #4
    SitePoint Zealot swiftone's Avatar
    Join Date
    Mar 2005
    Location
    Sunny Florida
    Posts
    145
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The error is probably coming from using the word "data", which is a reserved MySQL word.
    Happy Coding,
    Swiftone

    Unique Web Sites

  5. #5
    SitePoint Zealot swiftone's Avatar
    Join Date
    Mar 2005
    Location
    Sunny Florida
    Posts
    145
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for the redundant response. I must have been reading while markl999 was posting.
    Happy Coding,
    Swiftone

    Unique Web Sites

  6. #6
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It doesn't generate any errors, but the strange thing is the following code segment works perfectly.

    Code:
    	###Display all approved comments
    	$query = "SELECT * FROM data WHERE approved='1' ORDER BY `id` DESC";
    	$result = mysql_query($query);
    	
    	###Display the unapproved comments
    	while($comments = mysql_fetch_array($result)) {
    		$id = $comments["id"];
    		$title = $comments["title"];
    		
    		echo "...<a href=\"view.php?id=$id\" target=\"main\">$title</a>...<br /><br />";
    	}

  7. #7
    SitePoint Addict
    Join Date
    Jul 2004
    Location
    The Caribbean
    Posts
    267
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If it is not displaying an error, then what is wrong?

    Are you sure you have it setup to display errors to the screen? If not, you may need to either enable error displays or check your logs for the error message.

  8. #8
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem with that first bit is that it isn't getting any data from the database. I put in some mysql_error()'s, and still nothing showed up. Also, when I try to change data to 'data', I get some syntax errors.

    Here is the error I mentioned:
    Code:
    A fatal MySQL error occured.
    Query: SELECT * FROM 'data' WHERE id = '' AND approved = '1'
    Error: (1064) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''data' WHERE id = '' AND approved = '1'' at line 1

  9. #9
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, when I try to change data to 'data', I get some syntax errors.
    You have to use backquotes for reserved words, `data` rather than 'data'


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •