Jump to content


Photo

*SOLVED* Simple MySQL


  • Please log in to reply
7 replies to this topic

#1 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 14 April 2006 - 09:14 AM

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:
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();
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:
<?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>";
?>

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.
Think you're smarty?

(Gone until 20 to November)

#2 Yesideez

Yesideez
  • Members
  • PipPipPip
  • Advanced Member
  • 2,334 posts
  • LocationDevon, UK

Posted 14 April 2006 - 09:22 AM

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.
Not a pro just an enthusiast :)

if (empty($coffee)) {$coffee=new coffee();}

Please surround any code using the CODE tags - I rarely look at anything without them

#3 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 14 April 2006 - 09:28 AM

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.
Think you're smarty?

(Gone until 20 to November)

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 14 April 2006 - 09:31 AM

[!--quoteo(post=364730:date=Apr 14 2006, 10:22 AM:name=Yesideez)--][div class=\'quotetop\']QUOTE(Yesideez @ Apr 14 2006, 10:22 AM) View Post[/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 table 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] * FROM `table` [!--sql2--][/div][!--sql3--]

#5 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 14 April 2006 - 10:53 AM

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.
Think you're smarty?

(Gone until 20 to November)

#6 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 14 April 2006 - 01:46 PM

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 Tab key, or the one directly underneath the Esc key to inset a backtick).

You are doing 'table' instead of [!--sizeo:4--][span style=\"font-size:14pt;line-height:100%\"][!--/sizeo--][!--coloro:red--][span style=\"color:red\"][!--/coloro--]`[!--colorc--][/span][!--/colorc--][!--sizec--][/span][!--/sizec--]table[!--sizeo:4--][span style=\"font-size:14pt;line-height:100%\"][!--/sizeo--][!--coloro:red--][span style=\"color:red\"][!--/coloro--]`[!--colorc--][/span][!--/colorc--][!--sizec--][/span][!--/sizec--].

#7 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 14 April 2006 - 05:25 PM

Wow thanks alot!

Orio.
Think you're smarty?

(Gone until 20 to November)

#8 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 15 April 2006 - 08:02 AM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users