I have a strange question. I need to send some code to a client without having access to the server to test my code. In addition, it's using postgreSQL which I've never used, and I've not done PHP for a while!
In order to save some time, I'd really appreciate if someone could tell me if this code will do what I want?
example feed
<?
$sql = "SELECT * FROM V_SIDE_MENU_E";
include 'db.inc.php';
?>
db.inc.php
$connectString = 'host=localhost dbname=myDatabase user=foo password=bar';
$link = pg_connect($connectString);
if (!$link) {
echo "error";
} else {
$result = pg_query($link, $sql);
$rows = array();
while($r = pg_fetch_assoc($result)) {
$rows[] = $r;
}
print json_encode($rows);
}
I would change
$rows = array();
while($r = pg_fetch_assoc($result)) {
$rows[] = $r;
}
print json_encode($rows);
into
print json_encode(array_values(pg_fetch_all($result)));
But that's just a style thing -- your code should work.
Tested on your mysql ( it looks like it will work ). Your SELECT will work same in PostgreSQL like mySQL
Related
I am kinda new to SQL and I am having a problem. I am following the tutorial from Here to try to get data from my database into my android app.
I have it working, but it only gets one line. I know it should be possible to iterate through the database and echo out every line that matches the ID to JSON, but I do not know how. How would I do this?
My current code:
<?php
if($_SERVER['REQUEST_METHOD']=='GET'){
$id = $_GET['id'];
require_once('dbConnect.php');
$sql = "SELECT * FROM LATLON WHERE DeviceID='".$id."'";
$r = mysqli_query($con,$sql);
$res = mysqli_fetch_array($r);
$result = array();
array_push($result,array(
"INDEX"=>$res['INDEX'],
"DeviceID"=>$res['DeviceID'],
"LAT"=>$res['LAT'],
"LON"=>$res['LON']
)
);
echo json_encode(array("result"=>$result));
mysqli_close($con);
}
?>
EDIT
I edited my code to this, but I am getting nothing. Still not quite understanding what is happening. Thanks for all the help!
<?php
if($_SERVER['REQUEST_METHOD']=='GET'){
$id = $_GET['id'];
require_once('dbConnect.php');
$sql = "SELECT * FROM LATLON WHERE DeviceID='".$id."'";
$r = mysqli_query($con,$sql);
while($row = $result->mysqli_fetch_array($r, MYSQLI_ASSOC)){
$array[] = $row;
}
echo json_encode($array);
mysqli_close($con);
}
?>
You can iterate through mysqli_fetch_array();
while($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$array[] = $row;
}
echo json_encode($array);
When you use the OO interface, the method names don't begin with mysqli_, that's only used for procedural calls. You also have no variable named $result, the result is in $r.
So it should be:
while ($row = $r->fetch_array(MYSQLI_ASSOC)) {
or:
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
I am using android volley library to get the records from database on server. I send a query from my android app to this php script. When I write query myself in 4th line as follows $myquery = "Select firstname from Student;"; then all results are returned correctly but when I send a query directly, it doesn't returns records. I am sure that the issue is in php script, please help me where I am going wrong in this basic script?
function showStudent()
{
global $connect;
$myquery = $_POST["query"];
$result = mysqli_query($connect,$myquery);
$number_of_rows = mysqli_num_rows($result);
$temp_array = array();
if($number_of_rows>0)
{
while( $row = mysqli_fetch_assoc($result) )
{
$temp_array[] = $row;
}
}
header('Content-Type: application/json');
echo json_encode(array("dataArray"=>$temp_array));
mysqli_close($connect);
}
?>
Change to this
$myquery = $_REQUEST['query'];
Instead of this
$myquery = $_POST["query"];
I am building a site, and essentially what this PHP algorithm will do is look at a product (row in MySQL database) one at a time, and do a process accordingly.
I put a lot of research into this but couldn't find anything, any help would be greatly appreciated!
My Code (currently returning nothing for echo variables):
<?php
include_once 'dbconnect.php';
$query = "SELECT * FROM track";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$pro_code = mysql_result(mysql_fetch_array(mysql_query('SELECT product_code FROM track')));
$currency = mysql_fetch_array(mysql_query('SELECT currency FROM track'));
$cc = mysql_fetch_array(mysql_query('SELECT cctld FROM track'));
$initial_price = mysql_fetch_array(mysql_query('SELECT initial_price FROM track'));
$url = 'test';
}
echo $pro_code;
echo $currency;
echo $initial_price;
?>
First of all, try the advice about PDO and stuff from Jay Blanchard some day.
Secondly I've tried to answer your question anyway and I've tried to interpret your complete intention. I put comments in the code:
<?php
include_once 'dbconnect.php';
$query = "SELECT * FROM track";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
//You need to read the row variable as an array
$pro_code = $row['product_code'];
$currency = $row['currency'];
$cc = $row['cctld'];
$initial_price = $row['initial_price'];
//$url is not used.. I asume a test to get the external source ;-)
$url = 'test';
if ($url == $cc) {
//if you want to print every row, you must echo inside the while loop
echo $pro_code;
echo $currency;
echo $initial_price;
} elseif ($url == 'test') {
//do something else here
} else {
//do something for all the other cases here
}//end if
}//end while
?>
Why do you query the same table multiple times, your code should be written like this:
include_once 'dbconnect.php';
$query = "SELECT product_code, currency, cctld, initial_price FROM track";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo $row['product_code'];
echo $row['currency'];
echo $row['cctld'];
echo $row['initial_price'];
}
and please upgrade to mysqli or PDO
I am trying to fetch data and encode it to JSON. I have this very confusing trouble. The code I have put in getAnnotions() function, when I do not put it in function, the while loop (commented as //This loop) is reached. Whereas when I encapsulate the same code in getAnnotions() function, that while loop is not reached. What might be the problem?
Here is the code:
<?php
$city=$_GET["city"];
//$limit="1";
//$place=$_GET["place"];
getAnnotions("1");
function getAnnotions($limit)
{
$con = mysql_connect("localhost","hidden","*******");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("merrycod_tummy", $con);
$result = mysql_query("SELECT * FROM deal where city_names LIKE '%".$city."%'");
$rows = array();
while($row = mysql_fetch_array($result))
{
$rows[] = $row;
$result2 = mysql_query("SELECT locationLat,locationLong FROM place where city ='".$city."' AND name='".$row['place_name']."' LIMIT ".$limit);
while($row2 = mysql_fetch_array($result2))
{
$rows[] = $row2;
//This loop
}
}
echo json_encode($rows);
mysql_close($con);
}
?>
Because $city is defined in the global scope, and in PHP functions variables of another scope cannot be used directly. You can either pass it as a parameter (suggested), or use the global $city at the beginning of your function.
I suggest you to echo the sql in $result first,and then run the code you echo in phpmyadmin to look whether your sql is correct
I know I am making rookie errors here, and that I need to make a variable to pass forward - though I am not sure how to approach this one, and searching online tutes is not helping. If someone can steer me in the right direction I would be really grateful.
I would like to have the results echo into an array. That is; have 'xmlurl' row field from the 'xml' table display in the $rss array. I hope that makes sense.
// Get URLs from Database
$result = mysql_query("SELECT * FROM xml");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
printf("'%s',", $row["xmlurl"]);
}
mysql_free_result($result);
// Take the URLs (xmlurl) and place them in an array
$rss = array(
'http://www.xmlurl.com.au',
'http://www.xmlurl.com.au'
);
$rss = array();
while (...) {
$rss[] = $row['xmlurl'];
}
Try this:
$rss = array();
$result = mysql_query("SELECT * FROM xml");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$rss[] = $row["xmlurl"];
}
mysql_free_result($result);
// Get URLs from Database
$result = mysql_query("SELECT * FROM xml");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$rss[] = $row["xmlurl"]);
}
mysql_free_result($result);
print_r($rss); // Outputs the $rss array, listing all of the xmlurls'