1

I use this to get a column named "device_token" and save the values to an array:

mysql_connect("localhost", "xxxx", "xxxx") or die ("Not connected");
mysql_select_db("xxxxx") or die ("no database");
$query = "SELECT xxxx_device_token FROM device_tokens";


$result_array = array();
while($row = mysql_fetch_assoc($result))
{
    $result_array[] = $row['xxxx_device_token'];
}
print_r($result_array);

But all I get is an empty array, what is the problem ?

4
  • That there are no results... Commented Apr 3, 2013 at 12:14
  • Are you sure, your query returns any value? Commented Apr 3, 2013 at 12:15
  • You missed the $result = mysql_query($query). Commented Apr 3, 2013 at 12:15
  • your $query is just a string, you have no mysql_query, neither assigned $result Commented Apr 3, 2013 at 12:16

8 Answers 8

2

your code is not correct, try like this

mysql_connect("localhost", "xxxx", "xxxx") or die ("Not connected");
mysql_select_db("xxxxx") or die ("no database");
$query = "SELECT xxxx_device_token FROM device_tokens";
$result = mysql_query($query);


$result_array = array();
while($row = mysql_fetch_assoc($result))
{
    $result_array[] = $row['xxxx_device_token'];
}
Sign up to request clarification or add additional context in comments.

Comments

0

$result is empty and $query is not used anywhere.

You need something like this:

$result = mysql_query($query);

http://php.net/manual/en/function.mysql-query.php

Comments

0

Your code is not correct. You didn't execute the query. You need to do this :

mysql_connect("localhost", "xxxx", "xxxx") or die ("Not connected");
mysql_select_db("xxxxx") or die ("no database");

$result_array = array();
$query = mysql_query("SELECT xxxx_device_token FROM device_tokens");

while($row = mysql_fetch_assoc($query))
{
    $result_array[] = $row['xxxx_device_token'];
}
print_r($result_array);

But mysql_query is deprecated in new verion of PHP, so you need to do this with mysqli_query.

Comments

0

Try this,

$query = "SELECT xxxx_device_token FROM device_tokens";
$result = mysql_query($query);


$result_array = array();
while($row = mysql_fetch_assoc($result))
{
    $result_array[] = $row['xxxx_device_token'];
}

Dont use Mysql functions. It is deprecated. Please move to Mysqli (or) PDO

Comments

0

You have mysql_fetch_assoc($result) in the while statement, but no variable declaration for $result.

You must execute your query:

$result = mysql_query($query);

Comments

0

Assuming this is not a copy & paste error, you are probably missing this line:

$result = mysql_query($query);

Comments

0

Try not to use mysql_*, it's not recommended, use mysqli_* , ideally you should be using PDO for database connectivity.

That said you're missing this, $result = mysql_query($query);

Comments

0

You have forgotten to execute your MySQL query, like this:

$result = mysql_query($con,"SELECT * FROM Persons");

Comments