Jump to content

Archived

This topic is now archived and is closed to further replies.

Orio

*SOLVED* Simple MySQL

Recommended Posts

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 here
mysql_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]
<?php
include("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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[!--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--]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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--].

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.