I am currently using Phonegap along with xcode to make an IPhone App.
I'm just trying a simple Json call to get a query from a database (php) and it's returning Null.
I have Whitelisted the domain in the .plist file for phonegap. Here is m code:
$.getJSON('"http://slc0013.pickaweb.co.uk/~bengeor1/fixtures.php",', function(data) {
alert(data); //uncomment this for debug
//alert (data.item1+" "+data.item2+" "+data.item3); //further debug
$('#resultLog').html("<p>item1="+data.id+" item2="+data.home_team+" item3="+data.away_team+"</p>");
});
PHP code:
<?php
header("Access-Control-Allow-Origin: *");
ini_set('display_errors',1);
error_reporting(E_ALL);
// Set your return content type
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json');
$db = "localhost";
$db_name = "xxx";
$db_user = "xxx";
$db_pwd = "xxxx";
$con = mysql_connect($db, $db_user, $db_pwd);
if (!$con) {
$status = 11; //database error
}
$db_selected = mysql_select_db($db_name, $con);
if (!$db_selected) {
}
$query = "SELECT * FROM Fixtures";
$result = mysql_query($query);
mysql_close();
$num = mysql_numrows($result);
$rows = array();
while($r = mysql_fetch_assoc($result)) {
$rows[] = $r;
}
echo json_encode($rows)
?>
If you run just the php file it displays the correct results.
Would appreciate your help.
Thanks.
Try to replace the first line:
$.getJSON('"http://slc0013.pickaweb.co.uk/~bengeor1/fixtures.php",', function(data) {
to this:
$.getJSON("http://slc0013.pickaweb.co.uk/~bengeor1/fixtures.php", function(data) {
Looks like you are having problem with url '"http://slc0013.pickaweb.co.uk/~bengeor1/fixtures.php",' should be "http://slc0013.pickaweb.co.uk/~bengeor1/fixtures.php" I guess
Related
I want to improve my non-existing PHP knowledge. To do so I created a MySQL DB and a connection.php file with an SQL query (please ignore SQL injection comments for now, I am aware of it). I trying to figure out, how I can split the connection from the actual query.
<?php
header("Access-Control-Allow-Origin: *");
$username = "root";
$password = "root";
$host = "localhost";
$database="test";
$connection = mysqli_connect($host, $username, $password, $database);
$myNodesQuery = "
SELECT * FROM nodes";
$query = mysqli_query($connection, $myNodesQuery);
if ( ! $query ) {
echo mysqli_error();
die;
}
$data = array();
for ($x = 0; $x < mysqli_num_rows($query); $x++) {
$data[] = mysqli_fetch_assoc($query);
}
//echo json_encode($data, JSON_FORCE_OBJECT);
echo json_encode($data);
mysqli_close($connection);
My thoughts were to create another PHP file and add $connection = mysqli_connect (require('connection.php')) to receive the connection string. Unfortunately, I receive a path error.
Keeping your code as is then:
File connection.php:
<?php
$username = "root";
$password = "root";
$host = "localhost";
$database="test";
$connection = mysqli_connect($host, $username, $password, $database);
The main file
<?php
header("Access-Control-Allow-Origin: *");
require 'connection.php';
$myNodesQuery = "
SELECT * FROM nodes";
// whatever follows
...
Please note that - unless you use a framework - it would be much better if you build your reusable connection class or connection-returning function. And BTW consider using the far superior PDO.
I am having some issues with connecting my MySQL table to my php connection script. I am receiving this error whenever I attempt to test the code. (I am using Angular, MySQL Workbench, and php).
zone-evergreen.js:2845 POST http://localhost/angular_admin/php/login.php net::ERR_CONNECTION_REFUSED
The code that is attempting to make the connection with the MySQL server is the database.php script.
<?php
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Credentials: true');
header("Access-Control-Allow-Methods: PUT, GET, POST, DELETE");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
header("Content-Type: application/json; charset=UTF-8");
$db_host = 'localhost:3306';
$db_username = 'root';
$db_password = '';
$db_name = 'users';
$mysqli = new mysqli($db_host, $db_username, $db_password,$db_name);
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
?>
I manually entered a test record within side of my table on the MySQL: test, test#website.com, and test. I used that to login when I received the error listed above. My login.php script looks like this:
<?php
include_once("database.php");
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
if(isset($postdata) && !empty($postdata))
{
$pwd = mysqli_real_escape_string($mysqli, trim($request->password));
$email = mysqli_real_escape_string($mysqli, trim($request->username));
$sql = "SELECT * FROM users where email='$email' and password='$pwd'";
if($result = mysqli_query($mysqli,$sql))
{
$rows = array();
while($row = mysqli_fetch_assoc($result))
{
$rows[] = $row;
}
echo json_encode($rows);
}
else
{
http_response_code(404);
}
}
?>
And of course my users.ts script looks like this:
export class Users {
public Id: number;
public name: string;
public pwd:string;
public email:string;
constructor(Id:number,name: string,pwd:string,email:string) {
this.Id = Id;
this.name = name;
this.pwd = pwd;
this.email = email;
}
}
I have verified that my MySQL Workbench is running as: root, localhost:3306, the name of the schema is users (my database.php script may be looking for the wrong database name?). Is there any steps I am missing to ensure that the database is accessible by the php script? I am running the angular server using Node.js following the steps mentioned on this article https://fahmidasclassroom.com/register-and-login-system-using-angular-8-php-and-mysql/
Does anyone know why when I move a database connection 'include' out of a function and place it at the beginning of the PHP file that I get a POST 500 error? The exact same database connection 'include' works perfectly within the function. Here is the code that causes the error:
<?php
include($_SERVER['DOCUMENT_ROOT'].'/phpscripts/set_path.php'); // DB Connection Path
include 'mysqli_connect_sa.php'; // DB Connection
if(isset($_POST['row']))
{
$uid = $_POST['row'];
$response_bdls = getBDLs($uid);
$response_cg = getCompetencyGaps($uid);
echo json_encode(array($response_bdls, $response_cg));
}
Just in case you want to see the Ajax call:
$.ajax({
type: "POST",
url: '/phpscripts/ss_scbdls.php', //
data: {row : row},
success:(function(response){
response = JSON.parse(response);
loadSC_bdls(response[0]);
loadSC_cg(response[1]);
})
});
Thank you for any insights or direction you can provide.
Requested Information #1
Here is the mysqli_connect_sa.php Content:
// SA Database access information
$hn = 'localhost';
$db = 'xxxxxxxx';
$un = 'xxxxxxxx';
$pw = 'xxxxxxxx';
// Connect to DB
$dbc = new mysqli($hn, $un, $pw, $db);
// Set the Encoding
mysqli_set_charset($dbc, 'utf8mb4');
Function:
function getBDLs($candidate_id)
{
$sc_bdls = [];
$sql_query_bdls = "SELECT bdl_increment AS id, learn_load, start_date, target_date, completed_date, bdl_20, bdl_10, bdl_70, cg, cg_orn, cg_tit, cg_lj FROM bdls INNER JOIN competency_gaps USING (cg_id) WHERE bdls.candidate_id = $candidate_id";
$result_bdls = $dbc->query($sql_query_bdls);
while($bdls = mysqli_fetch_assoc($result_bdls)) {
$sc_bdls[] = $bdls;
}
mysqli_free_result($result_bdls);
return $sc_bdls;
}
I need simple web service to get data from local database, but when I've moved files from test server to actual server I just receive blank page with 'null' in top left corner.
My php code is very simple:
<?php
// Include config
require_once 'config/db.php';
$sql = new db();
$conn = $sql->connect();
$task = isset($_GET['task']) ? mysql_real_escape_string($_GET['task']) : "";
if(!empty($task))
{
if($task === "totals")
{
$qur = mysql_query("working query - no problem here;");
$result =array();
while($r = mysql_fetch_array($qur))
{
extract($r);
$result[] = array("total_value" => $total_value, 'orders_date' => $orders_date, 'number_of_orders' => $number_of_orders);
}
$json = $result;
}
}
#mysql_close($conn);
/* Output header */
header('Content-type: application/json');
echo json_encode($json, JSON_PRETTY_PRINT);
?>
And here is db.php code:
<?php
class db
{
// Properties
private $dbhost = 'ip-address';
private $dbuser = 'xxx';
private $dbpass = 'yyy';
private $dbname = 'zzz';
// Connect
public function connect()
{
$conn = mysql_connect($this->dbhost, $this->dbuser, $this->dbpass);
mysql_select_db($this->dbname, $conn);
}
}
?>
May be Json_encode not giving correct answer.
1) Also please make sure JSON_PRETTY_PRINT is only available for PHP versions >= 5.4. It's value is 128, so try replacing JSON_PRETTY_PRINT with 128.
just on error reporting at top of page to debug
2) Also to make sure once try to print $json before encoding it
3) As mentioned in comment just encode only when you are actually getting data
!empty($json){
header('Content-type: application/json');
echo json_encode($json, JSON_PRETTY_PRINT);
}
Can any of you guys help me to reorganize this code? I'll try to explain the problem below.
I have a db_connection.php wich includes the following (just my db info and don't worry, i am just using it locally):
<?php
try {
$db = new PDO('mysql:host=localhost;dbname=webappeind;charset=utf8','root','');
}
catch(PDOException $e) {
echo $e->getMessage();
}
?>
But the problem is i have the following file that also includes my db info, but i do not know how to reorganize my code to get it working with my separate php file.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "webappeind";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * FROM zoekopdrachten ORDER BY titel DESC LIMIT 3";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output gegevens van elke rij
while($row = $result->fetch_assoc()) {
echo "<div class='recenttoegevoegd'>"."<a href='#'>".$row["titel"]."</a>"."</div>";
}
} else {
echo "0 resultaten";
}
?>
You can put the connection information in a separate file called, say, conn.php where you mention the code related to database opening, including credentials. Then, in the calling file, say putdata.php, you use the "require" or "require_once" command to include that conn.php. Let the conn.php file return a connection. Somewhat like this :
<?php
function GetMyConn() {
$server_name = "localhost";
$db_name = "db_name_goes_here";
$db_user = "user_name_goes_here";
$db_pass = "password_goes_here_muahhhaa";
$db_full_addr = "mysql:host=" . $server_name . ";" . "dbname=" . $db_name;
$MyConn = new PDO($db_full_addr, $db_user, $db_pass);
$MyConn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $MyConn;
}
?>
In the calling file, you would say, something like :
require_once "GetConn.php";
$MyConnHere = GetMyConn();
$SqlHere = "Sql Statemetn goes here...."
$SqlHere2 = $MyConnHere->prepare($SqlHere);
$SqlHere2->execute();
Also, I suggest you can download the source code of some open source PHP application, such as mantissa, and see how they have organized their code files, folders and settings.