MySQL/PHP "data too long for column" - php

I'm getting the "data too long for column" error when trying to insert a single row in a MySQL table. I know the db is set up right because I can insert the same data other ways with no problem. Here are the essential parts of the code. If you want to see any of the functions' full code instead of pseudocode, I'll be happy to post - just trying to avoid clutter.
Output is as follows:
Connected to MySQL
Connected to database dublin
Data too long for column 'gpsMin' at row 1
testindex.php
include 'parseData.php';
parseAll("dublin");
parseData.php
function parseAll($sitename){
function pullAndDelete($filename){} #reads data from file, deletes file
$data = pullAndDelete($sitename);
function parse($str){} #extracts first piece of data from data string
function chop2($str){} #removes 2 chars from front of string
function chop14($str){} #removes 14 chars from front of string
#$data is put through parse and chop functions several times -
#take first piece of data, chop it off, take new first piece, chop again, etc.
#Function calls up through relevant one:
$radio=parse($data);
$short1=chop14($data);
$antenna=parse($short1);
$short2=chop14($short1);
$gpsmaj=parse($short2);
$short3=chop14($short2);
$gpsmin=parse($short3);
At this point, echo'ed called to strlen() function produces this output:
Radio: 0 Strlen: 1
Antenna: 0 Strlen: 1
GPS Major: 0 Strlen: 1
GPS Minor: 0 Strlen: 1
function writeToDB($site, $a, $b, $c, $d, $e, $f, $g, $h, $b1, $b2, $temp, $d, $t)
{
mysql_connect(xxx,xxx,xxx) or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("scada") or die(mysql_error());
echo "Connected to table ".$site."<br />";
mysql_query("INSERT INTO ".$site." (radio, antenna, gpsMaj, gpsMin, acpwr,
generator, blank1, blank2, battery1, battery2, temperature, date, time)
VALUES($a, $b, $c, $d, $e, $f, $g, $h, $b1, $b2, $temp, $d, $t)")
or die(mysql_error());
echo "Data inserted into table ".$site;
}
writeToDB($sitename, $radio, $antenna, $gpsmaj, $gpsmin, $acpwr, $generator,
$blank1, $blank2, $batt1, $batt2, $temperature, $mysqldate, $mysqltime);
} #end parseAll()
So the db is set properly, and the strlen() function tells me the data is the right length. I don't see anything getting added to it in any way, and the first three (radio, antenna, gpsmaj) go in just fine. I've got another version of this function in a file called dbconnector.php, which works fine. I can't find a difference between the two.
Thanks in advance!
Edits:
The column datatype is binary.
The var value is 0.
SHOW CREATE TABLE dublin;
CREATE TABLE dublin (
idsiteData int(11) NOT NULL AUTO_INCREMENT,
radio binary(1) DEFAULT NULL,
antenna binary(1) DEFAULT NULL,
gpsMaj binary(1) DEFAULT NULL,
gpsMin binary(1) DEFAULT NULL,
acpwr binary(1) DEFAULT NULL,
`gener...
Vardump on 'gpsmin' shows this:
string(1) "0"
Not sure I did the second vardump right, but here's the last line, which followed a bunch of "undefined variable" errors:
string(157) "INSERT INTO (radio, antenna, gpsMaj, gpsMin, acpwr, generator, blank1, blank2, battery1, battery2, temperature, date, time) VALUES(, , , , , , , , , , , , )"

Looking at the SQL query generated, I think the variables you are trying to insert as binaries are actually null or something.
What may work is by int-casting all your inputs as such:
$a = (int)$a;
$b = (int)$b;
$c = (int)$c;
// ... do the rest for all of your binary variables
(Even if it doesn't fix your problem here, it is always good practice to int-cast things which you really need a zero instead of a null. PHP is sometimes sloppy when it comes to distinguishing between zeros and nulls )

I figured it out! I stuck two variables in there both named $d. That's what I get for being overly efficient. Thanks all!

Related

Inserting data from several arrays into respective fields in mysql database

My code did work when I was using mysql, now upgrading to mysqli has introduced problems. I have several interger arrays of various length and I am using a for loop of 48 steps to insert the data into the fields of my database. What is happenning is my array that has only 4 elements is causing the whole database insert to stop after the 4 th loop. The process stops with a rejected URL request, which is my page presenting the insert commands.
My code:
$setcomm = array (1201.30,16.30,3.22,7.98,0.95, 78.78, 54.12,1.45,3.09,62.44,36.95,269.64,
239.59,6.15,4.57,.43,100.00,.8622,14640, 1.21, 1.55, 1.19, 1.16, 1, 0, 0, 0, 0, 0, 0, 0.2125 );//dec30 2014
$qk = array (1,1,1,1);
$xmports = array ( 777, 32197, 2534, 2124, 4658, 8943, 9214, 7360, 24020, 14689, 566, 2310, 00.00 ,3780, 10812, 2448, 262, 11557, 11171, 5711, 12740, 26850, 26551, 1818, 2615, 00.00, 00.00);
for($k=0;$k<48;$k++)
{
$sqli = ( " INSERT INTO $mr (comm, qrt, trade, gross, interest, coffer) VALUES ( $setcomm[$k], $qk[$k], $xmports[$k], $GDP[$k], $bratz[$k], $fiscal[$k] ) ");
$stone=mysqli_query($connect2, $sqli);
}
I have check your code. It looks fine. You must have to check one thing that is:-
You are using three arrays as you mention in you question & array $qk having 4 elements. So can you make 'qrt' column default value
NULL in your database under table structure. According to me after set
default Null value for 'qrt' column it will working fine.
Set Default value like this in your database table. Please note it is just an example so if you know which fields always get a value for that there is no need to set Default Null Value.

PHP SQLite - prepared statement misbehaves?

I have the following SQLite table
CREATE TABLE keywords
(
id INTEGER PRIMARY KEY,
lang INTEGER NOT NULL,
kwd TEXT NOT NULL,
count INTEGER NOT NULL DEFAULT 0,
locs TEXT NOT NULL DEFAULT '{}'
);
CREATE UNIQUE INDEX kwd ON keywords(lang,kwd);
Working in PHP I typically need to insert keywords in this table, or update the row count if the keyword already exists. Take an example
$langs = array(0,1,2,3,4,5);
$kwds = array('noel,canard,foie gras','','','','','');
I now these data run through the following code
$len = count($langs);
$klen = count($kwds);
$klen = ($klen < $len)?$klen:$len;
$sqlite = new SQLite3('/path/to/keywords.sqlite');
$iStmt = $sqlite->prepare("INSERT OR IGNORE INTO keywords (lang,kwd)
VALUES(:lang,:kwd)");
$sStmt = $sqlite->prepare("SELECT rowid FROM keywords WHERE lang = :lang
AND kwd = :kwd");
if (!$iStmt || !$sStmt) return;
for($i=0;$i < $klen;$i++)
{
$keywords = $kwds[$i];
if (0 === strlen($keywords)) continue;
$lang = intval($langs[$i]);
$keywords = explode(',',$keywords);
for($j=0;$j < count($keywords);$j++)
{
$keyword = $keywords[$j];
if (0 === strlen($keyword)) continue;
$iStmt->bindValue(':kwd',$keyword,SQLITE3_TEXT);
$iStmt->bindValue(':lang',$lang,SQLITE3_INTEGER);
$sStmt->bindValue(':lang',$lang,SQLITE3_INTEGER);
$sStmt->bindValue(':kwd',$keyword,SQLITE3_TEXT);
trigger_error($keyword);
$iStmt->execute();
$sqlite->exec("UPDATE keywords SET count = count + 1 WHERE lang =
'{$lang}' AND kwd = '{$keyword}';");
$rslt = $sStmt->execute();
trigger_error($sqlite->lastErrorMsg());
trigger_error(json_encode($rslt->fetchArray()));
}
}
which generates the following trigger_error output
Keyword: noel
Last error: not an error
SELECT Result: {"0":1,"id":1}
Keyword: canard
Last Error: not an error
SELECT Reult:false
Keyword:foiegras
Last Error: not an error
SELECT Result: false
From the SQLite command line I see that the three row entries are present and correct in the table with the id/rowid columns set to 1, 2 and 3 respectively. lastErrorMsg does not report an error and yet two of the three $rslt->fetchArray() statements are returning false as opposed to an array with rowid/id attributes. So what am I doing wrong here?
I investigated this a bit more and found the underlying case. In my original code the result from the first SQLite3::execute - $iStmt-execute() - was not being assigned to anything. I did not see any particular reason for fetching and interpreting that result. When I changed that line of code to read $rslt = $iStmt->execute() I got the expected result - the rowid/id of the three rows that get inserted was correctly reported.
It is as though internally the PHP SQLite3 extension buffers the result from SQLiteStatement::execute function calls. When I was skipping the assignment my next effort at running such a statement, $sStmt->execute() was in effect fetching the previous result. This is my interpretation without knowing the inner workings of the PHP SQLite3 extension. Perhaps someone who understands the extension better would like to comment.
Add $rslt = NONE; right after trigger_error(json_encode($rslt->fetchArray())); and the correct results appear.
FetchArray can only be called once and somehow php is not detecting that the variable has changed. I also played with changing bindValue to bindParam and moving that before the loop but that is unrelated to the main issue.
It is my opinion that my solution should not work unless there is a bug in php. I am too new at the language to feel confident in that opinion and would like help verifying it. Okay, not a bug, but a violation of the least surprise principle. The object still exists in memory so without finalizing it or resetting the variable, fetch array isn't triggering.

MSSQL + PHP - Error to insert oriental char

I'm having a problem to insert a oriental character with bind variables in SQL Server.
i'm using MSSQL commands and PHP.
My PHP code is like this:
$sql = "
CREATE TABLE table_test
( id int
,nvarchar_latin nvarchar(255) collate sql_latin1_general_cp1_ci_as
);";
$stmt = mssql_query($sql);
$conn = mssql_connect("server","user","pass");
mssql_select_db('test')
$stmt = mssql_init('test..sp_chinese', $conn);
$id = 1;
$nvarchar_latin = '重建議';
mssql_bind($stmt, '#id' , $id , SQLINT1);
mssql_bind($stmt, #nvarchar_latin, $nvarchar_latin, SQLVARCHAR);
mssql_execute($stmt);
My procedure is like this:
ALTER PROCEDURE sp_chinese
#id int
,#nvarchar_latin nvarchar (255)
AS
BEGIN
INSERT INTO char_chines (id, nvarchar_latin)
VALUES (#id, #nvarchar_latin);
END
this work if I change the oriental characters for normal one.
if I run directly this insert, it work's fine:
INSERT INTO table_test (id, nvarchar_latin)
VALUES (1, '重建議');
So, cleary the problem is when I send the variable from PHP to SQL Server.
Anyone have a clue how to make this works? some casting or something?
Thanks!
A solution that uses just the PHP (or even JavaScript) is to convert the character to its HEX value and store that. I don't know if you want to go this route but and I don't have time to show you the code but here is the full theory:
A non-English character is detected, like so: 重
Convert to HEX value (Look here for starters. But a search for Javascript will help you find better ways to do this even in PHP): 14af
NOTE: That is not what 重 really is in HEX
Store in a way that you can convert back to its original value. For example how can you tell what this is: 0d3114af is it 0d - 31 - 14 - af OR is it 0d31 - 14af. You can use deliminators like | or a . but one way is to provide padding of 00 in front. An English character would be only 2 characters long like 31 or af non-English will be 4 like 14af. Knowing this you can just split every 4 characters and convert to their values.
Downside is you will need to change your Database to accommodate these changes.
[ UPDATE ] -----
Here is some JavaScript code to send you off in the right direction. This is completely possible to replicate in PHP. This does not search for characters though, its part of an encryption program so all it cares about is turning everything into HEX. English characters will be padded with 00 (This is my own code hence no link to source):
function toHex(data) {
var result = '';
// Loop through entire string of data character by character
for(var i=0;i<data.length;i++) {
// Convert UTF-16 Character to HEX, if it is a 2 chracter HEX add 00 padding in front
result += (data.charCodeAt(i) + 0x10000).toString(16).slice(1);
}
// Display the result for testing purposes
document.getElementById('two').value = result;
}
function fromHex(data) {
var result = '', block = '', pattern = /(00)/; // Pattern is the padding
for(var i=0;i<data.length;i = i+4) {
// Split into separate HEX blocks
block = data.substring(i,i+4);
// Remove 00 from a HEX block that was only 2 characters long
if(pattern.test(block)){
block = block.substring(2,4);
}
// HEX to UTF-16 Character
result += String.fromCharCode(parseInt(block,16));
}
// Display the result for testing purposes
document.getElementById('two').value = result;
}

PHP the same strings but yet not the same

I use PHPExcel package in order to import data from .xls files to my database. From time to time the file is updated so I have to import it again. Before the import itself, I check if database already contains any of data included in .xls file.
In most cases, it works - data already included in DB is omitted, but still there are some values that are duplicated.
Here's what I do:
1. Query to DB: SELECT * FROM Table1, fetching the result to an array.
2. PHPExcel usage, get row value to a variable, check if the value already exists in the array
3. If yes - skip, if no - add to DB.
But there's a value "The name (xxx yyy zzz)" that IS included in the array (I used if ($array[0] == "The name (xxx yyy zzz)") and it returned true), but still PHP function array_search couldn't find it.
I used trim and still nothing. var_dump of variable, the actual value and the index value was the same. similar_text($array[0], "The name (xxx yyy zzz)") returned 2.
Please help, I ran out of ideas.
You would be better off using MySQL to do this:
ALTER IGNORE TABLE your-table ADD UNIQUE INDEX idx ( uniqueField, orFields );
This will remove all duplicated where the field or fields are the same. Drop the index once you are done:
ALTER TABLE your-table DROP INDEX idx
2 known issues:
1
I've once had a problem that is a bit similar. The problem ended up being that the imported data was read with an incorrect encoding. It lead to the following:
echo $str1; // output: foo
echo $str2; // output: foo
echo $str1 == $str2 ? "The same" : "Not the same"; // output: Not the same
I was only to understand it after I did the following:
for($i = 0; $i < strlen($str1); $i++) echo ord($str1[$i])." "; // 102 111 111
for($i = 0; $i < strlen($str2); $i++) echo ord($str2[$i])." "; // 102 0 111 0 111 0
I am guessing that you are running into something similar.
2
Another possibility is as suggested in the comments, that you have HTML in your string. To get that out in the open, do the following:
echo htmlspecialchars($str1); // foo
echo htmlspecialchars($str2); // <span>foo</span>
And/or view the source of your HTML page in the browser.

PHP/MYSQL - Insert multiple rows into one column, works via phpmyadmin but only partially via myqli prepared statement?

Using a mysqli prepared statement I would like to insert an array into a mysql database table.
Being aware that bind-param and arrays do not go together, we would like to write the query in php first, then process this as a prepared statement:
$tagQuery = "INSERT INTO word_tags(speaks) VALUES ";
// Count total array values
$icoderayInsideCount = count($icoderayInside);
foreach ($icoderayInside as $icoderayInsideKey=>$icoderayInsideValue)
{
// Last value
// Currrent Array Key Total / Last Array Value
if ($icoderayInsideKey == $icoderayInsideCount)
{
$tagQuery .= "('$icoderayInsideValue')";
}
// All other values
else
{
$tagQuery .= "('$icoderayInsideValue'), ";
}
}
// Send array (keywords) to database
if ($stmt2 = $link2->prepare($tagQuery))
{
if (!$stmt2->execute())
{
// #2 If it can prepare but can't execute, why?
echo "Error {$link2->errno} : {$link2->error} (Cant execute?) <br/>";
// Dump query to check the end result
var_dump($tagQuery);
exit();
}
$stmt2->close();
}
else
{
// #1 If it cant prepare, why?
echo "Error {$link2->errno} : {$link2->error} (Cant prepare?)";
exit();
}
When i run this via PHP / Server i get:
Error 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 '('DONGLES'),' at line 1 (Cant prepare?)
So, the statement prepares but is not being executed.
The value of the generated query, $tagQuery is:
INSERT INTO word_tags(speaks) VALUES ('PMP3670B_BK'), ('PRESTIGIO'), ('MULTIPAD'), ('7"'), ('800X480'), ('1GHZ'), ('ARM'), ('CORTEX'), ('A8'), ('CPU'), ('512MB'), ('DDR3'), ('DRAM'), ('ANDROID'), ('4.1'), ('JELLY'), ('BEAN'), ('MALI'), ('400'), ('MP'), ('GPU'), ('VIDEO'), ('4GB'), ('INTERNAL'), ('FLASH'), ('MEMORY'), ('SUPPORT'), ('32GB'), ('SDHC/SD'), ('USB/WI-FI/HEADSET'), ('PORT'), ('LITHIUM'), ('POLYMER'), ('BLACK'), ('HDMI'), ('OUTPUT'), ('UPTO'), ('1080'), ('HD'), ('USB2.0'), ('MINI'), ('HIGH'), ('SPEED'), ('FOR'), ('3G'), ('DONGLE'), ('OTG'), ('CABLE'), ('INCLUDED')
The fourth value from the end is('DONGLE') which is what the error message is complaining about.
When i run this exact same query through phpmyadmin there is no error involved.
What i assume is happening, is that there is some kind of length limit involved within creating a prepared statement... Or something to this effect.
Have scratched my brains for hours now to try to solve this and have not found any relating information.
If anyone could offer some assistance / advice / indication / input or otherwise as to what the conflict of problem may be within this, it would be GREATLY appreciated.
Thanks so much for the time and effort in readying through this!
EDIT:
#Mihai - Thanks for the thought.
It seems that the word dongle does have something string to it. In the original string, before being parsed to an array, it looks like this: DONGLE,
I run preg_replace to remove this comma from the string before parsing it to an array:
$icode = preg_replace('#,#', '', $icode);
Then into an array:
$icoderayInside = explode(" ", $icode);
Still cannot think of any reason this would cause a conflict as the output string, the query is as i have previously stated and includes no comma, or anything... Any would be greatly appreciated!
EDIT 2:
#ShadyAtef
Original input is stored in mysql as varchar, latin_general_ci:
PRESTIGIO MULTIPAD, 7" 800X480, 1GHZ ARM CORTEX A8 CPU, 512MB DDR3 DRAM, ANDROID 4.1 JELLY BEAN, MALI 400 MP GPU, VIDEO, 4GB INTERNAL FLASH MEMORY, SUPPORT 32GB SDHC/SD, USB/WI-FI/HEADSET PORT, LITHIUM POLYMER, BLACK, HDMI OUTPUT UPTO 1080 HD, USB2.0 MINI HIGH SPEED PORT FOR 3G DONGLE, OTG CABLE INCLUDED
Brought into php then processed to an array with additional requirements:
// Convert Var a + b to String
$icode = $itemCode . ' ' . $description;
// Clean of Unwanteds
$icode = preg_replace('#,#', '', $icode);
$icode = preg_replace('#\(#', '', $icode);
$icode = preg_replace('#\)#', '', $icode);
// Creates array from sting
$icoderayInside = explode(" ", $icode);
// Remove array duplicates
$icoderayInside = array_unique($icoderayInside);
Before being built into the query. Any assistance would be GREATLY appreciated!
EDIT 3:
#ShadyAtef
// Currrent Array Key Total / Last Array Value
if ($icoderayInsideKey == $icoderayInsideCount)
{
// dump here shows:
$icoderayInsideKey == 49
$icoderayInsideCount == 49
}
This was really tricky,but I got it : The tricky wrong part is that
if ($icoderayInsideKey == $icoderayInsideCount)
It should be
if ($icoderayInsideKey == ($icoderayInsideCount-1))
Because the last key in the array equals to the array (length -1) So you should change your if condition

Categories