output xml array parsing php repeated six times [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a php file in this format:
<?php
$username = "root";
$password= "";
$db = "kamus1";
mysql_connect("localhost",$username,$password) or die("koneksi ke MySQL gagal");
mysql_select_db($db) or die ("koneksi ke dataBase gagal");
$doc = new DomDocument('1.0');
$root = $doc->createElement('movies');
$root = $doc->appendChild($root);
$arr = array();
//$query=mysql_query("select id,istilah,definisi from kamus_jaringan");
$query=mysql_query("select * from kamus_jaringan");
while($get_data = mysql_fetch_array($query))
{
foreach($get_data as $fieldname=>$fieldvalue)
{
$item = $doc->createElement('movie');
$item = $root->appendChild($item);
$item ->setAttribute("id",$get_data["id"]);
$item ->setAttribute("istilah",$get_data["istilah"]);
$item ->setAttribute("definisi",$get_data["definisi"]);
}
}
echo $doc->saveXML();
$doc->save("movies.xml");
?>
when this code is executed on the browser, the output of each of the data will be repeated up to six times. for example id, will repeat six times. I want the output to each of the data, only one time. please help

First, don't use mysql_* functions, they are deprecated. Second, you have two loops and you only need one, change your code to this:
while($get_data = mysql_fetch_array($query))
{
$item = $doc->createElement('movie');
$item = $root->appendChild($item);
$item ->setAttribute("id",$get_data["id"]);
$item ->setAttribute("istilah",$get_data["istilah"]);
$item ->setAttribute("definisi",$get_data["definisi"]);
}

Related

PHP: How to check if no rows are returned [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
With the following PHP code, how do i check if there's no row retuned so i can echo some message?
PHP:
<?php
require_once 'db_conx.php';
$Result = mysql_query("SELECT * FROM ads WHERE pid = '2'")
or die (mysql_error());
while($row = mysql_fetch_array($Result)){
echo '<span class="classPid" style="display:none">'.$row['pid'].'</span>';
}
?>
Thanks
There are a number of ways to do this. There is a specific function mysql_num_rows() which will return the number of rows returned.
mysql_num_rows($Result);
This will return 0 if there are no rows affected or returned.
You could also create some conditions using mysql_fetch_array. mysql_fetch_array will return FALSE if there are no rows.
On a separate note it would be a good idea to update your connection
and functions to mysqli as mysql is no depreciated.
See docs on mysql_num_rows().
http://www.php.net/mysql_num_rows
Something like this
<?php
require_once 'db_conx.php';
$Result = mysql_query("SELECT * FROM ads WHERE pid = '2'")
or die (mysql_error());
$i=0;
while($row = mysql_fetch_array($Result)){
echo "<span class='classPid' style='display:none'>".$row['pid']."</span>";
$i++;
}
if($i==0){
echo "No rows found";
}
?>

what is the use of $rows=array() [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to know what to do with $rows=array(). why it is used and how i could use it further. would like to get some suggestions and answers
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbdb = "yumyum";
$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die("connection error");
mysql_select_db($dbdb) or die("database selection error");
$id = $_POST['id'];
$query1=mysql_query("SELECT Quantity,id FROM `yumyum`.`food` where `food`.`id` LIKE $id");
$rows = array();
while($r = mysql_fetch_assoc($query1)) {
$output = $r['Quantity'];
echo $output;
$query2=mysql_query("UPDATE food SET Quantity = Quantity - 1 where `food`.`id` LIKE ".$r["id"]);
}
?>
In your script it does nothing but initialize the variable $row with an array type. It would probably be intended to be used in your while loop ($r):
...
$query1=mysql_query("SELECT Quantity,id FROM `yumyum`.`food` where `food`.`id` LIKE $id");
$r = array();
while($r = mysql_fetch_assoc($query1)) {
...
See PHP basics:
It is not necessary to initialize variables in PHP however it is a very good practice.
You should also stop using mysql_ functions. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which.

Can this code work or not [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can the code below work or not to retrieve data from database and display.. I know that the sql4 can't work in a mysql database can the php code below make it work or not.
<?php
include 'connect.php';
$sql2 = "SELECT * from pm Order by mid";
while ($row3 = mysql_fetch_array($sql2)) {
$sql4 = mysql_query("SELECT * FROM reply ORDER BY mid =".$row3["$mid"]." ");
$row4 = mysql_fetch_array($sql4);
echo"<trid='".$mid."'><td><img src='a/$name' width='150' height='100' /></td> <td>".$row['mid']."</td></tr><tr><td>".$row['reply']."</td></tr>";
}
?>
esp. this mysql or mysql and php combined. Basically is the code below improper. I think it is.
<?php
$sql4 = mysql_query("SELECT * FROM reply ORDER BY mid =".$row3["$mid"]." ");
?>
Put the following at the top of your PHP file and try it out yourself:
<?php
error_reporting(-1);
ini_set("display_errors", true);
// Your code goes here
?>
(I think the code is self-explaining)
Try...
$sql2 = "SELECT * from pm Order by mid";
$result = mysql_query($sql2); <- MISSING THIS LINE
while ($row3 = mysql_fetch_array($result))
{
// OTHER CODE HERE
}

Reading data from mysql table [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In my database table, I have a lot of questions ,each having a category . Say, there are 100 questions and some number of categories which I don't know . I want to know the number and the names of the categories.
Please tell the way to do it in php.
You can try something like this
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
$result = mysqli_query($con,"SELECT category_name FROM your_table GROUP BY category_name");
$count = mysqli_num_rows($result); // no of categories
while ($row=mysqli_fetch_row($result)) {
echo $row[0]; // printing category name
}
How about...
<?php
$mysqli = mysqli_connect( /* info */) or die ("MySQL error");
$result = mysqli_query("SELECT COUNT(DISTINCT `category`)");
echo $result;
?>
That is assuming categories are all in a single column.

Convert Json Array to Strings [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have my result in json array format, I want to change my array results to json string format. How do I do this. Please help.. i dont have idea how i go about doing this :(. new to json
My result as json array is like this
[["Date.UTC(2011,09,03)","1"],["Date.UTC(2011,09,06)","53"],["Date.UTC(2011,09,07)","178"],["Date.UTC(2011,09,08)","305"],["Date.UTC(2011,09,09)","152"],["Date.UTC(2011,09,11)","20"],["Date.UTC(2011,09,12)","239"],["Date.UTC(2011,09,13)","25"],["Date.UTC(2011,09,14)","316"],["Date.UTC(2011,09,15)","169"],["Date.UTC(2011,09,16)","20"],["Date.UTC(2011,09,19)","126"]
My code
$mysql_connect = mysql_connect($db_host, $db_user, $db_pword, $db_name);
$query = "Select DATE_FORMAT(`timestamp`,'%Y,%m,%d') as date, Count(*) as frequency from mytable group by date_format(`timestamp`,'%Y,%m,%d')";
if (!$mysql_connect) die("unable to connect to database: " . mysql_error());
#mysql_select_db($db_name) or die( "Unable to select database");
$result = mysql_query($query);
$response = array();
if($result === FALSE)
{
die(mysql_error()); // TODO: better error handling
}
while($row=mysql_fetch_array($result))
{
$date = "Date.UTC(".$row ['date'].")";
$frequency =$row['frequency'];
$response[] = array ($date,$frequency);
}
JSONArray jArray;
String s = jArray.toString();
mysql_close($mysql_connect);
json_encode:
<?php
$json_string = json_encode($your_array);
Did you even bother to Google this?

Categories