just a small query..
.
.
i just want to know how to enter fetched data into an array..!!!

Dani AI

Generated

Short answer: query the database, then fetch rows into an array — either all at once or by looping. As said, that general flow is correct. pointed to the old mysql_* helpers; those are obsolete. 's ASP.NET example shows the same pattern (fill a dataset, then move values into an array). For modern PHP use PDO or mysqli, and prefer prepared statements.

Example using PDO: connect, run the query, then fetch all rows as an array of associative arrays.

$pdo = new PDO('mysql:host=localhost;dbname=your_db;charset=utf8mb4', 'user', 'pass', [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);

$stmt = $pdo->query('SELECT id, name FROM your_table');
$rows = $stmt->fetchAll(); // array of associative arrays

If you only need a single column as a simple indexed array, use PDO::FETCH_COLUMN or build the array while fetching:

$names = $pdo->query('SELECT name FROM your_table')->fetchAll(PDO::FETCH_COLUMN, 0);

// or with a prepared statement and loop
$stmt = $pdo->prepare('SELECT name FROM your_table WHERE status = ?');
$stmt->execute(['active']);
$names = [];
while ($r = $stmt->fetch()) {
    $names[] = $r['name'];
}

Notes and troubleshooting: 1) mysql_* functions are removed in current PHP; migrate to PDO or mysqli. 2) Use prepared statements to avoid SQL injection (PDO prepared statements). 3) If you see zero rows but expect results, use print_r($rows) or var_export($rows, true) to inspect the returned structure. 4) For convenience, mysqli has fetch_all (mysqli_result::fetch_all). 5) If your table name starts with a digit or has special chars, quote it with backticks.

Recommended Answers

All 5 Replies

do this:
1. Fetch the data from data base.
2. Have a loop to fill the data in array.

Post the code what ever you have done so far, for better help.

$checkExists = "select * from 0_ledger";  

$checkExists_result= mysql_query($checkExists)
   OR die('QUERY ERROR:<br />' .$checkExists. '<br />' .mysql_error());
   
$NumberOfResults=mysql_num_rows($checkExists_result);

i have fetch data into an array..!! Thanx

NO ans. uptil now.? :O i dont think it is that dificult..!!

hi sam... here i am fetching the data from the db and assing that data to a string array...
code is ...

protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=WINKC185045-MAB\SQLEXPRESS;Initial Catalog=Records;Integrated Security=True");
        SqlDataAdapter da = new SqlDataAdapter("select name from data", con);
        DataSet ds = new DataSet();
        da.Fill(ds, "data");

        string[] s;
        s = new string[9];
               

        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            s[i] = ds.Tables[0].Rows[i][0].ToString();
            Response.Write(s[i]);
            
        }

              
    }

Hope it helps u.....let me know if u face any difficulties...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.