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'
Related
I am trying to store a column named imgPath from a table named images in my database to an array in PHP. Then I want to print it out so I can see that it worked. You can see what the table looks like below:
Below is the code I am using to not succeed lol:
$db = mysqli_connect('localhost', 'userdatabase', 'PASSData', 'sftdatabase');
$result = mysqli_query($db,"SELECT imgPath FROM images");
$result_array = array();
while($row = mysql_fetch_assoc($result))
{
$result_array[] = $row['imgPath'];
}
print_r($result_array);
I have tried using print_r($result); right before creating the array and I get:
How can I get the path? (example: img/art/10/download.jpg)
Simple mistake,
Change,
while($row = mysql_fetch_assoc($result))
To,
while($row = mysqli_fetch_assoc($result))
Notice the i for mysqli is missing in the first.
$db = mysqli_connect('localhost', 'userdatabase', 'PASSData', 'sftdatabase');
$result = mysqli_query($db,"SELECT imgPath FROM images");
$result_array = [];
while($row = mysqli_fetch_assoc($result))
{
$result_array[] = $row['imgPath'];
}
print_r($result_array);
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)) {
in my last project I used foreach loop to assign to every mysqli result a variable, like $r->mydata, but I formatted my pc accidentally, so I lost my core file and I can't remember how exactly I did that. I remember that I did something like this
$result = $db->query("SELECT * FROM data");
if($result->num_rows){
while ($row = $result->fetch_object()) {
foreach ($row as $r){
$row[] = $r;
}
}
}
And I can access the result from outside the while loop like this:
<?php echo $r->mydata ?>
Can anyone edit my code so it will work like before?
it would be easier to use
$rows=$result->fetch_all(MYSQLI_ASSOC);
rather than looping through all the rows and building an array.
Maybe you don't remember how you did it, but if you did it once you should know at least which approach you followed to solve this, let help you to understand the code first:
$result = $db->query("SELECT * FROM data");
if($result->num_rows>0){
//iterating only if the table is not empty
while ($row = $result->fetch_object()) {
//Here you are iterating each row of the database
foreach ($row as $r){
//Here you are iterating each column as $r
//and (trying) adding it again to the $row array
$row[] = $r;
}
}
}
Why would you like to access to the $row outside the loop, if what you want to do is print each row, you can do it inside the loop:
$result = $db->query("SELECT * FROM data");
if($result->num_rows>0){
while ($row = $result->fetch_object()) {
foreach ($row as $r){
echo $r.'<br>';
}
}
}
If you do something like what follows, what part of the 'printed' data do you need? You may have been 'specializing' the result set by eliminating the rows to only use a single row...
$result = $db->query("SELECT * FROM data");
$r = new stdClass();
// Only loop if there is a result.
if ($result)
{
$r->myData = []; // You aren't exactly clear on 'myData'. In this instance, I am setting the rows inside of it.
while ($row = $result->fetch_object())
{
$r->myData[] = $row;
}
$result->close(); // Free result set
}
print_r($r->myData);
$records = array();
$result = $db->query("SELECT * FROM data");
if($result->num_rows){
while ($row = $result->fetch_object()) {
$records[] = $row;
foreach($records as $r);
}
}
and now you can access the result from any place in the page,example echo inside html h1:
<h1>My name is: <?php echo $r->name ?></h1>
The problem I have is when I echo or print the following variables, the data I receive is that of the last business listed in my table only.
At present no matter the listing I click I get the same set of data for the last business returned.
As you can see in the below code I am passing the business_name from the clicked listing to be used in my query to find the relevant business profile information.
$business_name = mysql_real_escape_string($_GET['business_name']);
$query = "SELECT
business_id,
category,
years_recommended,
profile_size,
business_name,
established,
employees,
service,
strengths,
ideal_for,
reassurance
FROM
business_data
WHERE
business_name = '$business_name'
AND
profile_size = 'A'
OR
profile_size = 'B'
OR
profile_size = 'C'
OR
profile_size = 'D'
OR
profile_size = 'E'";
$result = mysql_query($query, $dbc)
or die (mysql_error($dbc));
while($row = mysql_fetch_array($result)) {
$business_id = $row["business_id"];
$profile_size = $row["profile_size"];
$category = $row["category"];
$years = $row["years_recommended"];
$established = $row["established"];
$employees = $row["employees"];
$service = $row["service"];
$strengths = $row["strengths"];
$ideal_for = $row["ideal_for"];
$reassurance = $row["reassurance"];
}
echo...
If you need more information please let me know.
Is there anything wrong with my code?
Many thanks in advance.
Your echo call is outside the fetch loop, so you'll only see the last result even though the others were returned.
while($row = mysql_fetch_array($result)) {
$business_id = $row["business_id"];
$profile_size = $row["profile_size"];
$category = $row["category"];
$years = $row["years_recommended"];
$established = $row["established"];
$employees = $row["employees"];
$service = $row["service"];
$strengths = $row["strengths"];
$ideal_for = $row["ideal_for"];
$reassurance = $row["reassurance"];
// Echo **inside** the loop
echo...
}
If you wish, you can store all the results in a large array, which can then be used anywhere subsequently in your script, as many times as needed:
// Array for all results
$results = array();
while($row = mysql_fetch_array($result)) {
// Append each row fetched onto the big array
$results[] = $row;
}
// Now use it as needed:
foreach ($results as $r) {
echo $r['profile_size'];
print_r($r);
}
your echo should be inside the loop
I am currently using a JSON encoded array to display the users in my database for an auto-suggest feature.
It looks something like this:
$sth = mysql_query("SELECT id, name FROM users");
$json = array();
while($row = mysql_fetch_assoc($sth)) {
$json['name'] = $row['name'];
$json['id'] = $row['id'];
$data[] = $json;
}
print json_encode($data);
This returns:
[{"id":"81","name":"John Doe"},{"id":"82","name":"Jane Doe"}]
My question is somewhat 2-fold:
First, how would I manually add an additional object to this output? For example, let's say I wanted to add: {"id":"444","name":"A New Name"}
Thus, it'd look like:
[{"id":"81","name":"John Doe"},{"id":"82","name":"Jane Doe"},{"id":"444","name":"A New Name"}]
Second, let's say I also wanted to add more objects to the array from a separate table as well, such as:
$sth = mysql_query("SELECT id, title FROM another_table");
$json = array();
while($row = mysql_fetch_assoc($sth)) {
$json['name'] = $row['title'];
$json['id'] = $row['id'];
$data[] = $json;
}
print json_encode($data);
This way I could have both tables populated in the JSON array, thus, showing up as additional options in my autosuggest.
Hopefully this makes sense, as I've tried hard to articulate what I am trying to accomplish.
Thanks!
Just keep pushing to the $data array.
$json = array();
while($row = mysql_fetch_assoc($sth)) {
$json['name'] = $row['name'];
$json['id'] = $row['id'];
$data[] = $json;
}
$custom = array('name'=>'foo', 'id' => 'bar');
$data[] = $custom;
Then at the very end, do your json_encode. Assuming you're not referring to merging it in the JS itself with multiple ajax calls.
And if you have separate scripts, combine them in one php page.
Try in this way:-
$temp = json_encode($json); //$json={"var1":"value1","var2":"value2"}
$temp=substr($temp,0,-1);
$temp.=',"variable":"'.$value.'"}';
You could edit the JSON (text), but it's much easier to modify the array before you encode it.
Or am I missing something?
I'm not an expert in any of these fields, but I'll try and see if I can help. Try one of these:
Option 1 (I don't know how unions work in mysql):
$sth = mysql_query("SELECT id, name FROM users union SELECT id, name FROM (SELECT id, title as name from another_table) as T2");
$json = array();
while($row = mysql_fetch_assoc($sth)) {
$json['name'] = $row['name'];
$json['id'] = $row['id'];
}
$json['name'] = 'A new name';
$json['id'] = '444';
$data[] = $json;
print json_encode($data);
I've never done PHP, so I'm making assumptions. I've also never used MySql, so there's more assumptions.
Option 2:
$sth = mysql_query("SELECT id, name FROM users");
$json = array();
while($row = mysql_fetch_assoc($sth)) {
$json['name'] = $row['name'];
$json['id'] = $row['id'];
}
$sth = mysql_query("SELECT id, title from another_table");
while($row = mysql_fetch_assoc($sth)) {
$json['name'] = $row['title'];
$json['id'] = $row['id'];
}
$json['name'] = 'A new name';
$json['id'] = '444';
$data[] = $json;
print json_encode($data);
Hope this helps.