I use MySQL and PHP to store data in an Android app. I want to use UTF-8 in my database, and tables because the data is in English and Persian. This query is to create the table:
CREATE TABLE IF NOT EXISTS `tbl_notes` (
`user_id` int(11) NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`note_subject` varchar(22) NOT NULL,
`note_date` text NOT NULL,
`note_content` varchar(21000) NOT NULL,
`note_deleted` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
I use this code before connecting to the database:
$con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());
$db = mysql_select_db(DB_DATABASE) or die(mysql_error());
mysql_query("SET CHARACTER SET 'UTF8'", $con);
mysql_query("SET NAMES utf8_general_ci", $con);
My problem is when I save the data to note_content and note_subject, the data is showing as "???????". And when I retrieve data to my android app again, the data is "????????".
How can I fix this error?
Related
I'm writing a PHP script to delete a row from a MySQL database by id.
The value of the id is passed as a query string and stored in a variable.
The query runs normally, with no errors, but no rows are affected.
Here is my code, can somebody please point out what's wrong with it?
Thanks.
PHP Code:
$delete = $_GET['killthisguy'];
$sqlDel = "DELETE FROM `pba_files` WHERE id=".$delete;
$res = mysqli_query($cxn, $sqlDel);
$affRows = mysqli_affected_rows($cxn);
Database Schema:
CREATE TABLE IF NOT EXISTS `pba_files` (
`id` int(3) NOT NULL auto_increment,
`chap_id` int(2) default NULL,
`cat_id` varchar(2) character set utf8 collate utf8_unicode_ci default NULL,
`is_video` tinyint(1) default NULL,
`file_location` varchar(220) character set utf8 collate utf8_unicode_ci default NULL,
`clean_filename` varchar(116) character set utf8 collate utf8_unicode_ci default NULL,
`description` varchar(1026) character set utf8 collate utf8_unicode_ci default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=611 ;
You could try removing the ' from the "id" because it is not a string, it is an int (from the database point of view)
$delete = $_GET['killthisguy'];
$sqlDel = "DELETE FROM `pba_files` WHERE id=".$delete;
$res = mysqli_query($cxn, $sqlDel);
$affRows = mysqli_affected_rows($cxn);
Also are you sure that the "id" exists in the table?
The issue was just a careless mistake I made when setting up my access rights. DELETE was not allowed for my user on the database.
Thanks for all your help guys.
I want Data insert into mysql using PHP. I am writing script HTML, PHP and I create Mysql database. I don't understand data not send mysql server. Here is the HTML and PHP code:
<?php
if (isset($_POST['submit'])){
$con = mysql_connect("localhost","root","");
if(!$con){
die("Can not connect: " . mysql_error());
}
//Database Connection.
mysql_select_db("learnarabic",$con);
$sql = "INSERT INTO article(ArticleDate,ArticleAuthor,ArticleSubject,ArticleSource,ArticleLevel,ArticleTitleEnglish,ArticleTitleArabic,ArticleFree,ArticleEnglish,ArticleTranslationEnglish,ArticleArabic,ArticleVowels,ArticleTransliteration,ArticleAudio)
VALUES
('$_POST['ArticleDate']',
'$_POST['ArticleAuthor']',
'$_POST['ArticleSubject']',
'$_POST['ArticleSource']',
'$_POST['ArticleLevel']',
'$_POST['ArticleTitleEnglish']',
'$_POST['ArticleTitleArabic']',
'$_POST['ArticleFree']',
'$_POST['ArticleEnglish']',
'$_POST['ArticleTranslationEnglish']',
'$_POST['ArticleVowels']',
'$_POST['ArticleTransliteration']',
'$_POST['ArticleAudio']',
'$_POST['ArticleArabic']')";//Insert data into Mysql.
mysql_query($sql,$con);
mysql_close($con);//Connection Close.
}
?>
Here is the SQL Database code:
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- Database: `learnarabic`
--
-- --------------------------------------------------------
--
-- Table structure for table `article`
--
CREATE TABLE IF NOT EXISTS `article` (
`articleid` int(250) NOT NULL AUTO_INCREMENT,
`ArticleDate` date NOT NULL,
`ArticleAuthor` varchar(250) NOT NULL,
`ArticleSubject` varchar(250) NOT NULL,
`ArticleSource` varchar(250) NOT NULL,
`ArticleLevel` varchar(250) NOT NULL,
`ArticleTitleEnglish` varchar(250) NOT NULL,
`ArticleTitleArabic` varchar(250) NOT NULL,
`ArticleFree` varchar(250) NOT NULL,
`ArticleEnglish` longtext NOT NULL,
`ArticleTranslationEnglish` longtext NOT NULL,
`ArticleArabic` longtext NOT NULL,
`ArticleVowels` longtext NOT NULL,
`ArticleTransliteration` longtext NOT NULL,
`ArticleAudio` blob NOT NULL,
PRIMARY KEY (`articleid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
I need a solution.
Try removing the $con variable from the mysql_query() function eg mysql_query($sql); also add single quotes to any text inside $_POST like $_POST['something']
I am trying to read from a database in MySQL and insert my data in another database in MySQL .
my first table is like this
CREATE TABLE IF NOT EXISTS `link` (
`_id` bigint(20) NOT NULL AUTO_INCREMENT,
`country` varchar(30) COLLATE utf8 DEFAULT NULL,
`time` varchar(20) COLLATE utf8 DEFAULT NULL,
`link` varchar(100) COLLATE utf8 DEFAULT NULL,
PRIMARY KEY (`_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=6149 ;
and the second table is
CREATE TABLE IF NOT EXISTS `country` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(15) CHARACTER SET utf8 NOT NULL,
`Logo` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `Name_3` (`Name`),
UNIQUE KEY `ID` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8457 ;
There are about 6114 rows in first table that I'm trying to insert to second using this code
<?php
$tmp = mysqli_connect(******, *****, ****, *****); // First table in here
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$main = mysqli_connect(*****, *****, ****, ******); //Second table in here
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$req = "SELECT country FROM link";
$result = mysqli_query($tmp, $req) or die( mysqli_error($tmp) );
echo "-> ".mysqli_num_rows($result)."<br>";
while ($row = mysqli_fetch_array($result)) {
$con = $row["country"];
$req = "INSERT IGNORE INTO country (Name) VALUES ('$con')";
mysqli_query($main, $req) or die( mysqli_error($main) ) ;
}
?>
problem is the php code works but for 6114 take a very long time which I can't afford .
what is causing the code to take this long to work ? is it the "INSERT IGNORE" ?
is there any way I can do this faster ?
Since the databases are on the same server, you can simply use INSERT ... SELECT (which avoids having to bring the data into PHP and loop over the results executing separate database commands for each of them, so will be considerably faster):
INSERT INTO db2.country (Name) SELECT country FROM db1.link
you can try a create an index on column "country" of table Link.
I wish to find out why Arabic text is not storing in the databse. It changes to some other unicode format such as %$&^%$%^&!#***.
I'v made my column Collation as utf8_general_ci. Do I need to do some modification in my sql query?
My SQL query is:
$query="INSERT INTO blog_tab(blog_details)
VALUES('$blog_details') ";
The character set, collation and encoding seem to be correct.
Adding the following before connecting to the DB is supposed to help:
mysql_query("SET NAMES 'utf8'");
mysql_query('SET CHARACTER SET utf8');
The page at http://www.adviesenzo.nl/examples/php_mysql_charset_fix/ contains an explanation and further guidance for your issue.
When you create the database, the SQL might be like this:
CREATE TABLE IF NOT EXISTS `employees` (
`empid` int(11) NOT NULL,
`empname` varchar(255) NOT NULL,
PRIMARY KEY (`empid`)
) ENGINE=InnoDB ;
or like this:
CREATE TABLE IF NOT EXISTS `employees` (
`empid` int(11) NOT NULL,
`empname` varchar(255) NOT NULL,
PRIMARY KEY (`empid`)
) ENGINE=InnoDB charset=latin1 ;
Try changing the charset to utf-8 as shown below:
CREATE TABLE IF NOT EXISTS `employees` (
`empid` int(11) NOT NULL,
`empname` varchar(255) NOT NULL,
PRIMARY KEY (`empid`)
) ENGINE=InnoDB charset=utf-8 ;
Hope this helps! :)
I am new to php and SQL so this is probably an easy question but I could not find any good sources online.
I am trying to create a SQL table when someone submits a form and this is what I have so far
include("dbstufflive.php");
$cxn = mysqli_connect($host,$user,$passwd,$dbname)
or die("Couldn't connect to server");
$sql = "CREATE TABLE IF NOT EXISTS `$company` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`company_name` varchar(80) NOT NULL,
`contact` varchar(50) NOT NULL,
`email` varchar(80) NOT NULL,
`phone` varchar(13) NOT NULL,
... (long list of table data)
`description` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ";
mysqli_query($cxn,$sql);
I will of course be doing other stuff with this table but I think I have most of that under control.
The problem is that this statement does not actually create my table :( The SQL statement works in phpadmin when I enter it as is and also there are no errors when the script runs. So it goes through all of this, and more, and seems to work but the table simply doesn't appear.
I can supply more code if needed but I don't want to paste more code here than is necessary.
Thanks in advance for any help from the community.
EDIT:
I was using wrong DBinfo...wow, I am not very bright.
Your SQL Statement looks fine - from the looks of it, you are missing your login credentials. An efficient way to do so:
// Add this line
require_once('config.php');
// Then change the variables below to pull your credentials from that file.
$cxn = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD)
or die("Couldn't connect to server");
$sql = "CREATE TABLE IF NOT EXISTS `$company` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`company_name` varchar(80) NOT NULL,
`contact` varchar(50) NOT NULL,
`email` varchar(80) NOT NULL,
`phone` varchar(13) NOT NULL,
... (long list of table data)
`description` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ";
mysqli_query($cxn,$sql);
Then create a new file called config.php in same directory. Put your credentials:
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'username');
define('DB_PASSWORD', 'your_password');
define('DB_DATABASE', 'database_name');
?>
You should check the result of mysqli_query (as far as i know it returns false on failure). See mysqli documentation for details, where you can read, that "Create table doesn't return a resultset" but True/false on sucess/failure.
example:
if (!mysqli->query($cxn,$sql)) {
printf("Error: %s\n", mysqli_error($cxn));
}