Syntax error, unexpected TEXT_STRING (Wamp/MySQL Workbench) - php

My dad-in-law's webserver broke down yesterday, and we are trying to get a new one up running.
We transfered the frm/MYD/MYI files to my computer, and I have now managed to restore the sql files and tables. But when I tried to launch the page, it just showed up a load of gibberish.
I then opened the files in MySQL Workbench and found out that all (') had been replaced with (`)'s.
So after painstakingly replacing them, I tried to forward engineer again, but still the same gibberish was shown. I checked the code again and noticed that I'm getting this error, all the way through the code at 'x':
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
CREATE SCHEMA IF NOT EXISTS 'x' DEFAULT CHARACTER SET latin1 ;
USE 'x' ;
-- -----------------------------------------------------
-- Table 'x'.'configuration'
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS 'x'.'configuration' (
'config_item' VARCHAR(64) NOT NULL DEFAULT '',
'config_value' VARCHAR(255) NOT NULL DEFAULT '')
ENGINE = MyISAM
DEFAULT CHARACTER SET = latin1;
Any idea what the heck I'm doing wrong?
#Sirko
Sooooo, like this?
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
CREATE SCHEMA IF NOT EXISTS `x` DEFAULT CHARACTER SET latin1 ;
USE `x` ;
-- -----------------------------------------------------
-- Table `x`.`configuration`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `x`.`configuration` (
`config_item` VARCHAR(64) NOT NULL DEFAULT '',
`config_value` VARCHAR(255) NOT NULL DEFAULT '')
ENGINE = MyISAM
DEFAULT CHARACTER SET = latin1;

Try this:
CREATE TABLE IF NOT EXISTS `x`.`configuration` (
`config_item` VARCHAR(64) NOT NULL DEFAULT '',
`config_value` VARCHAR(255) NOT NULL DEFAULT '')
ENGINE = MyISAM
DEFAULT CHARACTER SET = latin1;

Related

The phpMyAdmin configuration storage is not completely configured, some extended - Xammp - Version -7.3.8-0-VC15

I updated Xampp version - Latest version = 7.3.8-0-VC15
I am getting these errors -
Error 1-
The phpMyAdmin configuration storage is not completely configured, some extended features have been deactivated. Find out why.
I clicked on to find out why but did not understand that -
Error 2 -
mysqli_real_connect(): (HY000/1045): Access denied for user 'pma'#'localhost' (using password: NO)
Error 3-
Connection for controluser as defined in your configuration failed.
I tried many solutions but not solved.
It's a mistake for missing some defaults table for default phpmydamin database. For solve this mistake you must to run following queries in phpmyadmin database
I suggest you download and install JetBrains DataGrip and connect this software with your local database and run following query on it to fixing phpmyadmin problem. another way is if you are friendly with command line log in to mysql with command line and run queries.
CREATE TABLE IF NOT EXISTS `pma_users` (
`username` varchar(64) NOT NULL,
`usergroup` varchar(64) NOT NULL,
PRIMARY KEY (`username`,`usergroup`)
)
COMMENT='Users and their assignments to user groups'
DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
CREATE TABLE IF NOT EXISTS `pma_usergroups` (
`usergroup` varchar(64) NOT NULL,
`tab` varchar(64) NOT NULL,
`allowed` enum('Y','N') NOT NULL DEFAULT 'N',
PRIMARY KEY (`usergroup`,`tab`,`allowed`)
)
COMMENT='User groups with configured menu items'
DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
CREATE TABLE IF NOT EXISTS `pma_navigationhiding` (
`username` varchar(64) NOT NULL,
`item_name` varchar(64) NOT NULL,
`item_type` varchar(64) NOT NULL,
`db_name` varchar(64) NOT NULL,
`table_name` varchar(64) NOT NULL,
PRIMARY KEY (`username`,`item_name`,`item_type`,`db_name`,`table_name`)
)
COMMENT='Hidden items of navigation tree'
DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
Then add following code to config.inc.php file:
$cfg['Servers'][$i]['users'] = 'pma_users';
$cfg['Servers'][$i]['usergroups'] = 'pma_usergroups';
$cfg['Servers'][$i]['navigationhiding'] = 'pma_navigationhiding';

Cannot store emoji in database

THE SITUATION:
Sorry in advance if this question has already been asked, but the solutions aren't working for me.
No matter what I try, I cannot store emoji in my database. They are saved as ????.
The only emojis that are properly saved are the ones that require only 3 bytes, like the shy face or the sun.
The actual utf8mb4 is not working.
It has been tested on both Android and Ios. With same results.
VERSIONS:
Mysql: 5.5.49
CodeIgniter: 3.0.0
THE STEPS:
I have modified database character set and collation properties.
ALTER DATABASE my_database CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci
I have modified table character set and collation properties.
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci
I have set each field of the table, where possible, as Encoding: UTF-8(ut8mb4) and Collation: utf8mb4_unicode_ci
I have modified the database connection in the CodeIgniter app.
I have run the following: SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci
Lastly I have also tried this:
REPAIR TABLE table_name;
OPTIMIZE TABLE table_name;
Everything should have been setup properly but yet it doesn't work.
DATABASE SETTINGS:
This is the outcome running the following command:
`SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';`
TABLE SETTINGS:
A screeshot of the table structure:
DATABASE CONNECTION:
These are the database connection settings inside database.php (note this is not the only database, there are also others that connect using utf8)
$db['my_database'] = array(
'dsn' => '',
'hostname' => PROJECT_DATABASE_HOSTNAME,
'username' => PROJECT_DATABASE_USERNAME,
'password' => PROJECT_DATABASE_PASSWORD,
'database' => PROJECT_DATABASE_NAME,
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8mb4',
'dbcollat' => 'utf8mb4_unicode_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
MY.CNF SETTINGS:
This is the whole content of the file my.cnf:
[mysqld]
default-storage-engine=MyISAM
innodb_file_per_table=1
max_allowed_packet=268435456
open_files_limit=10000
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
THE QUESTION:
Do you know why is not working? Am I missing something?
HYPHOTESIS 1:
I am not sure, but the cause of the problem may be this:
As you can see in my.cnf character-set-server is clearly set as utf8mb4:
But after running the query in the database:
SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';
The outcome is that character-set-server = latin1
Do you know why is that? Why is not actually updating?
HYPHOTESIS 2:
The application use several different databases.
This one is set to utf8mb4 but all the others are set to utf8. It may be a problem even if they are separated databases?
Thank you!
EDIT:
This is the outcome of SHOW CREATE TABLE app_messages;
CREATE TABLE `app_messages` (
`message_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`project_id` bigint(20) NOT NULL,
`sender_id` bigint(20) NOT NULL,
`receiver_id` bigint(20) NOT NULL,
`message` text COLLATE utf8mb4_unicode_ci,
`timestamp` bigint(20) DEFAULT NULL,
`is_read` enum('x','') COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`message_id`)
) ENGINE=InnoDB AUTO_INCREMENT=496 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
EDIT 2:
I have run the following command:
INSERT INTO app_messages (message_id, project_id, sender_id, receiver_id, message, timestamp, is_read)
VALUES ('496','322','77','188', '😜' ,'1473413606','x');
And other two similar with πŸ˜‚ and πŸ‘»
They were inserted in the table without problems:
But in the actual app what i really see is: ? (this time only one ? and not 4)
Okay I finally managed to make it working!
Thanks to everybody that tried to help me, especially #Rick James and #Gerard Roche.
SUGGESTION:
If you need to work with emoji first of all make simple tests on localhost. Create a new database and make a fresh app for testing purpose.
If you follow the steps I wrote in the question or if you follow this tutorial: https://mathiasbynens.be/notes/mysql-utf8mb4#utf8-to-utf8mb4 it must work.
Working locally on a fresh basic app you will have more control and more room to make all the test you need.
SOLUTION:
In my case the problem was in the configuration of the database in CodeIgniter. It was not properly setting up the char_set and the collation for a stupid overlooking: I was overriding the database settings in the function that save messages to be sure it was working with the mobile database.
BEFORE:
function message_save ( $data = FALSE )
{
$project_db_config = array();
$project_db_config['hostname'] = 'MY_HOST';
$project_db_config['username'] = 'MY_USERNAME';
$project_db_config['password'] = 'MY_PASSWORD';
$project_db_config['database'] = 'MY_DATABASE';
$mobile_db = $this->load->database( $project_db_config, TRUE );
// other code to save message
}
AFTER:
function message_save ( $data = FALSE )
{
$mobile_db_connection = $this->load->database('admin_mobile_mh', TRUE);
// other code to save message
}
CONCLUSION:
The app must set the connection to the database properly.
If you have the database properly setup but you don't make the proper connection with your app, it won't work.
So if you encounter similar problems make sure the api properly setup the char_set as utf8mb4 and db_collat as utf8mb4_unicode_ci.
The only way I know of to get ???? for an Emoji is to not have the column declared utf8mb4. I understand that you have apparently determined that the column is declared that way, but please run SHOW CREATE TABLE table_name; to further confirm it.
The system default, the database default, and the table default are irrelevant if the column overrides the CHARACTER SET.
A note to all the other attempted answers: The COLLATION is irrelevant, only the CHARACTER SET is relevant for this question.
my.cnf is loaded first, then conf.d/*.cnf.
Instead of modifying my.cnf *(which may be overridden by configurations in conf.d/*.cnf), create a custom override configuration e.g. conf.d/90-my.cnf.
Prefixing 90 ensures the custom settings are loaded last which means they overwrite any earlier set settings.
To ensure the new configuration is reloaded, see Reload Without Restarting the MySQL service.
Example Configuration Structure (Linux)
.
β”œβ”€β”€ conf.d
β”‚Β Β  β”œβ”€β”€ 90-my.cnf
β”‚Β Β  β”œβ”€β”€ conn.cnf
β”‚Β Β  β”œβ”€β”€ my5.6.cnf
β”‚Β Β  └── mysqld_safe_syslog.cnf
β”œβ”€β”€ debian.cnf
β”œβ”€β”€ debian-start
└── my.cnf
conf.d/90-my.cnf
# https://mathiasbynens.be/notes/mysql-utf8mb4
# http://stackoverflow.com/q/3513773/934739
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
# The server character set and collation are used as default values if the
# database character set and collation are not specified in CREATE DATABASE
# statements. They have no other purpose.
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
Instead of the varchar you can change the Table filed value as follows to utf8mb4
Make sure all your tables' default character sets and text fields are converted to utf8mb4, in addition to setting the client & server character sets, e.g. ALTER TABLE mytable charset=utf8mb4, MODIFY COLUMN textfield1 VARCHAR(255) CHARACTER SET utf8mb4,MODIFY COLUMN textfield2 VARCHAR(255) CHARACTER SET utf8mb4; and so on.
hi i have used EMOJI in android and i stored it to orm database using EMOJI_INDEX.I saved in db in normal message in string form but when i get that time i check if there is any emoji then convert it into there processemoji.
textMessage.setText(getItem(pos).file != null ? "":EmojiUtil.getInstance(context).processEmoji(getItem(pos).message, textMessage.getTextSize()));
Take a look from here how i changed Emoji_Index to process
if (emojiImages == null || emojiImages.isRecycled()) {
InputStream localInputStream;
try {
localInputStream = context.getAssets().open("emoji/emoji_2x.png");
Options opts = new Options();
opts.inPurgeable = true;
opts.inInputShareable = true;
emojiImages = BitmapFactory.decodeStream(localInputStream, null, opts);
} catch (IOException e) {
return Html.fromHtml(paramString);
}
}
For more information take a look from here.
Thanks hope this will help you.
I had a problem with the server version, on linux. I had to change the file database_interface.lib.php manually and around this
if (!PMA_DRIZZLE) {
if (! empty($GLOBALS['collation_connection'])) {
change it so that, is becomes this: ( note the utf8mb4_unicode_ci references )
// Skip charsets for Drizzle
if (!PMA_DRIZZLE) {
if (! empty($GLOBALS['collation_connection'])) {
PMA_DBI_query("SET CHARACTER SET 'utf8mb4';", $link, PMA_DBI_QUERY_STORE);
$set_collation_con_query = "SET collation_connection = '"
. PMA_Util::sqlAddSlashes($GLOBALS['collation_connection']) . "';";
PMA_DBI_query(
$set_collation_con_query,
$link,
PMA_DBI_QUERY_STORE
);
} else {
PMA_DBI_query(
"SET NAMES 'utf8mb4' COLLATE 'utf8mb4_unicode_ci';",
$link,
PMA_DBI_QUERY_STORE
);
}
}
Updated answer
You can try charset utf8 collation utf8_unicode_ci instead of utf8mb4_unicode_ci.
run this query
ALTER TABLE table_name CHANGE `column_name` `column_name` TEXT CHARSET utf8 COLLATE utf8_unicode_ci;
old answer
You should use collation utf8mb4_bin instead of utf8mb4_unicode_ci.
run this query
ALTER TABLE table_name CHANGE `column_name` `column_name` TEXT CHARSET utf8mb4 COLLATE utf8mb4_bin;
Emojis will be stored as code and converted into emojis again in Android and iOS apps. I have used this code in my projects as well.

#1064 Error Message When trying to export local host database online

Hi am an aspiring Computer Scientist. I've been trying to upload my localhost site online for almost a day now. This is what I've done so far:
I created a DB online (from my online hosting acct.), created users and password. I went to my PHPmyAdmin on local host, selected my DB, exported it (a .sql file), edited the URL (find and replace). I went online, to my DB (online) and chose to import DB. I Selected my exported DB file (that of localhost) and clicked Go. I get the following error message:
It tells me:
1064 - 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 ' CREATE TABLE [IF NOT EXISTS] wp_commentmeta (meta_id
bigint(20) u' at line 1
CREATE TABLE IF NOT EXISTS `wp_commentmeta` (
`meta_id` BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT ,
`comment_id` BIGINT( 20 ) UNSIGNED NOT NULL DEFAULT '0',
`meta_key` VARCHAR( 255 ) COLLATE utf8mb4_unicode_ci DEFAULT NULL ,
`meta_value` LONGTEXT COLLATE utf8mb4_unicode_ci,
PRIMARY KEY ( `meta_id` ) ,
KEY `comment_id` ( `comment_id` ) ,
KEY `meta_key` ( `meta_key` ( 191 ) )
) ENGINE = INNODB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci AUTO_INCREMENT =6;
Hope this improves my question?
I really appreciate the work every contributor on this platform has done prior to now. With you guys here at Stack Overflow, I know the nature of my problem, but I've not found a solution that directly applies to it. I can't figure out where the code is not right. Please I need a solution fast, even as people have to register on the site (a WP site) since yesterday.
Thanks to all.
The only way that you would (still) be receiving that error is because you are very most likely using your code via a PHP script, rather than executing it/inserting those codes inside phpmyadmin.
Since you did tag as PHP, that is the only conclusion I can come up with, since using your code in phpmyadmin, did create it successfully.
Minus the brackets of course, as I stated in comments.
Remove the brackets in here [ IF NOT EXISTS ].
You must've have taken the MySQL example literally from their site and used them.
http://dev.mysql.com/doc/refman/5.7/en/create-table.html
Those aren't meant to be used, but as an option.
Plus, if executing from a PHP script, it could be a quotes issue and using the wrong ones.
If you are attempting to, or wanting to create the table through a PHP script, an example of this would be:
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
if ($conn->query($sql) === TRUE) {
echo "Table MyGuests created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
However, phpmyadmin does have an "IMPORT" option.
Open phpMyAdmin by clicking the phpMyAdmin icon
Select the database in the left menu that you will be working with
Click Import in the top menu
Under File to Import, click Browse and select the backup file you previously created (it will be either a .sql .zip or .tar.gz file).
Click Go at the bottom right
When the database has been imported successfully, you should see a message at the top of the page similar to: Import has been successfully finished, ## queries executed.
Pulled from http://www.inmotionhosting.com/support/website/phpmyadmin/import-database-using-phpmyadmin
Also consult:
http://www.techrepublic.com/blog/smb-technologist/import-and-export-databases-using-phpmyadmin/
which talks about WordPress.
and
https://codex.wordpress.org/Restoring_Your_Database_From_Backup
Another possible reason could be that there may be some type of unicode (hidden) character that was introduced, and/or a file encoding issue.
TBH; at this point in time, I am out of ideas.
Edit:
As an option, you can use the following to be run as a PHP script if it's still giving you problems:
Sidenote: This was pre-tested. Replace the xxx with your own login credentials.
<?php
$DB_HOST = 'xxx';
$DB_USER = 'xxx';
$DB_PASS = 'xxx';
$DB_NAME = 'xxx';
$Link = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($Link->connect_errno > 0) {
die('Connection failed [' . $Link->connect_error . ']');
}
$sql = "CREATE TABLE `wp_commentmeta` (
`meta_id` BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT ,
`comment_id` BIGINT( 20 ) UNSIGNED NOT NULL DEFAULT '0',
`meta_key` VARCHAR( 255 ) COLLATE utf8mb4_unicode_ci DEFAULT NULL ,
`meta_value` LONGTEXT COLLATE utf8mb4_unicode_ci,
PRIMARY KEY ( `meta_id` ) ,
KEY `comment_id` ( `comment_id` ) ,
KEY `meta_key` ( `meta_key` ( 191 ) )
) ENGINE = INNODB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci AUTO_INCREMENT = 6
";
if ($Link->query($sql) === TRUE) {
echo "Table created successfully";
} else {
echo "Error creating table: " . $Link->error;
}
Important: Copy this exact code and paste it into the "SQL" option in phpmyadmin and not "IMPORT".
CREATE TABLE IF NOT EXISTS `wp_commentmeta` (
`meta_id` BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT ,
`comment_id` BIGINT( 20 ) UNSIGNED NOT NULL DEFAULT '0',
`meta_key` VARCHAR( 255 ) COLLATE utf8mb4_unicode_ci DEFAULT NULL ,
`meta_value` LONGTEXT COLLATE utf8mb4_unicode_ci,
PRIMARY KEY ( `meta_id` ) ,
KEY `comment_id` ( `comment_id` ) ,
KEY `meta_key` ( `meta_key` ( 191 ) )
) ENGINE = INNODB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci AUTO_INCREMENT =6;
Then click on "GO". That is how I originally tested your code in phpmyadmin.
Are you sure that yr MySQL version is β€˜4.1.14’? It's really old.
If it is, the problem is mySQL version.
utf8mb4 encoding is introduced in MySQL 5.5.3 (released in early 2010).
Also Please note that official wordpress docs say:
The utf8mb4 Upgrade
In WordPress 4.2, we’re upgrading tables to utf8mb4, when we can. Your site will only upgrade when the following conditions are met:
You’re currently using the utf8 character set.
Your MySQL server is version 5.5.3 or higher (including all 10.x versions of MariaDB).
Your MySQL client libraries are version 5.5.3 or higher. If you’re using mysqlnd, 5.0.9 or higher.
Also, in the doc there is en explicit reference to wp_commentmeta table.
In our query:
utf8mb4 charset
utf8mb4_unicode_ci collation
wp_commentmeta table
mySQL 4.1.14
Maybe upgranding mySQL can resolve your problems.
I had the same issue when uploading database to server.
Error
SQL query:
#
# Table structure of table `wp_commentmeta`
#
CREATE TABLE `wp_commentmeta` (
`meta_id` BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT ,
`comment_id` BIGINT( 20 ) UNSIGNED NOT NULL DEFAULT '0',
`meta_key` VARCHAR( 255 ) COLLATE utf8_unicode_520_ci DEFAULT NULL ,
`meta_value` LONGTEXT COLLATE utf8_unicode_520_ci,
PRIMARY KEY ( `meta_id` ) ,
KEY `comment_id` ( `comment_id` ) ,
KEY `meta_key` ( `meta_key` ( 191 ) )
) ENGINE = INNODB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_520_ci;
I Just replace all utf8_unicode_520_ci with utf8_unicode_ci in database. Then I'm able to upload the database. This works for me.
You can try replace all utf8mb4_unicode_ci with utf8_unicode_ci and utf8mb4 with utf8 in database. Hope this will works for u.
When I contacted my host, they helped me resolve the issue. I got this message:
I could see that, in the file 'dbxxx.sql', the command 'Use dbxx;' was missing, this is the reason, you were receiving that error when trying to import the SQL file to the database.
My site is now up and running.

How to backup SQL data base with PHP

How can I make a SQL file with PHP that contains SQL script to create exact copy of my entire database. Like I can do it in PhpMyAdmin.
For example:
I have a database with few tabel and I would like PHP script to to return SQL script like:
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- Database: `first_db`
--
-- --------------------------------------------------------
--
-- Structure of table `my_table`
--
CREATE TABLE IF NOT EXISTS `my_table` (
`id` int(11) NOT NULL,
`naslov` text COLLATE utf8_slovenian_ci NOT NULL,
`besedilo` text COLLATE utf8_slovenian_ci NOT NULL,
`datum` text COLLATE utf8_slovenian_ci NOT NULL,
`avtor` text COLLATE utf8_slovenian_ci NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_slovenian_ci;
--
-- Fill data for `my_table`
--
INSERT INTO `my_table` (`id`, `naslov`, `besedilo`, `datum`, `avtor`) VALUES
just use exec() in php with the following shell command
exec('mysqldump –u [user name] –p[password] [database name] > [dump file]');
dont forget that exec can be disabled in php.ini if it doesn't work

php mysql insert

I am pretty new in php programming and i'm facing with one trouble here. Probably it will be simple for all of you, but ok..
When i try to insert one row into mysql table named "novica" first time it works ok, but after that i'm unable to add any new row's. But when i delete this row, i can add one...but again, only one. I don't know what cause this.
here is my little php:
<?php
$con = mysql_connect("localhost","root","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("sola", $con);
mysql_query("INSERT INTO novica (naslov, vsebina, avtor, ustvarjeno) VALUES('$_POST[address]', '$_POST[content]', 'Klemen', NOW())");
mysql_close($con);
?>
And here is mysql export sql code:
-- phpMyAdmin SQL Dump
-- version 3.4.10.1deb1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Nov 22, 2012 at 06:41 PM
-- Server version: 5.5.28
-- PHP Version: 5.3.10-1ubuntu3.4
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `sola`
--
-- --------------------------------------------------------
--
-- Table structure for table `novica`
--
CREATE TABLE IF NOT EXISTS `novica` (
`id_novica` int(10) NOT NULL,
`naslov` text COLLATE utf8mb4_bin NOT NULL,
`vsebina` text COLLATE utf8mb4_bin NOT NULL,
`avtor` text COLLATE utf8mb4_bin NOT NULL,
`ustvarjeno` date NOT NULL,
`posodobljeno` date DEFAULT NULL,
PRIMARY KEY (`id_novica`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
--
-- Dumping data for table `novica`
--
INSERT INTO `novica` (`id_novica`, `naslov`, `vsebina`, `avtor`, `ustvarjeno`, `posodobljeno`) VALUES
(0, 'a', 'a', 'Klemen', '2012-11-22', NULL);
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;
Your primary key is not set to auto increment. As such when you insert a new row, the id of zero is possibly assigned, but any subsequent insert fails as that id is already taken.
Either add the id into the insert statement or make the id_novica auto increment - http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
CREATE TABLE IF NOT EXISTS `novica` (
`id_novica` int(10) NOT NULL AUTO_INCREMENT,
`naslov` text COLLATE utf8mb4_bin NOT NULL,
`vsebina` text COLLATE utf8mb4_bin NOT NULL,
`avtor` text COLLATE utf8mb4_bin NOT NULL,
`ustvarjeno` date NOT NULL,
`posodobljeno` date DEFAULT NULL,
PRIMARY KEY (`id_novica`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
It always god to check error (mysql_error()) to find out what the problem. If that is problematic, display your query, execute by hand in console and check what error you get.
In general your primary key id_novica have to be unique, hence failure of subsequent inserts. You should either change values or define your primari key as AUTO_INCREMENT, to tell mysql to take care of dealing with this. So instead of
`id_novica` int(10) NOT NULL,
you shall have
`id_novica` int(10) NOT NULL AUTO_INCREMENT,
and in your INSERTS do not provide id_novica at all.
You also should not be using mysql_ exitension - it is deprecated. Switch to mysqli_ but if you are starting, jump directly to PDO instead
add unique id to your insert or update your id column to be automatically incremented:
ALTER TABLE `novica` CHANGE `id_novica` `id_novica` INT( 10 ) NOT NULL AUTO_INCREMENT

Categories