The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Error while creating table at mySql (noob)
Discuss Error while creating table at mySql (noob) in the PHP Development forum on Dev Shed. Error while creating table at mySql (noob) PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 15th, 2013, 01:20 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 9
Time spent in forums: 3 h 3 m 47 sec
Reputation Power: 0
|
|
|
Error while creating table at mySql (noob)
Hi guys i am getting this error
mysqli_error() expects exactly 1 parameter, 0 given
at this line
Code:
echo "Error creating table: " . mysqli_error();
here is the complete code:
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$con=mysqli_connect("127.0.0.1:3306","root", "", "project2");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Create database
$sql="CREATE DATABASE proj2";
if (mysqli_query($con,$sql))
{
echo "Database proj2 created successfully";
}
else
{
echo "Error creating database: " . mysqli_error();
}
// Create table
$sql="CREATE TABLE contacts
(
ID INT(5) NOT NULL AUTO_INCREMENT,
primay key(ID),
Firstname CHAR(30),
Lastname CHAR(30), Phone VARCHAR(45),
Email VARCHAR(45))";
// Execute query
if (mysqli_query($con,$sql))
{
echo "Table persons created successfully";
}
else
{
echo "Error creating table: " . mysqli_error();
}
?>
</body>
</html>
Any solutions ?
|

March 15th, 2013, 01:53 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|

March 15th, 2013, 01:54 PM
|
|
|
You have mixed syntax.
PHP Code:
echo "Error creating table: " . mysqli_error;
or
PHP Code:
echo "Error creating table: " . mysqli_error($con);
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.
|

March 15th, 2013, 02:12 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 9
Time spent in forums: 3 h 3 m 47 sec
Reputation Power: 0
|
|
|
One more question guys, how can i check if database or table exists and if yes delete and make new ones ?
|

March 15th, 2013, 02:50 PM
|
|
|
|
MySQL has a built in 'if exists' syntax. Look it up.
|

March 15th, 2013, 03:01 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 9
Time spent in forums: 3 h 3 m 47 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by gw1500se MySQL has a built in 'if exists' syntax. Look it up. |
i try this
PHP Code:
mysql_query("DROP TABLE IF EXISTS contacts");
over create table command but it does not works i get this error
Warning: mysqli_error() expects exactly 1 parameter, 0 given in D:\xampp\htdocs\project2.php on line 32
Error creating database: Error creating table: Table 'contacts' already exists
|

March 15th, 2013, 03:04 PM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
|
Again, your error about mysqli_error is unrelated to the line about mysql_query. mysqli_error expects an argument. Give it one.
Why are you even dropping and recreating a table? You can just "DELETE FROM CONTACTS;"
Also, your script calls the table by two different names.
__________________
HEY! YOU! Read the New User Guide and Forum Rules
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002
Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
|

March 15th, 2013, 03:23 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 9
Time spent in forums: 3 h 3 m 47 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by ManiacDan Again, your error about mysqli_error is unrelated to the line about mysql_query. mysqli_error expects an argument. Give it one
Can you be more specific on this ?
Why are you even dropping and recreating a table? You can just "DELETE FROM CONTACTS;"
i want to check if database or table already exists and drop it, because if i change something to my script and reload, i must go to mysql workbench and drop proj2 database all the time.
If would like to hear a soluthion for this, if there is any
Also, your script calls the table by two different names.
Where is this hapenning ? |
Really thanks for your help and for your time !
|

March 15th, 2013, 03:52 PM
|
|
|
|
I already gave you the solution for the mysqli_error problem. Pick one.
|

March 18th, 2013, 07:51 AM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
Quote: Can you be more specific on this ? | As GW said, you've been given this solution. You were handed this solution even after being linked to the manual page for it.
Quote: i want to check if database or table already exists and drop it, because if i change something to my script and reload, i must go to mysql workbench and drop proj2 database all the time.
If would like to hear a soluthion for this, if there is any | You're saying "database" now instead of "table." If you're dropping an entire database every time you alter your script, you're doing it wrong. Set up your data structure first, and make your scripts flexible enough so that new columns won't break them. However, the DROP TABLE syntax should still work regardless.
Quote: | Where is this hapenning ? | Your success message says "persons", your query says "contacts"
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|