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;
}
Related
I'm passing values using ajax from a web page to a php file. The purpose is to pass database credentials to be able to test the database connection and get the result in return. I'm passing server name, username and password to the php file. The problem is that I'm not able to see the database connection response from the php file.
This is how I call the function within document ready:
$('#dbconn').click(db_connect)
Here is my ajax code:
function db_connect(dbserver, dbuser, dbpassword) {
var dbserver;
var dbuser;
var dbpassword;
dbserver = $('input:dbserver').val();
dbuser = $('input:dbuser').val();
dbpassword = $('input:dbpassword').val();
$.ajax({
url : "script/create_db.php",
type : "POST",
cache : false,
data : {
dbserver : dbserver,
dbuser : dbuser,
dbpassword: dbpassword,
success: function(result){alert ("response")}
}
});
};
And this is my php code:
$servername = $_GET["dbserver"];
$username = $_GET["dbuser"];
$password = $_GET["dbpassword"];
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else{
echo "Connection succsess";
}
Any help is highly appreciated
success: function(result){alert ("response")}
When you get the response you alert the string "response".
You ignore the value of result entirely.
You don't see the response because you aren't looking for it.
I am performing a DB operation after making an ajax call using php,
1) I see in fiddler that the request and response to the PHP (ajax call) are fine but and 2) I see the error "Access denied for user 'root'#'localhost' (using password: NO" in Fiddler.
I am not trying to connect to root but to a different user.
Here is the view.php file where ajax call is initiated:
$.ajax({
url: 'delete_entry.php',
data: "id="+del_id,
type: 'post',
success: function(output) {
alert("Success");
//document.getElementById("test1").innerHTML = output;
}
});
I recieve the alert message "Success" though.
Code in delete_entry.php:
<?php
$servername = "localhost";
$username = "testdb";
$password = "testdb";
$dbname = "testts";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
echo "Connection Failed";
die("Connection failed: " . $conn->connect_error);
}
$id=$_POST['id'];
echo $id; // I get a proper Id here
$delete = "DELETE FROM ExpenseTable WHERE Id='"+$id+"'";
$result = mysql_query($delete) or die(mysql_error());
?>
Please help as I do not understand why the mysql db is trying to connect to root eventhough I specify the db detais as "testdb".
I am able to connect to the same db with these credentials in my view.php
You are not using your created DB connection when you pass the sql query to the DB. Use the mysqli_query method as below..
mysqli_query($conn,$delete);
I've been unable to retrieve data from a json or php file because angular keeps yelling at me that either the function or response is undefined.
I want to pull up the navigation items from MySQL, when it failed to load php I've put the contents in a regular json file to see what was the problem it seems something in angular itself.
PHP:
<?php
header('Content-type:application/json');
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbname";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = 'SELECT * FROM navigation';
$result = $conn->query($sql);
$emparray = array();
while($row = mysqli_fetch_assoc($result)) {
$emparray[] = $row;
}
echo json_encode($emparray);
$conn->close();
?>
Which results in:
[{"nav_id":"0","nav_name":"Home","nav_link":""}, {"nav_id":"1","nav_name":"Contact","nav_link":"contact"}]
So far so good?
Here's my angular code that gets the file:
(function() {
var app = angular.module("headerControl", []);
app.directive("headerNavigation", function() {
return {
retrict:'E',
templateUrl:'views/partials/header-navigation.html'
};
});
app.controller("navController", function($scope, $http) {
$scope.navlist = {};
$http.get('phpservices/nav.php')
.success(function(response) {
alert('success');
})
.error(alert('error'));
});
})();
When I load the page it first gives me the error response and after that the success response... in the console angular tells me this:
Error: [ng:areq] http://errors.angularjs.org/1.4.2/ng/areq?p0=fn&p1=not%20a%20function%2C%20got%20undefined
whats wrong about this? I can't seem to figure it out...
The problem is that you are not passing error callback function properly. It should be:
.error(function() {
alert('error')
});
Otherwise you just invoke alert immediately (note ()) and since it doesn't return anything (so it "returns" undefined) error handler receives undefined in it. However, it expects a function. That's why you get this error.
I want to pass an integer parameter in a url to update a field for a record in remote database but I get errors. How can that be done? My php works but not if called from Swift. How do you execute a php file for updating a database record in Swift?
Below is the php on the server for updating record. How do I run this from a Swift app? From the web I do it this way and it runs (IdTask is unique):
http://.../updateonetasktocompleted.php?x=2
<?php
$host = "localhost"; //database host server
$db = "dbname"; //database name
$user = "dbuser"; //database user
$pass = "passwd"; //password
$connection = mysql_connect($host, $user, $pass);
//Check to see if we can connect to the server
if(!$connection)
{
die("Database server connection failed.");
}
else
{
//Attempt to select the database
$dbconnect = mysql_select_db("dbname", $connection);
//Check to see if we could select the database
if(!$dbconnect)
{
die("Unable to connect to the specified database!");
}
else
{
$idtask_selected = $_GET['x'];
$query = "UPDATE ProtocolSets_Protocols SET `ProtocolSets_Protocols`.`Completed`='Yes' WHERE `ProtocolSets_Protocols`.`IdTask`= $idtask_selected";
$resultset = mysql_query($query, $connection);
echo "Successfully added ";
echo $query;
}
}
?>
If you're trying to run a command-line php script from your Swift code, you can use NSTask:
var task = NSTask()
task.launchPath = "/path/to/your/script/script.php"
task.arguments = ["25"]
task.launch()
But I may be misunderstanding your question, since you didn't provide a lot of context.
I figured out one way to do it:
let currentIdTask:Int? = NSUserDefaults.standardUserDefaults().stringForKey("selectedIdTask")?.toInt()
var url: NSString = "http://.../updateonetasktocompleted.php?x=\(currentIdTask!)"
url = url.stringByReplacingOccurrencesOfString(" ", withString: "%20")
url = url.stringByReplacingOccurrencesOfString("/n", withString: "%0A")
println(url)
var data = NSData(contentsOfURL: NSURL(string: url)!)
var result = NSString(data: data!, encoding: NSUTF8StringEncoding)
println(result)
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