-1

I have a database with a column "regId" in a table users What I want to do is select all rows but only that column (SELECT regId FROM users)

However, in PHP, I want this as

Array("the first row ID", "the second row ID", and so on)

I am building an app using the Google Cloud Messaging and the registration IDs are needed in an array like "Array("something", "something else")

Here's my code

<?php
session_start();
require_once("global.php");



$qry = "SELECT `regId` FROM `users` WHERE regId IS NOT NULL"; //Here's where the IDs are
$fetchQry = mysqli_query($connection, $qry);

// Replace with the real server API key from Google APIs
$apiKey = "/////";

// Replace with the real client registration IDs




$registrationIDs = array("","","",""); //heres how they need the IDs

// Message to be sent
$message = "somethinnnnnn";

// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';

$fields = array(
    'registration_ids' => $registrationIDs,
    'data' => array( "message" => $message ),
);
$headers = array(
    'Authorization: key=' . $apiKey,
    'Content-Type: application/json'
);

// Open connection
$ch = curl_init();

// Set the URL, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_POST, true);
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields));

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($ch, CURLOPT_POST, true);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields));

// Execute post
$result = curl_exec($ch);

// Close connection
curl_close($ch);
echo $result;
//print_r($result);
//var_dump($result);
?>

And I have checked similar questions and they all use a while loop such as

$rows = [];
while($row = mysqli_fetch_array($result))
{
    $rows[] = $row;
}

However, if I set the $registrationIDs variable equal to $rows or array($rows), it does not work! (even echoing them doesn't show the data)

1

2 Answers 2

1

I ended up sussing it out. Thanks to Create PHP array from MySQL column

Here's what I did:

$column = array();

while($row = mysqli_fetch_array($result)){
    $column[] = $row['regId'];
//Edited - added semicolon at the End of line.1st and 4th(prev) line

}

I did earlier try this however it was originally mysql and not mysqli!! I then also set my $registrationIDs variable equal to $column

Sign up to request clarification or add additional context in comments.

1 Comment

Callum when you know there is already a question like that then you should remove yours otherwise you will get down voted for making site bulky with same repeated questions.
0

try this one:

$rows = [];
while($row = mysqli_fetch_array($fetchQry)) {
    $rows[] = $row;
}

see if it can work.

2 Comments

I did try something like that, however I've just posted my fix. Thanks tho! :)
U can use print_r($variable); to see the structure of any variables when u want to know what it contains.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.