And how was that? cPanel? Using phpMyAdmin's table wizard? Running a query from phpMyAdmin? Running a query from a script? Something else? When asking for help, be precise and informative.I tried to create a table like I use to do
And how was that? cPanel? Using phpMyAdmin's table wizard? Running a query from phpMyAdmin? Running a query from a script? Something else? When asking for help, be precise and informative.
CREATE TABLE `XXXXX_test`.`test01` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`check` INT NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM ;
mysql_query("CREATE TABLE subscr(`ID` varchar(30),`check` varchar(5))");
mysql_query("INSERT INTO subscr(`ID`,`check`) VALUES ('Daniel','Yes')");
if (FALSE === mysql_query(...)) {
// query failed; log an error
...
} else {
// continue to next step
...
}
, [php] and [html] tags when posting to delineate code and preserve whitespace.)
If the script is to be accessible by anyone other than site admins, make sure you [URL="http://msdn.microsoft.com/en-us/library/ms995351.aspx#securityerrormessages_topic2"]don't disclose too much information[/URL] in the error message. Simply printing the result of [URL="http://php.net/mysql_error"][FONT="Courier New"]mysql_error()[/FONT][/URL] is definitely too much information.
Switching to the newer [URL="http://php.net/PDO"]PDO[/URL] driver lets you make use of exceptions, simplifying error handling because you don't need to individually test each query.
[php]// manage DB connection in some other file named (e.g.) "localDB.php" with something like:
function localDBConnection() {
static $db = new PDO('mysql:host=localhost;dbname=...', 'username', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $db;
}
// in the script
include("localDB.php");
try {
$db = localDBConnection();
$db->query($stmt='CREATE TABLE subscr(ID varchar(30),check varchar(5))');
$db->query($stmt="INSERT INTO subscr(ID,check) VALUES ('Daniel','Yes')");
} catch (PDOException $exc) {
echo "Query '$stmt' failed: ", $exc;
}
[/php]
PDO also supports [URL="http://www.php.net/manual/en/pdo.prepared-statements.php"]prepared statements[/URL], which are more efficient (you can execute a prepared statement more than once) and easier to secure (prepared statement parameters aren't vulnerable to [url=http://unixwiz.net/techtips/sql-injection.html]SQL injection[/url]) than executing a string as a query.
[quote="garikr, post: 638280"]Any other languages accept [COLOR="red"]`[/COLOR] in their syntax??[/QUOTE]
MySQL is the only [URL="http://en.wikipedia.org/wiki/Relational_database_management_system"]RDBMS[/URL] which uses backticks for [URL="http://dev.mysql.com/doc/refman/5.1/en/identifiers.html"]quoting identifiers[/URL] (what are officially called "delimited identifiers") that I know of. SQL Server uses square brackets. The SQL standard specifiers double-quotes.
Note that identifiers are a different namespace from strings, so you can't use backticks in place of other quote characters.
// manage DB connection in some other file named (e.g.) "localDB.php" with something like:
function localDBConnection() {
static $db = new PDO('mysql:host=localhost;dbname=...', 'username', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $db;
}
// in the script
include("localDB.php");
try {
$db = localDBConnection();
$db->query($stmt='CREATE TABLE `subscr` (`ID` varchar(30),`check` varchar(5))');
$db->query($stmt="INSERT INTO `subscr` (`ID`,`check`) VALUES ('Daniel','Yes')");
} catch (PDOException $exc) {
echo "<Some error message like Invalid input. Please contact site administrator if you feel this is incorrect> ", $exc;
}
It does, which is the point: to display an error message so OP has a clue what's going wrong.@misson, Though I haven't tested your piece of code But I guess it will still face the same problem of check being a keyword
Good point; see the link in my previous post about information disclosure. Even displaying an SQL error message is a security risk. However, DDL statements (table creates & drops) should only be executed for admins, so (in this case) the statement and error aren't being displayed to anyone who shouldn't see them.also Displaying the complete Query when it fails imposes too much of risk of displaying the tablename and fields of the table being managed.