Im trying to write a function that uses an external db class but its not going so well. The class works fine when its not in a function. Heres my code:
require_once('MysqliDb.php');
function add_post($userid,$body,$cat_id,$user_link){
$db = new MysqliDb('localhost', 'root', 'root', 'my_database');
$date = new DateTime();
$now = $date->getTimestamp();
$insertData = array(
'user_id' => $userid,
'body' => $body,
'stamp' => $now,
'cat_id' => $cat_id,
'link' => $user_link
);
if ( $Db->insert('posts', $insertData) ) echo 'success!';
}
Heres the call:
$userid = 1;
$body = "hey whats up this is from a db class";
$cat_id = 3;
$user_link = "http://www.aol.com";
add_post($userid,$body,$cat_id,$user_link);
Related
I'm hosting a local wordpress site through xampp, and am testing some code. Basically I wanted to take the form data from a wpforms, and when the user clicks submit it then sends that data to an external mysql database. This is the code
global $wpdb;
function be_db_connector($fields) {
$username = 'example';
$password = 'example';
$database = 'example';
$localhost = 'example';
$wpdb = new wpdb('username','password','database','localhost');
$wpdb->show_errors();
$wpdb->insert('ProspectsDevOnly', array(
'username' => $fields['0']['value'],
'password' => $fields['2']['value'],
'email' => $fields['1']['value'],
'create_time' => 20180526),
array(
'%s',
'%s',
'%s',
'%d',
) );
}
add_action( 'wpforms_process_complete_7', 'be_db_connector', 10, 1 );
I put the code in my themes function.php file, but when I submit the form nothing happens - nothing is sent to my database.
Any idea why this may be? Thanks for the help.
First you can enable debug mode in wp-config.php in root folder.
Assign value for username,password,database,password.
function be_db_connector() {
$username = 'root';
$password = 'root123';
$database = 'account';
$localhost = '127.0.0.1';
//$wpdb = new wpdb('username','password','database','localhost');
$wpdb = new wpdb($username,$password,$database,'localhost');
$wpdb->show_errors();
$insert = $wpdb->insert('account', array(
'firstname' => '',
'lastname' => '',
'age' => '',
'gender' => 20180526,
'emailad' => '',
'username' => '',
'password' =>''
));
}
add_action( 'init', 'be_db_connector', 10, 1 );
I'm new on Soap\NuSoap
I can connect to server site but from server send data back to client i can not get data in array to show client site. I have to test to get data and send data back to other database. I have to get in array.
it's show error
XML error parsing SOAP payload on line 2: Invalid document end
I can't fix it by myself. please help.
This is my client.php
include("lib/nusoap.php");
$client = new nusoap_client("http://192.168.20.3/soap_server/webservice.php?wsdl");
$client->soap_defencoding = 'utf-8';
$client->encode_utf8 = false;
$client->decode_utf8 = false;
$params = array(
'strName' => $_POST["strName"],
);
$result = $client->call("resultCustomer",$params);
$err = $client->getError();
if ($err) {
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
echo '<h2>Debug</h2><pre>' . htmlspecialchars($client->getDebug(), ENT_QUOTES) . '</pre>';
exit();
}
print_r($result);
And this is server.php
require_once("lib/nusoap.php");
//Define our namespace
$namespace = "http://localhost/soap_server/webservice.php";
//Create a new soap server
$server = new soap_server();
//Configure our WSDL
$server->configureWSDL("getCustomer");
$server->wsdl->schemaTargetNamespace = $namespace;
$server->soap_defencoding = 'utf-8';
$server->encode_utf8 = false;
$server->decode_utf8 = false;
//Register our method and argument parameters
$varname = array(
'strName' => "xsd:string"
);
//Add ComplexType
$server->wsdl->addComplexType(
'ArrayOfString',
'complexType',
'array',
'',
'',
array(
'id_user' => array('name' => 'id_user', 'type' => 'xsd:string'),
'firstname' => array('name' => 'firstname', 'type' => 'xsd:string'),
'username' => array('name' => 'username', 'type' => 'xsd:string'),
'lastname' => array('name' => 'lastname', 'type' => 'xsd:string')
)
);
//Add ComplexType
$server->wsdl->addComplexType(
'ArrayOfString',
'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(
array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:DataList[]')
),
'tns:DataList'
);
// Register service and method
$server->register('resultCustomer',$varname, array('return' => 'tns:ArrayOfString'));
function resultCustomer($strName)
{
$objConnect = mysql_connect("localhost","root","") or die(mysql_error());
$objDB = mysql_select_db("qpt-test");
$strSQL = "SELECT * FROM qpt_user where firstname like '%".$strName."%'";
$objQuery = mysql_query($strSQL) or die (mysql_error());
$intNumField = mysql_num_fields($objQuery);
$resultArray = array();
while($obResult = mysql_fetch_array($objQuery))
{
$arrCol = array();
for($i=0;$i<$intNumField;$i++)
{
$arrCol[mysql_field_name($objQuery,$i)] = $obResult[$i];
}
array_push($resultArray,$arrCol);
}
return $resultArray;
}
$POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
// pass our posted data (or nothing) to the soap service
$server->service($POST_DATA);
exit();
I have a PHP file where I use the following PHP code to access my DB to retrieve some data to create a geojson. It does return a geojson but all html etc is exluded when i call this function.
<?php
require_once("db.php");
$geo = connectToDB::getGeoJSON();
?>
This is the function im calling from another a db.php file.
public static function getGeoJSON() {
$db_connection = new mysqli(mysqlServer, mysqlUser, mysqlPass, mysqlDB);
$statement = $db_connection->prepare("Select poiId, lat,lng,description from poi");
$statement->bind_result( $id, $lat, $lng, $description);
$statement->execute();
$feature = array();
$geojson = array(
'type' => 'FeatureCollection',
'features' => $feature
);
while ($statement->fetch()) {
$feature = array(
'type' => 'Feature',
'geometry' => array(
'type' => 'Point',
'coordinates' => array($lng, $lat)
),
'properties' => array(
'description' => $description
//Other fields here, end without a comma
)
);
array_push($geojson, $feature);
}
$statement->close();
$db_connection->close();
//Return routing result
header("Content-Type:application/json",true);
return $geojson;
}
Can anyone see whats wrong with the code? It does return the correct output but then the rest of the page wont show. Other functions I call is working normally so its the geojson function something is wrong with.
I'm trying to create a webservice with PHP and nuSoap but everytime I try to execute it I'm getting the error:
XML error parsing SOAP payload on line 1: Not well-formed (invalid token)
Can anyone see what's wrong?
service.php
<?php
require 'lib/nusoap.php';
$server = new nusoap_server();
$server->configureWSDL("casamitger" . "urn:casamitger");
$server->wsdl->schemaTargetNamespace = 'urn:casamitger';
include 'functions.php';
//getAvailabilities
$server->wsdl->addComplexType('Availabilities','complexType','struct','all','',array(
'StartDate' => array('name' => 'StartDate', 'type' => 'xsd:date'),
'EndDate' => array('name' => 'EndDate', 'type' => 'xsd:date'),
'State' => array('name' => 'State', 'type' => 'xsd:string'),
));
$server->wsdl->addComplexType('ArrayOfAvailabilities', 'complexType', 'array', '', 'SOAP-ENC:Array', array(), array(
array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:Availabilities[]')), 'tns:Availabilities');
$server->register(
'getAvailabilities',
array(
"SessionID" => 'xsd:string',
"AccommodationId" => 'xsd:integer'
),
array("return" => 'tns:ArrayOfAvailabilities')
);
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>
functions.php
function getAvailabilities($sessionID, $accommodation_code) {
$connection = mysqli_connect("localhost", "root", "", "casamitger");
if (authenticate($sessionID)) {
$user = getUser($sessionID);
$query = mysqli_query($connection, "SELECT count(AccommodationId) c FROM UserAccommodations WHERE AccommodationId = '$accommodation_code' AND CompanyId = '$user'") or die();
$row = mysqli_fetch_object($query);
$count = $row->c;
if ($count > 0) {
$query = mysqli_query($connection, "SELECT StartDate,EndDate,State FROM Availabilities WHERE AccommodationId = '$accommodation_code'") or die();
$n = 0;
while ($row = mysqli_fetch_object($query)) {
$result[$n]['StartDate'] = $row->StartDate;
$result[$n]['EndDate'] = $row->EndDate;
$result[$n]['State'] = $row->State;
$n++;
}
return $result;
}
}
}
and the client.php
<?php
require 'lib/nusoap.php';
include 'functions.php';
$sessionid = '1234';
$accommodation_code = '83081';
$client = new nusoap_client("http://192.168.8.155:8090/ws/service.php?wsdl");
$availabilities = $client->call(
'getAvailabilities',
array(
"SessionID" => "$sessionid",
"AccommodationId" => "$accommodation_code",
)
);
if ($client->fault) {
echo 'Fault';
} else {
$err = $client->getError();
if ($err) {
echo $err;
} else {
print_r($servicetypes);
}
}
?>
If I call the method getAvailabilities() directly it works but it doesn't through the web service, any help will be appreciated, thanks.
The following code is working just fine, but the line $connection->query('call user_create(145552, \'a#a.com\');'); is not creating a user into the database.
function updateFacebook($id) {
$connection = mysqli_connect('localhost', 'root', '', 'databasename');
$count = $connection->query('call user_by_facebook_read(' . $id . ');')->num_rows;
if ($count == 0) {
$request = \Slim\Slim::getInstance()->request();
$body = $request->getBody();
$json = json_decode($body);
$token = $json->token;
$facebook = new Facebook(array(
'appId' => 'value',
'secret' => 'value',
'cookie' => true
));
$facebook->setAccessToken($token);
$facebook->api('/me/feed', 'post', array(
'message' => 'message',
'picture' => 'https://www.google.com.br/images/srpr/logo4w.png',
'link' => 'https://www.google.com.br',
'description' => 'description',
'name' => 'name',
'caption'=> 'caption'
));
$connection->query('call user_create(145552, \'a#a.com\');');
}
$connection->close;
$json = json_encode(array(
'r' => true
));
echo $json;
}
If I run the following code it also works just fine:
$connection = mysqli_connect('localhost', 'root', '', 'databasename');
$connection->query('call user_create(145552, \'a#a.com\');');
$connection->close;
If I close the connection after the $count and open it again before the user_create call it also works.
What is happening?
You need to run ->close() on the result, but you discarded that. So:
$result = $connection->query(..);
$count = $result->num_rows;
$result->close();
and after CALL()s in MySQLi it seems to need this:
$connection->next_result();