Orio Posted April 14, 2006 Share Posted April 14, 2006 Hello,I just started learning Mysql, so after reading a tutorial I tried to do it on my own.I've made a table called table with this script:[code]include("dbinfo.inc.php"); //un pw and db name heremysql_connect(localhost,$username,$password);@mysql_select_db($database) or die( "Unable to select database");$query="CREATE TABLE table (id int(4) NOT NULL auto_increment,Name varchar(15) NOT NULL,Rating int(2) NOT NULL,date varchar(7) NOT NULL,ip varchar(16) NOT NULL,PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))";mysql_query($query);mysql_close();[/code]I've entered some info to the table, and now I want to see everything.So I made this, but every time I run it I get an error. Here's the script:[code]<?phpinclude("dbinfo.inc.php");mysql_connect(localhost,$username,$password);@mysql_select_db($database) or die( "Unable to select database");$query="SELECT * FROM table";$result=mysql_query($query) or die (mysql_error());$rows = mysql_num_rows($result) or die(mysql_error()); mysql_close();echo "<b><center>Output</center></b><br><br>";?><table border="2" cellspacing="2" cellpadding="2"><tr> <td><font face="Arial, Helvetica, sans-serif">#</font></td><td><font face="Arial, Helvetica, sans-serif">Name</font></td><th><font face="Arial, Helvetica, sans-serif">Rating</font></td><td><font face="Arial, Helvetica, sans-serif">Date</font></td></tr><?php$i=0;while ($i < $rows) {$id=mysql_result($result,$i,"id");$name=mysql_result($result,$i,"Name");$rating=mysql_result($result,$i,"Rating");$date=mysql_result($result,$i,"date");?><tr> <td><font face="Arial, Helvetica, sans-serif"><?php echo "$id"; ?></font></td><td><font face="Arial, Helvetica, sans-serif"><?php echo "$name"; ?></font></td><td><font face="Arial, Helvetica, sans-serif"><?php echo "$rating"; ?></font></td><td><font face="Arial, Helvetica, sans-serif"><?php echo "$date"; ?></font></td></tr><?$i++;} echo "</table>";?>[/code]But I always get this error:"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 'table' at line 1."No idea what to do...Thanks,Orio. Quote Link to comment https://forums.phpfreaks.com/topic/7394-solved-simple-mysql/ Share on other sites More sharing options...
Yesideez Posted April 14, 2006 Share Posted April 14, 2006 You have created "TABLE" yet you're trying to access "table".Try and keep case the same as in some instances its case sensitive.Also best not to close the MySQL connection until the end of the script. Some MySQL functions need the connection to be open when called - closing it at the end of the script helps put an end to some otherwise problematic errors. Quote Link to comment https://forums.phpfreaks.com/topic/7394-solved-simple-mysql/#findComment-26908 Share on other sites More sharing options...
Orio Posted April 14, 2006 Author Share Posted April 14, 2006 I havent created a table called "TABLE", its called "table".Here:$query="CREATE TABLE table (...)";Anyway, its the same when I call the table "1". So thats not the problem.Orio. Quote Link to comment https://forums.phpfreaks.com/topic/7394-solved-simple-mysql/#findComment-26910 Share on other sites More sharing options...
wildteen88 Posted April 14, 2006 Share Posted April 14, 2006 [!--quoteo(post=364730:date=Apr 14 2006, 10:22 AM:name=Yesideez)--][div class=\'quotetop\']QUOTE(Yesideez @ Apr 14 2006, 10:22 AM) [snapback]364730[/snapback][/div][div class=\'quotemain\'][!--quotec--]You have created "TABLE" yet you're trying to access "table".[/quote]Actually that is not the case here, beacuse if MySQL couldn't find a TABLE called "table" then it'll be a complety different error message.What I would recommend you to do is to apply backticks to the word [b]table[/b] in your SELECT query as the word table is a predefined keyword inside MySQL. So MySQL is gettinga little confused. If you apply backtick MySQL will then treat the word table as-is. So change your query to this:[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] * [color=green]FROM[/color] [color=orange]`table`[/color] [!--sql2--][/div][!--sql3--] Quote Link to comment https://forums.phpfreaks.com/topic/7394-solved-simple-mysql/#findComment-26911 Share on other sites More sharing options...
Orio Posted April 14, 2006 Author Share Posted April 14, 2006 Hmm, I tried that and it still gives me the same error.I uploaded the whole file system to a rar file.If you want you can download it and find whats the problem... I would really appreciate it.File can be downloaded here:[a href=\"http://www.oriosriddle.com/mysql.rar\" target=\"_blank\"]Here[/a]Thanks,Orio. Quote Link to comment https://forums.phpfreaks.com/topic/7394-solved-simple-mysql/#findComment-26926 Share on other sites More sharing options...
wildteen88 Posted April 14, 2006 Share Posted April 14, 2006 Its not working because you are not use backticks but single quotes around the word table, Backticks are these: ` (hit the key just above the the [b]Tab[/b] key, or the one directly underneath the [b]Esc[/b] key to inset a backtick).You are doing 'table' instead of [!--sizeo:4--][span style=\"font-size:14pt;line-height:100%\"][!--/sizeo--][b][!--coloro:red--][span style=\"color:red\"][!--/coloro--]`[!--colorc--][/span][!--/colorc--][/b][!--sizec--][/span][!--/sizec--]table[!--sizeo:4--][span style=\"font-size:14pt;line-height:100%\"][!--/sizeo--][b][!--coloro:red--][span style=\"color:red\"][!--/coloro--]`[!--colorc--][/span][!--/colorc--][/b][!--sizec--][/span][!--/sizec--]. Quote Link to comment https://forums.phpfreaks.com/topic/7394-solved-simple-mysql/#findComment-26953 Share on other sites More sharing options...
Orio Posted April 14, 2006 Author Share Posted April 14, 2006 Wow thanks alot!Orio. Quote Link to comment https://forums.phpfreaks.com/topic/7394-solved-simple-mysql/#findComment-27020 Share on other sites More sharing options...
wildteen88 Posted April 15, 2006 Share Posted April 15, 2006 No problem. Just keep in mind that you should really surround your table names in backticks for good practice, however not many PHP programmers do this. Quote Link to comment https://forums.phpfreaks.com/topic/7394-solved-simple-mysql/#findComment-27186 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.