mySQL not returning an array into angularJS - javascript

I am trying to retrieve the data stored in a mySQL table with a PHP script. I want this data to be returned as an array because I then loop through it in my AngularJS app and conduct various transformations etc. I am getting the data out just fine, but it is returned as just one item in an array i.e. each row is not returned as a separate item of the array. My code as it stands is:
PHP Get Request
<?php
require 'config.php';
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = 'SELECT * FROM user_details';
$stmt = $pdo->prepare( $sql );
$stmt->execute();
$result = $stmt->fetchAll( PDO::FETCH_ASSOC );
$json = json_encode( $result );
echo $json;
Database::disconnect();
?>
Angular Controller
$scope.userprofiles = [];
$http.get('php/getUserDetails.php')
.success(function(data) {
$scope.userprofiles = data;
});
I also run some tests to see what the issue is. Specifically, I see if the variable is an array with:
$scope.varcheck = $scope.userprofiles.constructor === Array;
This returns true. And then I check the length of the array with:
$scope.numRecords = $scope.userprofiles.length;
This returns 0.
If anyone had any thoughts it would be a great help.
I also have an issue that if a "/" or a "'" is stored in the database it throws the get request. I assume that it is exiting early. If anyone knew about this it would be great too!
Thanks,
Jack

$http methods return a promise, which can't be iterated, so you have to attach the results to the scope variable through the callbacks:
$scope.userprofiles = [];
$http.get('php/getUserDetails.php')
.then(function(response) {
$scope.userprofiles = response.data;
});
Hope it may help you :-)

Related

passing a variable with get request to PHP

Goal: I am trying to populate a dropdown from a database table column... I need to pass a variable from the front-end to the PHP indicating which table to ping from, as it may change depending on user selection beforehand...
This is where I'm trying to pass my which variable in to use as
this indicator — but I am having a hard time, because I am trying to
do so within a $.get request? If I could get $which = $_GET['id']; to report/gather what I have in my jQuery with let which = $(frm).attr("id"); I should be good to go.
How else could I do this, I don't think I could nor think it would be
any good practice to try and wrap a post around my get or vica
versa? Or chain a post then a get - I just need the PHP to access
my jQuery which variable so it knows which table to query.
Below is my most recent attempt: jquery
$('#agent').click(function(){
let which = $(frm).attr("id");
console.log(which);
$.get('dlteopt', 'id='+which, function (response) { // attempt at id
console.log(response);
$.each(response,function(i,obj){
let dd_data="<option value="+obj+">"+obj+"</option>";
$(dd_data).appendTo('#agent');
console.log(obj);
});
});
});
And the PHP side:
$app->get('/dlteopt', function ($request, $response, $args) {
$which = $_GET['id'];
var_dump($which);
if ($which) {
var_dump($which);
$db = $this->db;
$repo = new coolDBclass($db);
$selectIt = $repo->byCol($which);
}
});
here is function byCol btw: (all should be fine if I can just pass the correct table variable)
public function byCol($which) {
var_dump($which);
if ($which == 'table_1'){
$sql = "SELECT table_1 FROM tab.cool";
} else if ($which == 'table_2'){
$sql = "SELECT table_2 FROM tab.awesome";
} else if ($which == 'table_3'){
$sql = "SELECT table_3 FROM tab.rad";
}
// ............/
the relevant markup:
<select name='agent' id='agent'><option>Placeholder</option></select>
If you're trying to use the second parameter of the $.get method to pass the data, it needs to be an object.
See the examples in the docs.
.get('dlteopt', {id: which }, function (response) {

Null return from controller

I have a service file where I'm running a stored procedure:
function createCampaign($campaignName, $groupNumber){
$stmt = \DB::connection('odbc')->getPdo()->prepare('CALL SCHEMA.INSERT_CAMPAIGN(?,?,?)');
$stmt->bindValue(1,$campaignName, PDO::PARAM_STR);
$stmt->bindValue(2,$groupNumber, $groupNumber==0 ? PDO::PARAM_NULL : PDO::PARAM_INT);
$stmt->bindParam(3,$out2, PDO::PARAM_INT);
$stmt->execute();
return $out2;
}
When I run this stored procedure, the third parameter is giving back OUT_CAMPAIGN_ID and the expected ID, so this works. I'm returning that output variable with $out2
My controller, which calls the previous function and also expects the return back:
public function createCampaign(Request $request)
{
$campaignName = $request->campaignName;
$groupNumber = $request->groupNumber;
$campaignService = new CampaignService();
$createCampaign = $campaignService->createCampaign($campaignName, (int) $groupNumber);
return Response::json(["OUT_CAMPAIGN_ID" => $createCampaign]);
}
However, when I console log data.OUT_CAMPAIGN_ID in my blade, or even console log data it just gives me OUT_CAMPAIGN_ID:null
Am I doing something wrong in the way I expect it back in the controller?
Stored procedure:
BEGIN
INSERT INTO SCHEMA.TABLE(NAME,NUMBER)
VALUES (IN_NAME, IN_NUMBER);
SET OUT_ID = IDENTITY_VAL_LOCAL();
END;
It sounds like your stored procedure returns a value? I think what you need to do is more like:
$stmt = \DB::connection('odbc')->getPdo()->prepare('CALL SCHEMA.INSERT_CAMPAIGN(?,?,?)');
$stmt->bindValue(1,$campaignName, PDO::PARAM_STR);
$stmt->bindValue(2,$groupNumber, $groupNumber==0 ? PDO::PARAM_NULL : PDO::PARAM_INT);
$stmt->bindParam(3, $out2, PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT);
$stmt->execute();

How do I return $wpdb->get_results as json in WordPress?

I created a custom table in wordpress called custom_users and I want to access the records via the API
functions.php
function get_wp_custom_users()
{
global $wpdb;
$custom_users = $wpdb->get_results("SELECT * FROM wp_custom_users");
foreach ($custom_users as $custom_user)
{
echo $custom_user->first_name.' '.$custom_user->last_name;
}
}
add_action('rest_api_init', function()
{
register_rest_route( 'wpcustomusers/v1', '/users/', array(
'methods' => 'GET',
'callback' => 'get_wp_custom_users'
));
});
The API can be accessed via url http://localhost/mywebsite/wp-json/wpcustomusers/v1/users
Do you know how can I return the results inside the for loop as JSON from the API request?
I hope to see all the fields returned.. Thanks for the help.
There is no need of foreach loop, make changes as below.
function get_wp_custom_users(){
global $wpdb;
$custom_users = $wpdb->get_results("SELECT * FROM wp_custom_users");
//change as below.
echo json_encode($custom_users);
}
var_dump $custom_users to make sure $custom_users is not empty. Skip for loop. Replace return with echo.
echo json_encode($custom_users);

How do I use a php array within an array returned to Javascript?

I've been at this for more than half a day trying to figure out this problem and I swear I've tried every possible thing. So here's the idea behind what I'm trying to do... Every 10 seconds Javascript performs an AJAX call to see if you have any friends online, then returns a list of users, their status etc... Instead of formatting everything from PHP, I'll be formatting it from Javascript for various reasons... So here's what happens:
Javascript
// Let's get the data from the controller
$.post('/pagething', { datastuff }, function(data){
if(data.status == 'ok'){
// Magic nonsense here that can translate the array example:
var keys = Object.keys(data.allfriends);
}
} etc...
PHP
// Let's skip other code in the controller and focus on the important stuff
$friends_information = array(
'userid' => array();
'username' => array();
'avatar' => array();
'status' => array();
);
foreach($result_from_my_friends_model as $row){
// For ease of read, i'll just associate things with $row
$friends_information["userid"][] = $row->user_id;
$friends_information["username"][] = $row->username;
$friends_information["avatar"][] = $row->avatar;
$friends_information["status"][] = $row->status;
}
$result = array('status' => 'ok', 'allfriends' => $friends_information);
return json_encode($result);
exit();
The closest I've gotten is to either get the results by say username, or userid for example through a new object or getting the entire result but unable to distinguish between keys since object[0][1] for instance would return undefined.
Thank you in advanced, this stuff is tough to understand :/
There's no need to put each column into a separate element of $friend_array, things are generally easier if you keep all the data related to a particular friend together in an object. So do:
$result = array('status' => 'ok', 'allfriends' => $result_from_my_friends_model);
echo json_encode($result);
In the Javascript, make sure you specify that the result is JSON:
$.post('/pagething', { datastuff }, function(data) {
if (data.status == 'ok') {
$.each(data.allfriends, function(i, friend) {
// do stuff with friend.userid, friend.username, friend.avator, friend.status
});
}
}, 'json');
You already have your list_of_friend in your keysvariable of js. Just iterate through it then you will get your desired result. Best of luck

SSE loads lots of data and paralyzes AJAX post requests

This is my sse_server.php file
include_once 'server_files/init2.php'; //this file includes the connection file to the database and some functions
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$assocArray = array();
$fetch_article = $dbh->prepare("SELECT
article_author_id,
article_author_un,
article_id,
article_cover,
article_title,
article_desc
FROM articles ORDER BY article_id DESC");
$fetch_article->execute();
while ($fetch = $fetch_article->fetch(PDO::FETCH_ASSOC))
{
$article_author_id = $fetch['article_author_id'];
$article_author_u = $fetch['article_author_un'];
$article_id = $fetch['article_id'];
$article_cover = $fetch['article_cover'];
$article_title = $fetch['article_title'];
$article_desc = $fetch['article_desc'];
$randomNum = rand(0,500);
//Insert the Random Number along with the article's info | Random Number as a Value and the contents as a Key
$assocArray[
'randomNum'.'|'. //0
$article_author_id.'|'. //1
$article_author_u.'|'. //2
$article_id.'|'. //3
$article_cover.'|'. //4
$article_title.'|'. //5
$article_desc //6
] = $randomNum;
}
//sort the array
arsort($assocArray, 1);
//echo '<pre>';
//print_r($assocArray);
//while(true){
$var = '';
foreach ($assocArray as $key => $value) {
$var .= $value .' => ' . $key . '`|||`<br>';
}
echo "retry: 6000\n";
echo "data: {$var}\n\n";
ob_flush();
flush();
//}
and this is how I'm processing the data in client.php file
<div id="feeds"></div>
<script>
if(typeof(EventSource)!=="undefined") {
var eSource = new EventSource("sse_server.php");
//detect message received
eSource.addEventListener('message', function(event) {
var jsV_feeds = event.data;
var eventList = document.getElementById("feeds");
var jsV_feedsArray = jsV_feeds.split('`|||`'); //Seperator
eventList.innerHTML = jsF_ToFetch(jsV_feedsArray);
}, false);
}
else {
document.getElementById("feeds").innerHTML="Whoops! Your browser doesn't receive server-sent events.";
}
function jsF_ToFetch(jsP_array)
{
var string = ''; //an empty string
for (var i = 0; i < jsP_array.length-1; i++)
{
jsV_Feed = jsP_array[i].split('|');
jsV_randomNum = jsV_Feed[0];
jsV_article_author_id = jsV_Feed[1];
jsV_article_author_u = jsV_Feed[2];
jsV_article_id = jsV_Feed[3];
jsV_article_cover = jsV_Feed[4];
jsV_article_title = jsV_Feed[5];
jsV_article_desc = jsV_Feed[6];
string += jsV_randomNum +'<li><b>'+jsV_article_author_u+'</b><!--process the rest in a similar way--> </li>';
} // for loop ENDS here
return '<ol>' + string + '</ol>';
}
</script>
The Problem is if I use the foreach loop only, it reconnects every 6 seconds.
And if I wrap the foreach inside a while loop it keeps the connection alive but continously keeps on sending data. This eventually loads up a lot of data within seconds. Also it makes AJAX Post request very slow which is executed via another page simultaneously.
Why is that happening ?
How can I get it to keep the connection open, not send data, and not slow down the AJAX post requests.
PS: I have visited these links -
http://www.html5rocks.com/en/tutorials/eventsource/basics/
PHP Event Source keeps executing
May be I didn't understood them very well enough. If it could be boiled down to even simpler terms, kindly do it!
Thanks in advance!
You want to be using the while(true){} loop that you've commented out in sse_server.php. Your SSE script should never exit (until the socket is closed, which would happen from client-side, i.e. your JavaScript script closing it, or the browser window being closed).
The reason you have problems when using the while loop is that there is no sleep() or wait action inside the while loop. So you are sending data to the client (the same data over and over again!), at maximum rate.
Conceptually, what I'm guessing you are after is this code:
$lastID = 0;
while(true){
$fetch_article = $dbh->prepare("SELECT something FROM somewhere WHERE conditions AND articleID > ?");
$results = $fetch_article->execute($lastID);
if(has 1+ results) {
foreach($results){
echo result formatted for SSE
$lastID = ...;
}
flush();ob_flush();
}
sleep(5);
}
This is saying it will poll the DB every 5 seconds for new records. If there are no new records it does nothing - just goes back to sleep for another 5 seconds. But if there are new records it pushes them out over SSE to the client.
You can adjust the 5 second sleep to find the balance between CPU usage on the server and latency. Shorter sleep means lower latency (your clients get the new data sooner), but higher CPU on the server.
Aside: The lastID approach above is just some way of detecting what records you have seen, and have not yet seen. It is good if you have a unique ID in your table, which is AUTO_INCREMENT. But, alternatively, if DB records are inserted with a created timestamp, then the query becomes:
$now = 0;
while(true){
$stmt = prepare( "SELECT ... AND created > ?" );
$stmt->execute( $now );
$now = time();
...process results ...
sleep(5);
}
(Slightly safer is to set $now to the maximum created timestamp that was found in results, rather than to time() each time; otherwise it is possible for a record to slip through the cracks and not get sent to clients.)

Categories