Jump to content

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

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Stasonix

Stasonix

    Learning Programmer

  • Members
  • PipPipPip
  • 82 posts
  • Programming Language:C++, PHP, JavaScript, Delphi/Object Pascal, Pascal
  • Learning:C++, PHP, JavaScript, Delphi/Object Pascal
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?

#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
  • Location:New York, NY
Iterate through the results echoing name and age.

#3
codehutch

codehutch

    Newbie

  • Members
  • PipPip
  • 13 posts
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.

#4
Calgon

Calgon

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
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); 

});



#5
SoN9ne

SoN9ne

    Programmer

  • Members
  • PipPipPipPip
  • 129 posts
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.
"Life would be so much easier if we only had the source code."

#6
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
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.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users