I've got this db called Guild Logo (its for a game) in there I've got guild master's id + mark + mark_jpg columns.
Mark is a varbinary column and Mark_jpg is a varchar column. The Mark field is usually filled with hex format numbers and letters - 0x001f52ae .... etc .
I believe I should convert the image to binary and put the output there. I am using Microsoft SQL Server.
Here is what I've tried but it doesn't seem to work, because I get a bunch of useless letters which are not HEX.
<?php
$image = file_get_contents('assets/test.png');
$imagebin =base64_encode($imagedata1);echo $imagebin;
?>
Related
I get the error message SQLite3::exec(): unrecognized token: "'Date" when I insert a command into a SQLite database.
If I have the SQL commands echoed and execute them via the console, this also works. The data for this comes from a dbase database.
If I enter a string for fieldname the insert commands will work.
$field=unpack( "a11fieldname/A1fieldtype/Voffset/Cfieldlen/Cfielddec", substr($buf,0,18));
$db = new SQLite3('databases/test.db');
$sqlCode .= "INSERT INTO HEADER (name) VALUES ('".$field['fieldname']."');";
$db-> exec($sqlCode);
Would be that much easy to use prepare satement against sql injections
If I enter a string for fieldname the insert commands will work
make sure if fieldname is a text value
I am using pdo :
$sqlCode = "INSERT INTO header VALUES (:name)";
$query = $db->prepare($sqlCode);
$query->bindValue(':name', $field['fieldname'], SQLITE3_TEXT);
$result = $query->execute();
EDIT : Error says unrecognized token Date, So, if fieldname is a date you might need to change SQLITE3_TEXT to SQLITE3_BLOB OR SQLITE3_INTEGER Usualy not needed.
But you need to insert date into date column not into name column
EDIT 2 :
it's a bit complicated.
See here for a full description of the dbf file format.
So it would be best if you could use a library to read and write the dbf files.
If you really need to do this yourself, here are the most important parts:
Dbf is a binary file format, so you have to read and write it as binary. For example the number of records is stored in a 32 bit integer, which can contain zero bytes.
You can't use string functions on that binary data. For example strlen() will scan the data up to the first null byte, which is present in that 32 bit integer, and will return the wrong value.
If you split the file (the records), you'll have to adjust the record count in the header.
When splitting the records keep in mind that each record is preceded by an extra byte, a space 0x20 if the record is not deleted, an asterisk 0x2A if the record is deleted. (for example, if you have 4 fields of 10 bytes, the length of each record will be 41) - that value is also available in the header: bytes 10-11 - 16-bit number - Number of bytes in the record. (Least significant byte first)
The file could end with the end-of-file marker 0x1A, so you'll have to check for that as well.
See asked : binary safe write on file with php to create a DBF file
Final Word : you need DBF library
Data File Header Structure
In MYSQL database, I have a varbinary(255) field.
In my PHP script, I am trying to pass the input of string (either in decimal or hex) to MYSQL to save the value in varbinary(255). I tried passing the value in decimal, hexadecimal, and binary, but none of them gave me the correct value.
$val = "0x54321"; // Used hardcoded hex value here, but it can be also decimal in my code
$v_dec = intval($val,0); // That's why I have intval() here
$v_hex = dechex(intval($val,0));
$v_bin = decbin(intval($val,0));
$v_bin_2 = (binary)intval($val,0);
I tried inserting these values using regular INSERT query, but each of them gave weird numbers.
Variable Converted Value (in php) varbinary Value (in MYSQL)
v_dec: 344865 -> 333434383635
v_hex: 54321 -> 3534333231
v_bin: 1010100001100100001 -> 31303130313030303031313030313030303031
v_bin2: 344865 -> 333434383635
What data type should the value be in? I want to do the conversion in php instead of using CAST() in the MYSQL query. Thanks!
EDIT:
What I want to do is to correctly display the saved varbinary value in hex.
EDIT:
Found the problem. The problem was how I was retrieving the value. Instead of HEX(value), I retrieved it as BIN(value) in mysql level and then converted it into hex with dechex(bindec($value)) in php.
I have some very odd situation. I am making a query to a firebird database and there is mismatch with the result in PHP. In DB the result is just fine, but when it comes to PHP there are different values.
The query:
SELECT LIST(t."ID", ',') ID,t."Date", LIST(n."Name",',') Name
FROM "Tests" t
LEFT JOIN "Names of tests" n ON t."Name ID" = n."ID"
WHERE t."Locked" = 0
GROUP BY t."Date"
ORDER BY t."Date" DESC
Result in DB:
ID = 546,552 Date = 23.10.2015 Name = Математика (тест),География(тест)
Result in PHP:
ID => 0x0000000200000000,
Date => 2015-10-23,
Name => 0x0000000500000000
I am using "UTF-8" encoding when connecting to DB with ibase_connect() the database encoding is WIN1251.
The result type of LIST() is a blob, not a CHAR or VARCHAR. I don't use PHP myself, but I believe that the Firebird/Interbase driver for PHP requires you to explicitly request the blob.
The values you see for ID and Name are the blob ids that can be used to request the blobs.
You have two options:
Request the blob value using these blob ids, see ibase_blob_open and ibase_blob_get (afaik, you will need to do the correct byte to character conversion yourself)
Cast the value to a VARCHAR (eg CAST(LIST(t."ID", ',') AS VARCHAR(2048)) AS ID)
The downside of the second option is that if you can have really long results, then you also need to cast to a long VARCHAR, otherwise you get truncation errors; and unfortunately varchars are restricted to 32K-2 bytes (8191 characters for UTF8), and a row as a whole to 64K bytes.
I'm using php and mysql and I have a problem with inserting latitude and longitude values along with the degree, min and sec symbols. that is (°,',") symbols.
I have referred several sites and tried different ways but i just can't solve it.
First i tried copying symbols and concatenating with the 3 user input values.
$latitude=$degree.'°'.$min.'''.$sec.'?'.'N';
Then after executing the insert query,the $latitude variable value is inserted in database, but the symbols are replaced by random characters as shown here.
2˚3ʼ4ˮN (data stored in database)
Then i tried using html character code instead of symbols.
$latitude=$degree.'°'.$min.'’'.$sec.'#8221'.'N';
But it doesn't work.It displays the same html characters in databse.
I don't know whether it is a problem with html entities or something.
In my opinion such values should be stored without including its ( DMS : Deg Min Sec ) formatting .
In other words , I might store latitude/longitude ( for example ) as a float value and would apply required formatting when displaying .
Just for example :
You would store a date as 2015-04-23 but might display it as 4th April, 2015 .
You might store an amount as 10.50 but might display it as $10.50 USD .
Coming to latitude/longitude considering 36°19'11.46" N as example , one of these ways might apply to your situation :
Store it as 36.31985 but use required conversion to display it as 36°19'11.46" N .
Store it as 36D19M11.46S N but use required string manipulation to display it as 36°19'11.46" N .
Store it as four parts lat_deg , lat_min , lat_sec , lat_dir but use concatenation to display it as 36°19'11.46" N .
Links that might help :
mysql-convert-degree-minutes-seconds-to-degree-decimal
latitude-and-longitude-datatype-and-storage-format
convert-dd-to-dms-in-mysql
converting-latitude-and-longitude-coordinates-between-decimal-and-degrees-minutes-seconds
whats-the-best-way-to-store-co-ordinates-longitude-latitude-from-google-maps
Try this function:
$result = mysqli_real_escape_string();
refer following link
http://php.net/manual/en/mysqli.real-escape-string.php
I want to make a excel file by fetching data from database. Every thing works fine but the phone number field return unknown value in excel sheet but it displays correct value in browser.
<?php
include('connection.php');
header( "Content-Type: application/vnd.ms-excel" );
header( "Content-disposition: attachment; filename=spreadsheet.xls" );
$q = mysql_query("select * from members order by id desc");
echo 'First Name' . "\t" . 'email' . "\t" . 'Phone' . "\t". 'address' . "\t". 'Membership Number' . "\t". 'Membership Category' . "\n";
while($r = mysql_fetch_array($q))
{
$name = $r['name'];
$email = $r['email'];
$phone = $r['phone'];
$address = $r['address'];
$mem_num = $r['mem_num'];
$mem_cat = $r['mem_category'];
echo "$name"."\t"."$email"."\t"."$phone"."\t"."$address"."\t"."$mem_num"."\t"."$mem_cat"."\n";
}
mysql_close($con);
?>
I believe you are confusing data-types here. A phone number for instance, is not a number. But it's called a number! Yeah I know, because it has a lot of numbers in it, but still, it isn't a number... Why?
A phone number is indeed constructed of a set of numerals - the symbols that represent a number - but that doesn't make it a number yet. Compare letters and words; a word is constructed of a set of letters, but not every set of letters is a word. For example: dfslkwpdjkcnj is not a word, not in a language I know of at least... And if it would be a number, how would you add-up two phone numbers? Or how would you divide a phone number by another one? Would that be something like [grandma's phonenumber] / [mom's phonenumber] = [my phonenumber]?
So, to store a phone number in a database, a varchar would be a more suitable column type. For example international phone numbers start with either a + sign, or double zero (00). Both of them can not be stored in a numeral field type. The + isn't a numeral sign, or is used to designate a positive number, and will be cut off. Leading zero's in a number have no function at all, and will be cut off as well...
So bottom line; in your database, use a varchar to store a phone number, and use conversion functions to format a phone number to your liking. There are almost certainly a dozen of algorithms to be found to format a phone number to some kind of a standardized format.
Then back to your excel: your aren't creating an excel file, but a csv file, and you're giving it an excel mime-type. But that would be the same to give someone a cd and say it is a dvd. If you put the cd in a dvd player, it will almost certainly be able to play it, but it is mere luck then wisdom that it does.
Creating an excel file isn't as easy as setting the mime-type, as you can't expect the browser to know how to convert text to an excel file, as it does not know about excel's internals. So if you reaaally want to create an excel file, and set the data types of certain columns, use a library like phpExcel or any other available, if you don't want to create a library yourself that is.
have you tried expanding(stretching) phone column in your excel file? sometime if column is small and numbers are big, excel displays number like(1.23+09) this.
If stretching column does not work. you can convert numbers into string and then put them in excel file
sorry i can't add this in comment as i don't have privilege to comment yet.
If with your API you are able to format cells, that's what you would need to do. You are storing your phone number as a BigInt instead of as a String. I have always stored phone numbers as Strings.
Excel is interpreting your data correctly--as a number, not as text. If you wish to continue to store your phone number as a BigInt (which I don't recommend), you would need to convert it to a String before writing it out to Excel. Or, if your API permits, write it out to Excel as a number, but then apply cell formatting to bring it to the formatting you expect.