FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


MySQL error





awesty
what is wrong with this code?

Code:

<?php
$con = mysql_connect("localhost","awesty","*******");
if(!$con)
{
die('Could not connect: ' . mysql_error());
}

$sql = "CREATE DATABASE my_db";
if(mysql_query($sql,$con))
{
echo "Database my_db Created";
}
else
{
echo "Error Creating Database: " . mysql_error();
}

mysql_select_db("my_db",$con);

$isql = "CREATE TABLE persons
(
id int NOT NULL AUTO_INCREMENT;
PRIMARY KEY(id);
FirstName varchar(15);
LastName varchar(15);
Age int;
)";

mysql_query($isql,$con);

$asql("INSERT INTO persons
(FirstName, LastName, Age)
VALUES
('Peter', 'Griffen', '35')");

mysql_query("INSERT INTO persons
(FirstName, LastName, Age)
VALUES
('Glenn', 'Quagmire', '33')");

mysql_query("INSERT INto persons
(FirstName, LastName, Age)
VAlUES
('$_POST[firstname]', '$_POST[lastname]', '$_POST[age]')");
if (!mysql_query($asql, $con))
{
die('Error: ' . mysql_query());
}
echo "Success";

mysql_close();
?>

<?php
$con = mysql_connect("localhost", "awesty", "**********");
if(!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select("my_db", $con);

$result = mysql_query("SELECT * FROM persons");

while($row = mysql_fetch_array($result));
{
echo $row['FirstName'];
echo "<br />";
echo $row['LastName'];
echo "<br />";
}
?>


this is the error message im getting:
MySQL wrote:

Error Creating Database: Access denied for user: 'awesty@localhost' to database 'my_db'
Fatal error: Call to undefined function: () in /home/awesty/domains/flashuser.frih.net/public_html/stuff/asd.php on line 31


any help would be apriciated!

thanks in advance Smile
mathiaus
The username you'r using is wrong. You dont actually have a username setup I think because you have to create the databases manualy not through the script.

You need to
Create a database through Direct admin
change the username in the script
remove the create a database part
awesty
ah, now i get it. thanks for the help Smile

EDIT: i am now getting this message:
Quote:

Fatal error: Call to undefined function: create table persons ( id int not null auto_increment; primary key(id); firstname varchar(15); lastname varchar(15); age int; )() in /home/awesty/domains/flashuser.frih.net/public_html/stuff/asd.php on line 21


and this is the code:
Code:

<?php
$con = mysql_connect("localhost","awesty_data","*******");
if(!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("awesty_data",$con);

$isql = "CREATE TABLE persons
(
id int NOT NULL AUTO_INCREMENT;
PRIMARY KEY(id);
FirstName varchar(15);
LastName varchar(15);
Age int;
)";

mysql_query($isql,$con);

$isql("INSERT INTO persons
(FirstName, LastName, Age)
VALUES
('Peter', 'Griffen', '35')");

mysql_query("INSERT INTO persons
(FirstName, LastName, Age)
VALUES
('Glenn', 'Quagmire', '33')");

mysql_query("INSERT INto persons
(FirstName, LastName, Age)
VAlUES
('$_POST[firstname]', '$_POST[lastname]', '$_POST[age]')");
if (!mysql_query($isql, $con))
{
die('Error: ' . mysql_query());
}
echo "Success";

mysql_close();
?>

<?php
$con = mysql_connect("localhost", "awesty_data", "******");
if(!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select("awesty_data", $con);

$result = mysql_query("SELECT * FROM persons");

while($row = mysql_fetch_array($result));
{
echo $row['FirstName'];
echo "<br />";
echo $row['LastName'];
echo "<br />";
}
?>
DanielXP
use this to make the tables (edit it to make the others) this is instead of the first one

Code:
mysql_query("
CREATE TABLE `persons` (
`id` int(10) NOT NULL auto_increment,
`FirstName` varchar(15);
`LastName` varchar(15);
`Age` int(10)
PRIMARY KEY (`id`)
) TYPE = MYISAM
") or die(mysql_error());
awesty
okay, thanks. but would you mind explaining the difference for me?

thanks.

EDIT: now i am getting this message
Quote:

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 '; `LastName` varchar(15); `Age` int(10) PRIMARY KEY (`id`) ) TY


and this is the code
Code:

<?php
$con = mysql_connect("localhost","awesty_data","*****");
if(!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("awesty_data",$con);

mysql_query("
CREATE TABLE `persons` (
`id` int(10) NOT NULL auto_increment,
`FirstName` varchar(15);
`LastName` varchar(15);
`Age` int(10)
PRIMARY KEY (`id`)
) TYPE = MYISAM
") or die(mysql_error());

mysql_query("INSERT INTO `persons`
(FirstName, LastName, Age)
VALUES
('Peter', 'Griffen', '35')");

mysql_query("INSERT INTO `persons`
(FirstName, LastName, Age)
VALUES
('Glenn', 'Quagmire', '33')");

mysql_query("INSERT INTO 'persons'
(FirstName, LastName, Age)
VAlUES
('$_POST[firstname]', '$_POST[lastname]', '$_POST[age]')");
if (!mysql_query($isql, $con))
{
die('Error: ' . mysql_query());
}
echo "Success";

mysql_close();
?>

<?php
$con = mysql_connect("localhost", "awesty_data", "*****");
if(!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select("awesty_data", $con);

$result = mysql_query("SELECT * FROM persons");

while($row = mysql_fetch_array($result));
{
echo $row['FirstName'];
echo "<br />";
echo $row['LastName'];
echo "<br />";
}
?>


sorry for all the trouble Embarassed
hexkid
awesty wrote:
Code:
// ...
mysql_query("
CREATE TABLE `persons` (
`id` int(10) NOT NULL auto_increment,
`FirstName` varchar(15);
`LastName` varchar(15);
`Age` int(10)
PRIMARY KEY (`id`)
) TYPE = MYISAM
") or die(mysql_error());
// ...


You have too many semicolons in your SQL command.
awesty
hexkid wrote:
awesty wrote:
Code:
// ...
mysql_query("
CREATE TABLE `persons` (
`id` int(10) NOT NULL auto_increment,
`FirstName` varchar(15);
`LastName` varchar(15);
`Age` int(10)
PRIMARY KEY (`id`)
) TYPE = MYISAM
") or die(mysql_error());
// ...


You have too many semicolons in your SQL command.


so which ones should i take out Confused
hexkid
awesty wrote:
hexkid wrote:
You have too many semicolons in your SQL command.


so which ones should i take out Confused


Within PHP, semicolons should never be used for SQL statements.
You use semicolons to separate statements in tools that allow multiple statements per instruction; the "mysql_query()" in PHP accepts only a single statement and therefore does not need semicolons (though you may, redundantly, finish the statement with one).

Take all semicolons out of the statement!
awesty
hexkid wrote:
awesty wrote:
hexkid wrote:
You have too many semicolons in your SQL command.


so which ones should i take out Confused


Within PHP, semicolons should never be used for SQL statements.
You use semicolons to separate statements in tools that allow multiple statements per instruction; the "mysql_query()" in PHP accepts only a single statement and therefore does not need semicolons (though you may, redundantly, finish the statement with one).

Take all semicolons out of the statement!


that doesnt work Sad
hexkid
awesty wrote:
hexkid wrote:
Take all semicolons out of the statement!


that doesnt work Sad


My bad, sorry. I meant "replace all semicolons with commas" Smile

Code:
mysql_query("
CREATE TABLE `persons` (
`id` int(10) NOT NULL auto_increment,
`FirstName` varchar(15), -- comma
`LastName` varchar(15), -- comma
`Age` int(10)
PRIMARY KEY (`id`)
) TYPE = MYISAM
") or die(mysql_error());
awesty
yea, it still doesnt work Crying or Very sad
hexkid
awesty wrote:
yea, it still doesnt work Crying or Very sad


Try this
Code:
mysql_query("
CREATE TABLE `persons` (
`id` int(10) NOT NULL auto_increment,
`FirstName` varchar(15),
`LastName` varchar(15),
`Age` int(10), -- new comma here
PRIMARY KEY (`id`)
) TYPE = MYISAM
") or die(mysql_error());



I like to do queries like this instead
Code:
$sql = "
CREATE TABLE `persons` (
`id` int(10) NOT NULL auto_increment,
`FirstName` varchar(15),
`LastName` varchar(15),
`Age` int(10), -- new comma here
PRIMARY KEY (`id`)
) TYPE = MYISAM
";
mysql_query($sql) or die('Error in query: [<b>' . $sql . '</b>]. The error was: ' . mysql_error());

This way I can copy/paste the SQL to a command-line MySQL client and tweak the command until it does what I want.
awesty
ive solved that problem. i have a new one now but i cant be bothered posting it Confused

thanks alot for your help Very Happy
Related topics
Mysql error!
MySQL error 1604
[solved] Weird mysql error
Suppressing mysql error
[mySQL] Error I cannot Debug
MySql error: Column count doesn't match value count at row 1
Mysql Error
Error connecting to mysql ; please help
Mysql error: Warning: mysql_num_rows():
mysql error
MySQL error
Urgent! MySQL error!
MYSQL Ajax im Installation Error
Mysql ERROR
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.