Store mysql column to array PHP - php

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 ?

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'];
}

$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

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.

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

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);

Assuming this is not a copy & paste error, you are probably missing this line:
$result = mysql_query($query);

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);

u have forgotten to execute your mysql query, like this
$result = mysql_query($con,"SELECT * FROM Persons");

Related

Use data from one SQL-query to another

I need to store the values of the column 'following' in an array. However, I can't figure out what's wrong with this code.
session_start();
$connect = mysqli_connect("localhost", "root", "root", "user1");
$user = $_SESSION['username'];
$result = mysql_query("SELECT * FROM Followers WHERE user='$user'");
$data = array();
while ($row = mysql_fetch_array($result)) {
$data[] = $row['following'];
}
It's easier to use a jointure, which will be far more efficient :
Select * from message left join followers on followers.following=Messages.user where Follower.user=...
HTH
Regards
Your solution will encounter problems if you forget to escape caracters like " ' " or " " " or even " \ ".
If I was you, I'd merge into a subquery this way:
$sql = "SELECT * FROM Messages WHERE user IN
(SELECT * FROM Followers WHERE user='$user')"
$result = mysqli_query($connect, $sql );
cheers!
Replace the below
mysql_fetch_array($result) with mysqli_fetch_array($result)
Hope it works!
try this
session_start();
$connect = new mysqli("localhost", "root", "root", "user1");
$user = $_SESSION['username'];
$result = mysqli_query($connect,"SELECT * FROM Followers WHERE user='$user'");
$data = array();
while ($row = mysqli_fetch_array($result)) {
$data[] = $row['following'];
}
As was pointed out you ought to use a prepared statement to avoid nasty sql injection. As the only field that is being used is follower limit the columns returned ( makes it easier using below notation - notably bind_result )
session_start();
$data = array();
$db = new mysqli("localhost", "root", "root", "user1");
$user = $_SESSION['username'];
$sql='select `following` from `followers` where user=?';
$stmt=$db->prepare($sql);
if( $stmt ){
$stmt->bind_param('s',$user);
$res=$stmt->execute();
if( $res ){
$stmt->bind_result($follower);
while( $stmt->fetch() ){
$data[]=$follower;
}
$stmt->free_result();
$stmt->close();
}
}
$db->close();
You have 3 errors in code:
1.mysql_query() is deprecated and may give error so use mysqli_query() which expects 2 paramter connection and query.
2.Your query is not receiving user variable value since its a string.
3.Your row is not an associative array for which you can get following value so use mysqli_fetch_assoc() instead.
You can use the following code:
$connect = mysqli_connect("localhost", "root", "root", "user1");
$user = $_SESSION['username'];
$result = mysqli_query($connect,"SELECT * FROM followers WHERE user='".$user."'");
$data = array();
while ($row = mysqli_fetch_assoc($result)) {
$data[]=$row['following'];
}

Table 'databasename.info' doesn't exist

So I installed this jackpot script with a layout and everything and within the jackpot script there was a set.php file which I tried to set up, it looked like this:
<?php
$sitename = "csgoxd.net";
$link = #mysql_connect("localhost:3306", "csgoxdne", "thisisasecretpassword");
$db_selected = mysql_select_db('csgoxdne_csgoxddb', $link);
mysql_query("SET NAMES utf8");
function fetchinfo($rowname,$tablename,$finder,$findervalue) {
if($finder == "1") $result = mysql_query("SELECT $rowname FROM $tablename");
else $result = mysql_query("SELECT $rowname FROM $tablename WHERE `$finder`='$findervalue'") or die (mysql_error());
$row = mysql_fetch_assoc($result);
return $row[$rowname];
}
?>
So I'm new when it comes to coding in general (I know some basic stuff but that's it) so basically I'm not sure if I'm supposed to fill out more of this file because I get this error on my website.
"Table 'csgoxdne_csgoxddb.info' doesn't exist"
I'm new to this and I'm trying to learn so help is much appreciated.
You should use MySQLi to make use of its advantages it offers over MySQL. You can see more here.
The script you have isn't all too bad, but it does need some tweaking. It's vulnerable to injection like Marc B said. I'm going to assume that csgoxdne_csgoxddb is your table name.
Try this:
<?php
$mysqli = new mysqli("localhost:3306", "csgoxdne", "thisisasecretpassword");
if (mysqli -> error){ print ("Error connecting! Message: ".$mysqli->error); }
mysqli_set_charset($mysqli, 'utf8');
function fetchinfo($rowname, $tablename, $finder, $findervalue) {
if ($finder == "1") {
$query = "SELECT * FROM $tablename WHERE rowname = '$rowname'";
$result = mysqli_query($mysqli, $query);
} else {
$query = "SELECT * FROM $tablename WHERE `$finder`='$findervalue'";
if (!$query) {
die('Invalid query: ' . $mysqli->error);
}
$result = mysqli_query($mysqli, $query);
}
return $result;
}
?>
Oh and make sure the port number on your localhost is correct.
Also to go through the values of result you can use:
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
#do things
}
}

PHP No database found

So it says no database selected, please help me as my hosting cant
<?php
$sitename = "http://devilgaming.phy.sx/csgopie.xyz/";
$link = #mysql_connect("localhost", "devilgam_csgopie", "ssb12boycek1");
$db_selected = mysql_select_db('devilgam_csgopie', $link);
mysql_query("SET NAMES utf8");
function fetchinfo($rowname,$tablename,$finder,$findervalue)
{
if($finder == "1")
$result = mysql_query("SELECT $rowname FROM $tablename");
else
$result = mysql_query("SELECT $rowname FROM $tablename WHERE
`$finder`='$findervalue'") or die (mysql_error());
$row = mysql_fetch_assoc($result);
return $row[$rowname];
}
?>
Where is says
$result = mysql_query("SELECT $rowname FROM $tablename WHERE
`$finder`='$findervalue'") or die (mysql_error());
The mysql_fetch_assoc() function returns an associative array, row by row. To access the rows you would do:
while ($row = mysql_fetch_assoc($result)) {
echo $row[$rowname];
}
According situations when You install purchased script, or You are comfort with mysql_* functions, I'll not say to switch to mysqli or PDO. PHP 7 will force to switch to modern drivers to everyone (:
but for Your question, run this script and tell me result:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$sitename = "http://devilgaming.phy.sx/csgopie.xyz/";
$link = mysql_connect("localhost", "devilgam_csgopie", "ssb12boycek1") or die(mysql_error());
mysql_select_db('devilgam_csgopie', $link) or die(mysql_error());
mysql_query("SET NAMES utf8");
function fetchinfo($rowname, $tablename, $finder, $findervalue) {
$q = "SELECT $rowname FROM $tablename";
if($finder != 1) {
$q .= " WHERE `$finder`='$findervalue'";
}
$result = mysql_query($q) or die (mysql_error());
$row = mysql_fetch_assoc($result);
return $row[$rowname];
}
?>
Also go to administration panel of hosting create or modify user: devilgam_csgopie to have same username and password.
Because:
Access denied for user 'devilgam_csgopie'#'localhost' to database

php script select where statement xcode

I'm developing an app with xcode and trying to do a select with some statement, but the query is not working. For example, I created a button in app that contains letter 'A', when I click it calls a script php (JSON) that does a select and return in a listview some result that starts with 'A'.
In xcode I try to define an api like this: #define GET_API #"http://website.16mb.com/field/testsearch.php" or this #"http://website.16mb.com/field/testsearch.php?letter=%#,letter"
In my testsearch.php I wrote:
<?php
$host = "mysql.xxxx.com";
$user = "userxxx";
$pass = "xxxxx";
$database = "udatabase";
mysql_connect($host,$user,$pass) or die("Error connection");
mysql_select_db($database) or die("Error to select database");
$letter = $_GET["letter"];
$query = "SELECT * FROM tablename where letter = '$letter' ORDER BY letter DESC";
$result = mysql_query($query) or die ("Error ");
mysql_close();
$lines = array();
while ($r = mysql_fetch_assoc($result)){
$lines[] = $r;
}
echo json_encode($linhas);
?>
Someone could tell me how I send a data from xcode to script with select and statement?
Thanks for your help.
$letter = $_GET['letter'];
$query = "SELECT * FROM `tablename` where `letter` = '".$letter."' ORDER BY letter DESC";
$result = mysql_query($query) or die (mysql_error());
$lines = array();
while ($r = mysql_fetch_array($result))
{
$lines[] = $r;
}
echo json_encode($lines);
?>
try this

Adding title in PHP

I would like to add, let's say, some kind of a title. My PHP returns something like this:
[{"Grad":"Beograd","Predmet":"matematika"},{"Grad":"Novi_Sad","Predmet":"matematika"},{"Grad":"Beograd","Predmet":"matematika"}]
And I would like to get something like this
{"lista"[{"Grad":"Beograd","Predmet":"matematika"},{"Grad":"Novi_Sad","Predmet":"matematika"},{"Grad":"Beograd","Predmet":"matematika"}]}
This is my PHP
$con = mysqli_connect($host, $user, $pwd, $db);
if(mysqli_connect_errno($con)) {
die("Failed to connect to MySQL: " . mysqli_connect_error());
}
$sql = "SELECT Grad, Predmet FROM lista";
$result = mysqli_query($con, $sql);
$rows = array();
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$rows[] = $row;
}
mysqli_close($con);
echo json_encode($rows);
$rows = (object) array('lista' => $rows);
echo json_encode($rows);
Something like this: $rows['lista'][] = $row ?

Categories