Flex PHP service - php

Already tearing my hairs out for a couple of days. There is not much left of them ;-)
I am experiencing a strange problem when I want to bind a service to a button or something else:
files:
- CDPC.php
<?php
require_once ('VOcdpc.php');
class CDPC {
var $username = "root";
var $password = "";
var $server = "localhost";
var $port = "3306";
var $databasename = "xoffercommon";
var $tablename = "tblcity";
var $connection;
public function __construct() {
$this->connection = mysqli_connect(
$this->server,
$this->username,
$this->password,
$this->databasename,
$this->port
);
mysqli_set_charset($this->connection,'utf8');
$this->throwExceptionOnError($this->connection);
}
public function getCDPC($cityID) {
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("xoffercommon", $con);
$cdpc_Id = new Vocdpc();
$cdpc_Id->id_cdpc = 1;
$cdpc_Id->city_Id=$cityID;
$result_prov = mysql_query("SELECT tblProvence_Id FROM tblCity WHERE Id = " . $cityID);
$row = mysql_fetch_array($result_prov);
$cdpc_Id->provence_Id=intval($row['tblProvence_Id']);
$result_dist = mysql_query("SELECT tblDistrict_Id FROM tblProvence WHERE Id = " . $cdpc_Id->provence_Id);
$row = mysql_fetch_array($result_dist);
$cdpc_Id->district_Id=intval($row['tblDistrict_Id']);
$result_coun = mysql_query("SELECT tblCountry_Id FROM tblDistrict WHERE Id = " . $cdpc_Id->district_Id);
$row = mysql_fetch_array($result_coun);
$cdpc_Id->country_Id=intval($row['tblCountry_Id']);
return $cdpc_Id;
mysql_close($con);
}
private function throwExceptionOnError($link = null) {
if($link == null) {
$link = $this->connection;
}
if(mysqli_error($link)) {
$msg = mysqli_errno($link) . ": " . mysqli_error($link);
throw new Exception('MySQL Error - '. $msg);
}
}
}
?>
VOcpdc.php
<?php
class VOcdpc
{
public $id_cdpc;
public $country_Id;
public $district_Id;
public $provence_Id;
public $city_Id;
// explicit actionscript class
var $_explicitType = "Vocdpc";
}
?>
In flex builder
I can add the services to the Data Services panel but I have two strange things:
1) when I want to configure the return type he doesn't let me create a new ValueObject type, I only get the bottom datagrid which states: Properties returned by the operation: Property: country_Id, provence_Id, city_Id, id_cdpc, district_Id with the related values on the right side. Why can't I create a new data type on the top?
2) When I accept this and want to add the service call to a button (drag&drop) I get the following error: Error occurred while generating code. Make sure that there are no compiler eroors and try again after reopening the file. Componentn type services.cdpc.CDPC not found...
(ps: When I perform a Test Operation everything seems to be ok, I get the expected output values)

this is the class included in the main cdpc.php file, the post drops it apparently, so here is the VOcpdc file:
// explicit actionscript class
var $_explicitType = "Vocdpc";
}
?>

Related

WWW in Unity returning empty string

Its returning an empty string and I can't seem to figure out why, if you visit the page it outputs fine. (http://lgdev.site.nfoservers.com/getItems.php). I'm trying to get all the data, separate it by certain characters, then use the code in the game t operate it and sort it into the correct places. But like I said its returning an empty string instead of the data that's show on the webpage I gave above.
Game Code:
public void loadItems()
{
WWWForm form = new WWWForm();
WWW w = new WWW("http://lgdev.site.nfoservers.com/getItems.php", form);
StartCoroutine(loadItemsFunc(w));
}
IEnumerator loadItemsFunc(WWW w)
{
yield return w;
Debug.Log(w.text);
string[] tmpdata1 = w.text.Split(char.Parse("="));
foreach(string data in tmpdata1)
{
string[] tmpinfo = data.Split(char.Parse("~"));
GameObject tmpObj = Instantiate(weapon);
tmpObj.GetComponent<weapon>().name = tmpinfo[0];
tmpObj.GetComponent<weapon>().vitalisim = int.Parse(tmpinfo[1]);
tmpObj.GetComponent<weapon>().defence = int.Parse(tmpinfo[2]);
tmpObj.GetComponent<weapon>().strength = int.Parse(tmpinfo[3]);
string[] offsetInfo1 = tmpinfo[5].Split(char.Parse(";"));
foreach (string off in offsetInfo1)
{
string[] offset = off.Split(char.Parse(","));
tmpObj.GetComponent<weapon>().offsetsPos.Add(new Vector3(float.Parse(offset[0]), float.Parse(offset[1]), float.Parse(offset[2])));
tmpObj.GetComponent<weapon>().offetsRot.Add(new Vector3(float.Parse(offset[3]), float.Parse(offset[4]), float.Parse(offset[5])));
tmpObj.GetComponent<weapon>().offetsSize.Add(new Vector3(float.Parse(offset[6]), float.Parse(offset[7]), float.Parse(offset[8])));
}
string[] typeInfo = tmpinfo[4].Split(char.Parse(","));
if (tmpinfo[1] == "SwordnShield")
{
tmpObj.GetComponent<weapon>().anim = swordAndShieldAnimSet;
}
else if (tmpinfo[1] == "TwoHanded")
{
tmpObj.GetComponent<weapon>().anim = twoHandedAnimSet;
}
foreach (string prefab in tmpinfo[6].Split(char.Parse(",")))
{
tmpObj.GetComponent<weapon>().prefabs.Add(prefab);
}
}
}
PHP Code:
<?PHP
$con = mysql_connect("localhost","user","passs") or ("Cannot connect!" . mysql_error());
if (!$con)
die('Could not connect: ' . mysql_error());
mysql_select_db("lgdev_projectzed" , $con) or die ("could not load the database" . mysql_error());
$query = "SELECT * FROM `equipment`";
$check = mysql_query($query) or die(mysql_error()." ".$query);
if($check){
}
$numrows = mysql_num_rows($check);
$text = "";
while ($row = mysql_fetch_array($check, MYSQL_ASSOC)) {
$text = $text."".$row["name"]."~".$row["vitalism"]."~".$row["defence"]."~".$row["strength"]."~".$row["type"]."~".$row["offsets"]."~". $row["prefabs"] ."=";
}
die($text);
?>
First, check if it gives any error with:
Debug.Log(w.error); // place this before or after the other debug.log
Since the form that you submit is empty, there is some error..
So check how to setup your form:
https://docs.unity3d.com/ScriptReference/WWWForm.html
If you don't need to post anything in the form, can use the plain WWW
https://docs.unity3d.com/ScriptReference/WWW.html

Client looks like we got no XML document in soapclient php

I have problem in using SoapClient in php. Considering that it is my fist try in authenticating user credentials so I might have some basic mistakes in my code as well.
I have a simple html tags that takes user credentials(on client side) and sends them to a processing page (works in backend) and sends soap message to server page using __soapCall. Here is the code.
Please help with your suggestions
Client.php
<html>
<body>
<form method='POST' action='middle_client.php'>
<lable>User Name</lable><input type='text' name= 'user' id='user'>
<br>
<lable>Password</lable><input type='password' name= 'pass'>
<br>
<lable>Insurance Name</lable><input type='text' name= 'insurance'>
<br>
<input type='submit' name= 'submit'>
</form>
<body>
</html>
Middle_client.php
<?php
use \SoapClient;
if(isset($_POST['submit'])){
$name= $_POST['user'];
$password= $_POST['pass'];
$insurance= $_POST['insurance'];
$con=mysql_connect("localhost","root","");
// Check connection
if (!$con) {
die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db($insurance, $con);
if (!$db_selected) {
die('Invalid query: ' . mysql_error());
}
if ($db_selected=='insurance'){
//header('Location:server.php');
}
}
class client{
function __construct(){
$parameters = array('location' => 'http://localhost/XXX/server.php',
"uri" => 'urn://localhost/XXX/',
'trace' => 1,
);
$this->instance = new SoapClient(NULL,$parameters);
$auth_parameter = new stdClass();
$auth_parameter->name = "ABC";
$auth_parameter->password = "root";
$header_param = new SoapVar($auth_parameter, SOAP_ENC_OBJECT);
$header = new SoapHeader('XXX', 'authenticate', $header_param, false);
$this->instance->__setSoapHeaders(array($header));
}
public function getname($array){
return $this->instance->__soapCall('testing', $array);
}
}
$client = new client();
$array = array ('P_name'=> 'Foo');
echo $result = $client->getname($array);
var_dump($client);
?>
and Server.php
<?php
class server{
private $con;
public function authenticate($header_param){
if ($header_param->name == 'ABC' && $header_param->password == 'root' ){
return true;
}
else throw new SOAPFault("Wrong Name/Password", 401);
}
public function __construct(){
$this->con = (is_null($this->con)) ? self::connect() : $this->con;
}
static function connect(){
$con=mysql_connect("localhost","root","insurance");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$db = mysql_select_db("user", $con);
return $con;
}
public function testing($array){
$usernme = $array['P_name'];
$sql = "SELECT * from user where P_name=".$usernme;
$qry= mysql_query($sql,$this->con);
$result = mysql_fetch_array($qry);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
/return $returned_name= $result["P_name"];
}
}
$parameter = array("uri" => 'localhost/XXX/server.php');
$server = new SoapServer(NULL, $parameter);
$server-> setClass('server');
$server-> handle();

How to make a list of most ordered item on database?

I have a MVC like store.
I wish to make a list of the most bought items (in this case prints, because it's a print store).
I'm finding it very hard specially because I'm very new to php / mysql and specially to this MVC structure... I hope this isn't a bad question.
I have a model.php like this :
<?php
class model {
private $conn;
function __construct() {
$server = "localhost";
$login = "root";
$pass = "";
$db = "printstore";
$conn = new mysqli($server, $login, $pass, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
$this->conn = $conn;
}
}
function __destruct() {
$this->conn->close();
}
function buildArr($result) {
$arr = array();
while ($row = $result->fetch_assoc()) {
array_push($arr, $row);
}
return $arr;
}
}
a controller.php:
<?php
include 'model.php';
class Controller {
private $model;
function __construct() {
$this->model = new model();
}
function home() {
include 'views/home.php';
}
}
a index.php:
<?php
session_start();
include 'controller.php';
define("BASE_URL", 'http://' . $_SERVER['SERVER_NAME'] . '/printstore2/index.php/');
define("MAIN_BASE_URL", 'http://' . $_SERVER['SERVER_NAME'] . '/printstore2/');
$controller = new controller();
include 'views/templates/header.php';
if (isset($_SERVER['PATH_INFO'])) {
$url = explode("/", $_SERVER['PATH_INFO']);
$function_name = $url[1];
if (isset($url[1]) && $url[1] !== "") {
if (isset($url[2])) {
$controller->$function_name($url[2]);
} else {
$controller->$function_name();
}
} else {
$controller->home();
}
} else {
include 'views/home.php';
}
include 'views/templates/footer.php';
And the view where I want to post the "Best Selling Prints":
<div>
<h2>Top Prints</h2>
<ol>
<li>1st print most bought</li>
<li>2nd</li>
<li>3rd</li>
</ul>
Now, my database has a table called "print_for_sale" which has the print_id and the sale_id, where I can see how many of each prints has been bought.
How can I do this? I'm so lost!
I'm sorry for the long post.
Use COUNT and GROUP BY
to get a list of all prints vs their sale count:
SELECT fk_print_id as printId, COUNT(print_for_sale_id) as saleCount
FROM print_for_sale
GROUP BY fk_print_id
ORDER BY saleCount DESC
that is, if i understood your table right.

PHP Class Function Ignores Return Statement

For some reason the return doesn't work when the check_em() succeeds. I'm new to php, so I'm at a loss here.
<?php
//Class to handle mysql
class db_handler {
private $db_host = 'localhost';
private $db_name = 'project';
private $db_user = 'project';
private $db_pass = 'dbpassword';
private $db_con_mysql = '';
private $db_con_db = '';
public function check_em($username, $password) {
$db_query = "SELECT password FROM user WHERE name='".$username."' LIMIT 1;";
if($this->db_con_mysql!='') {
$db_query_response = mysql_query($db_query) or die('Query failed: '.mysql_error());
$db_query_return = mysql_fetch_row($db_query_response);
$db_sha1_hash = $db_query_return[0];
echo $db_sha1_hash."<br>";
echo sha1($password)."<br>";
if(sha1($password)==$db_sha1_hash) {
return 'user valid'; //THIS DOESN'T WORK!?!?!?
} else {
return 'no good';
}
} else {
$this->db_connect();
$this->check_em($username, $password);
}
}
//Connect to mysql, then database
private function db_connect() {
$this->db_con_mysql = mysql_connect($this->db_host, $this->db_user, $this->db_pass) || die('Connection failed: '.mysql_error());
$this->db_con_db = mysql_select_db($this->db_name) || die('Could not use'.$this->db_name.'. '.mysql_error());
return;
}
//Disconnect from database and reset vars used to track connection.
private function db_disconnect() {
if($this->db_con_mysql!='') {
mysql_close();
$this->db_con_mysql = '';
$this->db_con_db = '';
return;
}
}
public function fake($some_val) {
if($some_val<6) {
return TRUE;
} else {
return FALSE;
}
}
}
$db_obj = new db_handler();
$val1 = $db_obj->check_em('someuser','password'); //should return 'user valid'
echo "val1:".$val1."<br>";
echo "<br><br>";
$val2 = $db_obj->check_em('someuser','passw0rd'); //should return 'no good'
echo "val2:".$val2."<br>";
echo "<br><br>";
echo "test<br>";
echo $db_obj->fake(4)."<br>";
?>
Results:
5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
val1:
5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
7c6a61c68ef8b9b6b061b28c348bc1ed7921cb53
val2:no good
test
1
This line needs a return:
return $this->check_em($username, $password);
But a more sensible solution would be to connect to the database inside the if when the connection is null. Really, the whole thing could be better written, but I'll leave it at that.
...
else {
$this->db_connect();
return $this->check_em($username, $password);
}
...
You want to add the return, so that if it fails, then it goes one level deeper and finds another. If that level deeper succeeds, it passes the value up to the level above, which can pass it up and up until it reaches the original function call.

MySQL connect parameters won't work when stored in another file (sometimes)

This has been baffling me for a few hours now and I'm reaching the point of giving up.
My project has 3 relevant files:
dbconfig.php
register.php
testmysql.php
dbconfig.php has this function:
public function connString()
{
$connString['host'] = $this->host;
$connString['username'] = $this->username;
$connString['password'] = $this->password;
$connString['database'] = $this->database;
return $connString;
}
I do this in testmysql.php and it works perfectly:
require('dbconfig.php');
$dbObject = new dbconfig();
$conn = $dbObject->connString();
mysql_connect($conn['host'],$conn['username'],$conn['password']) or die(mysql_error());
mysql_select_db($conn['database']) or die(mysql_error());
But when I do the same thing in register.php, it fails:
<div class="div_texbox">
<select id="field1" name="field1" class="textbox">
<option value="0">Select</option>
<?php
require('dbconfig.php');
$dbObject = new dbconfig();
$conn = $dbObject->connString();
mysql_connect($conn['host'],$conn['username'],$conn['password']) or die(mysql_error());
mysql_select_db($conn['database']) or die(mysql_error());
$result = mysql_query("SELECT * FROM table") or die ('error selecting');
while ($row = mysql_fetch_array( $result ))
{
echo "<option value=\"" . $row['ID'] . "\">" . $row['value'] . "</option>";
}
?>
</select>
</div>
Details on when I say 'it fails':
Field1 is a select box that is supposed to populate, but it doesn't and no further fields in the form show up (presumably because of the die).
I tried installing a PHP debugger to see the message being thrown but could not get one working.
If I hardcode in the connect parameters, it all works.
A var_dump of $conn shows it to be holding all the right values at all the right indices (also evident in the fact that testmysql.php works).
Also, it some troubleshooting made it seem that it is failing at mysql_connect
I analyzed my HTML code of the rendered page to see the output of the PHP script and this is what it shows:
( ! ) Fatal error: Cannot redeclare class dbconfig in C:\wamp\www\site\dbconfig.php on line 10
Call Stack
#TimeMemoryFunctionLocation
10.0005698544{main}( )..\register.php:0
21.2728763824require( 'C:\wamp\www\site\dbconfig.php' )..\register.php:106
dbconfig constructor
public function __construct($config = NULL)
{
if ($config != NULL)
{
$this->host = $config['hostname'];
$this->database = $config['database'];
$this->username = $config['username'];
$this->password = $config['password'];
}
else
{
$this->host = $this->defaultConfig['hostname'];
$this->database =$this->defaultConfig['database'];
$this->username = $this->defaultConfig['username'];
$this->password = $this->defaultConfig['password'];
}
}
Looks like you're including dbconfig.php more than once which is causing the cannot redeclare class fatal error.
Change any reference of
require('dbconfig.php')
to
require_once 'dbconfig.php';
While you're at it, stop using or die() and switch to PDO and parameterised statements.

Categories