Get Btc transaction hash and send to mysql - mysql

Hello since yesterday i am trying to get the transaction hash on blockchain send many api and send to mysql but it dont works. i put to insert at mysql $txid but it give wrong results as per example "2147483647" i really thanks for any help. Below is my code.
<p>$guid="***********";
$firstpassword="**********";
$secondpassword="**************";
$amounta="3599";
$amountb="2000";
$amountc="2000";
$addressa="********";
$addressb="***********";
$addressc="******";
$recipients=urlencode('{
"'.$addressa.'": '.$amounta.',
"'.$addressb.'": '.$amountb.',
"'.$addressc.'": '.$amountc.'
}');
$json_url =
"https://blockchain.info/pt/merchant/$guid/sendmany?password=******&second_password=********&recipients=$recipients&note=********";
$json_data = file_get_contents($json_url);
$json_feed = json_decode($json_data);
$message = $json_feed->message;
$txid = $json_feed->tx_hash;
$tx = "$txid";
$servername = "localhost";
$username = "******";
$password = "*******";
$dbname = "*****";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$<font color="#FF0000">sql = "INSERT INTO payments (user_id, transaction_hash,
payment, date)
VALUES ('5331', '$txid', '4', now())";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;<br>
}
$conn->close();
?>

Lol, sorry that scrypt work very well, the problem i a set the table as INT 11, so i changed it to vachar 255 and its works well.
Thanks

Related

How can we start a connection in mysql and php

Can anyone tell me about how to build a connection between mysql and php page.
Please help me with this.
To start a connection You Might do Following code:-
<?php
$servername = "localhost";
$username = "test";
$password = "test";
$dbname = "test";
//change test with your server details//
$conn = mysqli_connect($servername,$username,$password,$dbname);
if ($conn)
{
//echo "connection ok";
}
else
{
echo "connection Failed";
}
?>
hope this code would help you .
Make conn.php file with this code inside and use $conn variable to prepare queries using prepared statement
if(!defined("connection")){
header('Location:error.php');
exit();
}
define("host","localhost");
define("user","mysql_user");
define("pass","password");
define("dbname","database_name");
$conn=new mysqli(host,user,pass,dbname);
if($conn->connect_error){
die("Failed to connect");
exit();
}
mysqli_set_charset($conn,"utf8mb4");
Use conn.php and $conn variable like this
Page.php
require_once("conn.php");
if($stmt = $conn->prepare($query)){
echo "prepared";
}

Adding data to ENUM field via form in sql

I currently have a form where values are submitted to add data to my table. I have a field 'property_id' which is an ENUM. Every time I try to submit the field I get the error message:
Fatal error: Uncaught mysqli_sql_exception: Data truncated for column 'property_id' at row 1 in ...
Is there any way I can add data to the ENUM field via my form? Here is my code below that connects the form to the database:
ini_set('display_errors', 1);
ini_set('log_errors', 1);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$property_id = filter_input(INPUT_POST, 'property_id');
$firstline_address = filter_input(INPUT_POST, 'firstline_address');
$postcode = filter_input(INPUT_POST, 'postcode');
$vacant = filter_input(INPUT_POST, 'vacant');
if (!empty($property_id)) {
if (!empty($postcode)) {
$host = "";
$dbusername = "";
$dbpassword = "";
$dbname = "";
//new connection
$conn = new mysqli($host, $dbusername, $dbpassword, $dbname);
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ')'
. mysqli_connect_error());
} else {
$sql = "INSERT INTO property (property_id, firstline_address, postcode, vacant)
values ('$property_id', '$firstline_address', '$postcode', '$vacant')";
if ($conn->query($sql)) {
echo "New property has been successfully added. Refresh page to view new property in the table below.";
} else {
echo "Error: ";
//. $sql ."<br>".$conn->error;
}
$conn->close();
}
} else {
echo "All fields are required!";
die();
}
} else {
echo "Please fill out all fields to continue.";
die();
}

Replacing a string with pdo on MySQL

I want to replace a string but I use PDO. Whatever I have tried is not working. Have you ever replaced a mysql string by using PDO connection?
Here is an example which is not working.
try
{
$image = $_POST['data1'];
$id= $_POST['data2'];
$insert = "UPDATE myTable SET images=REPLACE(images,':image','') where id = :id";
$insertStmt = $conn->prepare($insert);
$insertStmt->bindValue(":image", $image);
$insertStmt->bindValue(":id", $id);
$insertStmt->execute();
echo "success";
}
catch(PDOException $ex)
{
echo $ex->getMessage();
}
write :image instead of ':image'
$insert = "UPDATE myTable SET images=REPLACE(images,:image,'') where id = :id";

Function to get mysql results in PHP 7

I'm struggling with the following problem. Some years ago, I've written a function to retrieve MYSQL results (multiple rows). Till PHP7, this code worked fine:
function MultipleRows($query)
{
global $dbhost, $dbname, $dbuser, $dbpass;
mysql_connect($dbhost, $dbuser, $dbpass)
or die("Error! Couldn't <b>Connect</b>.");
mysql_select_db($dbname)
or die("Error! Couldn't <b>Select database</b>.");
$result = mysql_query($query)
or die("Error! Couldn't execute query.");
if(($result)&&(mysql_num_rows($result)>0))
{
return $result;
} else {
return false;
}
mysql_close();
}
Now in PHP7, this code doesn't seem to work anymore. After a lot of searching I came up with this as a replacement but unfortunately it doesn't work:
function MultipleRows($query)
{
$mysqli = new mysqli($dbhost, $dbpass, $dbuser, $dbname);
$result = mysqli_fetch_all($mysqli->query($query), MYSQLI_ASSOC);
return $result;
$mysqli->close();
}
The function is meant to work with code like this:
$res_test = MultipleRows("SELECT id, name FROM table");
if($res_test)
{
while($res = mysql_fetch_array($res_test))
{
echo $res['id'].' '.$res['name'];
}
}
It's not a good option to rewrite the 'display code' (last fragment) because in that case I have to rewrite many lines in my website. Who can give me some help in this? Thanks in advance!
mysql extension has been dropped from PHP 7, after being deprecated in PHP 5.x.
You could rewrite function MultipleRows() using mysqli as follows:
function MultipleRows($query)
{
global $dbhost, $dbpass, $dbuser, $dbname;
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
$result = $mysqli->query($query);
if ($result !== FALSE)
$result = $result->fetch_all(MYSQLI_ASSOC);
$mysqli->close();
return $result;
}
A few remarks:
it is not a good idea to use global variables ($dbhost, $dbuser, $dbpass and $dbname)
this way of handling database queries makes you vulnerable to SQL injection attacks

Find how many times every word is repeated in db

Am using drupal to manage my content. I want to search all my contents title and body and find how many times each word is repeated in the whole contents.
It may be by an sql query, but I have no experience with sql.
Any ideas?
This code searches the body field and ALL fields of ANY Content Types for a specific string. You can run it via command line. Say you save it as "fieldsearch.php", you can then run it as:
php fieldsearch.php "myStringForWhichToSearch"
You need to fill in your connection data and database name. It outputs the array of matching nodes but you can format that output into anything you'd like (I recommend csv).
<?php
//Set Parameters here
$env = "dev"; // Options [dev|prod] - Defaults to dev
$prodConnection = array(
"host" => "",
"user" => "",
"pass" => ""
);
$devConnection = array(
"host" => "",
"user" => "",
"pass" => ""
);
//Use the selected env settings
if($env == "prod"){
$connection = $prodConnection;
} else {
$connection = $devConnection;
}
function getTables($con, $database){
//Get the set of field tables
$sql = "SHOW TABLES FROM $database";
$result = mysqli_query($con, $sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
$tables = array();
while ($row = mysqli_fetch_row($result)) {
$tables[] = $row[0];
}
mysqli_free_result($result);
return $tables;
}
function getFieldTables($con,$database){
$allTables = getTables($con, $database);
$fieldTables = array();
foreach($allTables as $key => $table){
if( isFieldTable($table) ){
$fieldTables[] = $table;
}
}
//add the common tables
$fieldTables[] = "field_data_body";
$fieldTables[] = "field_data_comment_body";
return $fieldTables;
}
function isFieldTable($table){
//echo $table . "\n";
if( stripos($table, "field_data_field") !== FALSE){
return TRUE;
}
}
//Set the search term here:
if (array_key_exists(1, $argv)) {
$searchString = $argv[1];
}
else {
die('usage: php fieldsearch.php "search string"' . "\n");
}
$databaseName = "myDatabaseName";
$outArray = array();
//Connect
$con=mysqli_connect($connection['host'],$connection['user'],$connection['pass'],$databasePrefix.$databaseNum);
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//getFieldTables
$fieldTables = getFieldTables($con, $databaseName);
//Query each field tables data for the string in question
foreach($fieldTables as $key => $table){
//get Field value column name
$valueCol = str_replace("field_data_field_", '', $table);
$result = mysqli_query($con,"SELECT
entity_id
FROM
$table
WHERE
field_" . $valueCol . "_value
LIKE
'%$searchString%';");
if($result){
while($row = mysqli_fetch_assoc($result)){
$dataArray[$table][$row['entity_id']]['nid'] = $row['entity_id'];
}
}
}
//Add the body table
$result = mysqli_query($con,"SELECT
entity_id
FROM
field_data_body
WHERE
body_value
LIKE
'%$searchString%';");
if($result){
while($row = mysqli_fetch_assoc($result)){
$dataArray['field_data_body'][$row['entity_id']]['nid'] = $row['entity_id'];
}
}
var_dump($dataArray);