0

i used this code

$dbhandle = mysql_connect("localhost","root","password")
or die("Unable to connect to MySQL");

$arr;

//select a database to work with
$selected = mysql_select_db("info",$dbhandle)
or die("Could not select examples");

$result = mysql_query("SELECT * FROM students");

while($row = mysql_fetch_array($result))
{
    $arr[]=$row;
}

Is it right way to store all data of a table into a 2D array. And how i use this array in Ajax as json to show data in a HTML file.?

5
  • 2
    yes, whats your actual problem? Commented Jul 22, 2014 at 13:24
  • the other thing you could do is, you can create associative array for all students (by some primary key), to efficiently access students by id. like $students[$row['stud_id']]=$row;... and later access individual student records like, $students[101] Commented Jul 22, 2014 at 13:25
  • I want to use this array in Ajax as json to print it in a HTML page. how i can? Commented Jul 22, 2014 at 13:27
  • Please rephrase you question to include your real question or it will get flagged. Commented Jul 22, 2014 at 13:31
  • @AniketSingh, check my answer. Hope it helps :) Commented Jul 22, 2014 at 13:39

2 Answers 2

1

As you mentioned in comments, that you want to do ajax and json.

You can do code like this (use mysql_fetch_assoc() instead of array):

//set content type: text/json
header("Content-Type: text/json");

$dbhandle = mysql_connect("localhost","root","password") or die("Unable to connect to MySQL");

//select a database to work with
$selected = mysql_select_db("info",$dbhandle) or die("Could not select examples");

$result = mysql_query("SELECT * FROM students");

if(mysql_num_rows($result)>0){
    $rows=array();
    while($row = mysql_fetch_assoc($result))
    {
        $rows[]=$row;
    }
    echo json_encode(array("success"=>$rows));
}else{
    echo json_encode(array("error"=>"No records found."));
}
exit;

and for ajax (assuming, jQuery.get() AJAX)

$.get("script.php", function(data){
    if(data.success){
        //process each row
        data.success.forEach(function(row){
            console.log(row); //access student row here, simply like row.student_name, row.city etc.
        });
    }else{
        alert(data.error);
    }
});

Scurity Note:

  1. mysql_* functions are deprecated and will be removed from support, so you should avoid using it.
  2. use mysqli_* or PDO for database queries.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Yes it working when i using data as a 2d array like this for(var i=0;i<data.length;i++) { $("p").append("ID"+data[i].s_id+"Name"+data[i].name); }
0

Use mysql_fetch_assoc instead of mysql_fetch_array...

while ($row = mysql_fetch_assoc($result)) {
    $arr[]=$row; 
}

Check mysql_query documentation for more information. However, its recommended that you use PDO or MySQLi drivers as this is deprecated as of PHP 5.5.0.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.