error with my mysql statement

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
afbase
Forum Contributor
Posts: 113
Joined: Tue Aug 15, 2006 1:29 pm
Location: SoCAL!!!!

error with my mysql statement

Post by afbase »

can anyone tell me what the heck is wrong with my MYsql statement?

Code: Select all

$mysql_tickname_insert="INSERT INTO curldata VALUES ($ticker,$companyname,'','','','','','');";
my error is 
[quote]Error number: 1064 Message: 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 '1838 Bond Debenture Trading Fd,'','','','','','')' at line 1[/quote]
i'm inserting it into my table curldata that has 8 fields.  I don't havevalues to insert for the last six values.  all fields are VARCHAR.

okay here is my php code from my php class:
[syntax=php]function get_ticker($text_ticker){
        $file = "NYE.txt";
        $fc=file($file);
		foreach ($fc as $line_num => $pline){
			$spline=explode("          ",$pline);
				foreach ($spline as $key => $value){
					if ($key == 0){
						$companyname = $value;

}
					else if ($key == 1){
						$ticker = $value;

}
			$mysql_tickname_insert="INSERT INTO curldata VALUES ($ticker,$companyname,'','','','','','');";
if ( $sqldb = mysql_connect( "localhost", "localuser", "localpass" ) ) {
	   print "Connection to mysqldb username: localuser complete<br>";
}
	else {
	   die ("Unable to connect to mysqldb username: localuser . Error: <b>".
                                                                    mysql_error()."</b>");
}

	if ( mysql_select_db( intelligent , $sqldb )  ) {
	  print "Select of intelligent complete<br>";
}
	else {
	  die ("Select failed database name intelligent Error number: <b>".mysql_errno().
                                                           " Message: ".mysql_error()."</b>");
}

	if ( mysql_query( $mysql_tickname_insert, $sqldb )  ) {
	  print $mysql_tickname_insert." complete<br>";
}
	else {
	  print  $mysql_tickname_insert." failed. Error number: <b>".mysql_errno().
                                          " Message: ".mysql_error()."</b>";
}
mysql_close($sqldb);
}
}

//end of get ticker
}[/syntax][syntax=php][/syntax][syntax="sql"][/syntax][quote][/quote]
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

Post your table schema so as to know the table fields with their data type.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

You aren't quoting the inserted values. If they are anything other than NULL, or a number you need to quote them.

Code: Select all

// Good way
$string = mysql_real_escape_string($string); // this is good for security 
$query = "INSERT INTO table VALUES ('$string')"; // < see single quotes around variable

Code: Select all

// Bad way
$query = "INSERT INTO table VALUES ($string)"; // < see no quotes
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I think ole got it. Whenever you insert data that is not NULL or a number, it is considered a string (sort of, but for this little lesson we can get away with it). All string types need to be quoted when inserted or updated.
afbase
Forum Contributor
Posts: 113
Joined: Tue Aug 15, 2006 1:29 pm
Location: SoCAL!!!!

Problem Solved

Post by afbase »

ole did get the problem solved! Thanks guys, Merry Christmas and Happy New Year!
Post Reply