Solved

How do I get multiple rows of data with PHP using Mysql, send it JSON encoded with ajax, and then read it with javascript/jquery

Posted on 2009-05-14
6
1,520 Views
Last Modified: 2013-11-08
I am pretty new to combining codes so I need some help. I want to grab the comments for a particular article and put them on the webpage. I can do this when I just find 1 row, but I don't know how to grab multiple rows of data from a database using PHP, JSON encode it, then grab it with ajax, decode it and loop through the rows with javascript on the main page.

When I do the PHP code by itself it shows "{"ID":"1","comment":"this is the first comment"}{"ID":"2","comment":"this is the second comment"}{"ID":"3","comment":"this is the third comment"}" I don't know if this is the proper format for the javascript to read it.

I also, never get to the alerts in the javascript.

Thanks for your help.
/*****************here is javascript that I've been using on index.html*******************************/
 

$.getJSON('../advice/php/get_comments.php', ' ', process_comments);

function process_comments(data) {

var new_comment = data.comment;

var ID = data.ID;

alert('new_comment'+ new_comment);

alert('ID'+ID);

}
 
 
 
 

/*****************here is get_comments.php*******************************/
 

<?PHP

include("../../php/mysql.php"); //gets the mysql info.
 
 
 

$get_comments_sql = "SELECT ID, comment

					FROM comments

					WHERE ID = articles_ID = 47

					GROUP BY ID";

		if ($result = mysqli_query($mysqli, $get_comments_sql)) {

			while ($row = mysqli_fetch_array($result)) {

				$array_data = array('ID'=>$row[0], 'comment'=>$row[1]);

				echo json_encode($array_data);

			}

		}

?>

Open in new window

0
Comment
Question by:Sucao
  • 3
  • 2
6 Comments
 
LVL 12

Expert Comment

by:alien109
ID: 24388683
you only want to echo the json string once. So rather than doing it for every row, try building an array of rows, and then encoding that.

0
 
LVL 7

Expert Comment

by:Jonah11
ID: 24388779
ya, basically if you switch lines 28 and 29 in your code it should work
0
 
LVL 7

Expert Comment

by:Jonah11
ID: 24388787
oh and also process comments needs to account for the fact that data is an array...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Sucao
ID: 24388896
Ok, so I've learned that I want to only echo the JSON string once. I switched lines 28 and 29. How do I send out an array of rows. Could someone write an example using this code? Also, what do I need to change in my javascript to access that JSON encoded array of rows?
0
 
LVL 7

Accepted Solution

by:
Jonah11 earned 500 total points
ID: 24388956
sorry, you had to make a couple more changes actually.  try the code below.
/*****************here is javascript that I've been using on index.html*******************************/

 

$.getJSON('../advice/php/get_comments.php', ' ', process_comments);

function process_comments(data) {

	var new_comment = data[0].comment;

	var ID = data[0].ID;

	alert('new_comment'+ new_comment);

	alert('ID'+ID);

}

 

 

 

 

/*****************here is get_comments.php*******************************/

 

<?PHP

include("../../php/mysql.php"); //gets the mysql info.

 

 

 

$get_comments_sql = "SELECT ID, comment

                                        FROM comments

                                        WHERE ID = articles_ID = 47

                                        GROUP BY ID";

								$array_data = array();

                if ($result = mysqli_query($mysqli, $get_comments_sql)) {

                        while ($row = mysqli_fetch_array($result)) {

                                $array_data[] = array('ID'=>$row[0], 'comment'=>$row[1]);

                        }

												echo json_encode($array_data);

                }

?>

Open in new window

0
 

Author Comment

by:Sucao
ID: 24389901
Jonah11 that's perfectly what I needed. I have one final part to figure out though, how do I loop through the data with javascript? right now I am only getting alerts about the first row of data.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now