Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

PHP+MYSQL!? How better to output a couple of records?

ajax mysql records

  • Please log in to reply
5 replies to this topic

#1 Stasonix

Stasonix

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 115 posts
  • Programming Language:C++, PHP, JavaScript, Delphi/Object Pascal, Pascal
  • Learning:C++, PHP, JavaScript, Delphi/Object Pascal

Posted 22 November 2011 - 03:03 PM

for example, I have query like this

$sql = "SELECT `name`,`surname`,`age` FROM `table` WHERE (`name`='".$name,"') AND (`age`=20)";
$query = mysql_query($sql);
$result = mysql_fetch_array($query);


so I need to echo only 2 records is name and age and to recieve it in JQuery(ajax-method), what I am gonna do next?
  • 0

#2 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 22 November 2011 - 08:28 PM

Iterate through the results echoing name and age.
  • 0

#3 codehutch

codehutch

    CC Newcomer

  • Member
  • PipPip
  • 13 posts

Posted 22 November 2011 - 08:50 PM

while ($result = mysql_fetch_array($query, MYSQL_ASSOC)) {
    echo $result["name"] . " - " . $result["age"];
}
I don't know what you mean about the "ajax thing" though. Maybe you can make it more clearer.
  • 0

#4 Calgon

Calgon

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 53 posts

Posted 23 November 2011 - 03:15 AM

Create a separate page which calculates everything using the code codehutch provided, and then use jquery's ajax functions to echo out the results dynamically, here's an example:

$.ajax({
  type: "GET",
  url: "page_query_calc_thing.php",
}).done(function( msg ) {
  $('#div-to-update').html(msg); 
});

  • 0

#5 SoN9ne

SoN9ne

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 118 posts
  • Programming Language:Java, PHP, JavaScript, Bash
  • Learning:C, Objective-C

Posted 23 November 2011 - 08:46 AM

If you are using it VIA AJAX, do not echo the data. echo the data as JSON. This is a standard for handling data VIA AJAX and it allows you more control over the data.
  • 0
"Life would be so much easier if we only had the source code."

#6 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 23 November 2011 - 03:18 PM

Agree with SoN9ne. You just read out your data and fill an array with it, and then, there are json_encode() function, just echo it's result to the ajax call.

---------- Post added at 12:18 AM ---------- Previous post was at 12:17 AM ----------

unnecesary post as I missed the SoN9nes link to the function, still I agree with him.
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.






Also tagged with one or more of these keywords: ajax, mysql, records

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download