Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 136,162 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,890 people online right now. Registration is fast and FREE... Join Now!




whats wrong with this code?

 
Reply to this topicStart new topic

whats wrong with this code?, short sql query wont work

c0mrade
23 Dec, 2007 - 11:07 AM
Post #1

D.I.C Head
**

Joined: 16 Nov, 2007
Posts: 111



Thanked: 1 times
My Contributions
I have this short script to pull data from a mysql db.

CODE

<?php

include 'dbinfo.php';

//connect to db
$connection = mysql_connect("$DBHOST","$DBUSER","$DBPASS");
mysql_select_db("$DBNAME");

//get data
$sql = "SELECT title FROM #__content WHERE id='3';";
$sql_result = mysql_query($sql,$connection) or die(mysql_error());

echo $sql_result;
?>


but, when I run it, i get this very helpful error message
CODE

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 '' at line 1



please help, im still new to SQL but as far as I can tell my syntax is correct confused.gif
User is offlineProfile CardPM
+Quote Post

marcells23
RE: Whats Wrong With This Code?
23 Dec, 2007 - 12:19 PM
Post #2

D.I.C Head
Group Icon

Joined: 22 Aug, 2007
Posts: 134



Thanked: 3 times
Dream Kudos: 125
My Contributions
$sql = "SELECT title FROM #__content WHERE id='3';";

try removiing the semicolon after 3

$sql = "SELECT title FROM #__content WHERE id='3'";
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Whats Wrong With This Code?
23 Dec, 2007 - 01:48 PM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Is id a numeric column? If so, take the quotes off. $sql = "SELECT title FROM #__content WHERE id=3;";

User is offlineProfile CardPM
+Quote Post

girasquid
RE: Whats Wrong With This Code?
23 Dec, 2007 - 02:02 PM
Post #4

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,266



Thanked: 14 times
Dream Kudos: 650
My Contributions
I might be totally out to lunch here, but could the fact that your table is named #__content be an issue?

If someone knows this definitively, please let me know - I have no idea if it could be causing an issue or not.
User is offlineProfile CardPM
+Quote Post

snoj
RE: Whats Wrong With This Code?
23 Dec, 2007 - 05:54 PM
Post #5

$Null
Group Icon

Joined: 31 Mar, 2003
Posts: 3,304



Thanked: 6 times
Dream Kudos: 700
My Contributions
marcells23, the semicolon is not the problem. In fact, SQL statements should be followed by a semicolon.

baavgai, MySQL, like PHP, doesn't much care. It will automatically convert a numeric number from a string to an integer when need be.

girasquid, you're not out to lunch. The pound sign (#) is for line comments in MySQL.
User is offlineProfile CardPM
+Quote Post

marcells23
RE: Whats Wrong With This Code?
23 Dec, 2007 - 08:29 PM
Post #6

D.I.C Head
Group Icon

Joined: 22 Aug, 2007
Posts: 134



Thanked: 3 times
Dream Kudos: 125
My Contributions
QUOTE(snoj @ 23 Dec, 2007 - 08:54 PM) *

marcells23, the semicolon is not the problem. In fact, SQL statements should be followed by a semicolon.



http://us2.php.net/function.mysql-query

maybe im confused but i just looked here and it said sql queries dont end with a semicolon and php does indeed take care of it for you when dealing with one line queries
User is offlineProfile CardPM
+Quote Post

snoj
RE: Whats Wrong With This Code?
28 Dec, 2007 - 08:47 PM
Post #7

$Null
Group Icon

Joined: 31 Mar, 2003
Posts: 3,304



Thanked: 6 times
Dream Kudos: 700
My Contributions
While the manual does state that, it does only process the text before the semi-colon (provided it wasn't in a string).
User is offlineProfile CardPM
+Quote Post

quim
RE: Whats Wrong With This Code?
30 Dec, 2007 - 11:09 AM
Post #8

D.I.C Head
Group Icon

Joined: 11 Dec, 2005
Posts: 145



Thanked: 2 times
Dream Kudos: 350
My Contributions
CODE
$sql = "SELECT title FROM #__content WHERE id='3';";


Here is the correction:
CODE
$sql = "SELECT title FROM content WHERE id = 3";


u cannot use #
# is for comment so, the mySql statement that you have there is
SELECT title FROM
you are no allowed to use ; to end mySql statement inside php
if id column is not a string have it as id = 3 instead of id = '3'.
i dont know if mySql would automatic convert id to numeric, but anyways this is the right way to do it.
"dont expect for the database do correct your mistake"




User is offlineProfile CardPM
+Quote Post

amrish_indianic
RE: Whats Wrong With This Code?
4 Jan, 2008 - 01:25 AM
Post #9

New D.I.C Head
*

Joined: 4 Jan, 2008
Posts: 2

$sql = "SELECT title FROM content WHERE id = 3";
User is offlineProfile CardPM
+Quote Post

darklighter
RE: Whats Wrong With This Code?
5 Jan, 2008 - 07:52 AM
Post #10

New D.I.C Head
*

Joined: 5 Jan, 2008
Posts: 18


My Contributions
Lol, you're all looking at the query, did no one notice his incorrect syntax for include?
Btw, you shouldn't use include, use Require instead.

Some ideas to help you fix this:
- Make sure that #__content is the correct table name.
- Remove the semi-colon after the '3' in the query (id='3';)
- You cannot just echo $sql_result , i added a line of code to it first.

CODE

<?php

require('dbinfo.php');

//connect to db
$connection = mysql_connect("$DBHOST","$DBUSER","$DBPASS");
mysql_select_db("$DBNAME");

//get data
$sql = "SELECT title FROM #__content WHERE id='3'";
$sql_result = mysql_query($sql,$connection) or die(mysql_error());
$sqlvars = mysql_fetch_array($sql_result);
echo $sqlvars['title'];
?>


but, when I run it, i get this very helpful error message
CODE

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 '' at line 1


That error is definitely pointing at that semi-colon at (id='3';)

Hope my ideas help. (and yea i know some people mentioned some of them earlier already, i hope i made them clearer.)

~Darklighter

User is offlineProfile CardPM
+Quote Post

quim
RE: Whats Wrong With This Code?
5 Jan, 2008 - 08:02 PM
Post #11

D.I.C Head
Group Icon

Joined: 11 Dec, 2005
Posts: 145



Thanked: 2 times
Dream Kudos: 350
My Contributions
blink.gif i didn't notice any error with his include statement unless the path is invalid.
QUOTE
The include() statement includes and evaluates the specified file.
The documentation below also applies to require(). The two constructs are identical in every way except how they handle failure. They both produce a Warning, but require() results in a Fatal Error. In other words, use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless. Be sure to have an appropriate include_path setting as well. Be warned that parse error in included file doesn't cause processing halting in PHP versions prior to PHP 4.3.5. Since this version, it does.

http://us3.php.net/manual/en/function.include.php

...and also have you tried to make a table with the name #__content, if so you may have notice the error. as i have mentioned before # is for commenting single line in mySql. so what every comes after it in a line is a comment icon_down.gif


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 11:52PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month