0

In my php code I take data from the database and I put to a specific values ​​of the Array:

$from_db=mysql_query(" SELECT * FROM ... ");

while ($row = mysql_fetch_array($from_db)) {
$number_id[]=$row['number_id'];
...
}

And now I would like add this line (for other purposes) before comand "while":

$from_db = mysql_fetch_array($from_db);

How can I change this line: while ($row = mysql_fetch_array($from_db)) { to process an array instead of a raw data from database?

6
  • your question is way unclear. in both cases you have arrays both contains raw data from database. Can you tell what you actually want, and why? Without using some terms you don't quite understand. Thanks. Commented Feb 23, 2012 at 10:05
  • I tried put $from_db to cache, but I got an error, so I needed convert mysql_query to an Array, put in into cache and after that process. Commented Feb 23, 2012 at 10:19
  • WHAT you want to put into cache? what for? Do you really need any cache? May be you need to learn very basic syntax first? Commented Feb 23, 2012 at 10:21
  • I needed put result from mysql_query(" SELECT * FROM ... ") into cache. But mysql_query result need to be converted to an Array before will be saved on a disk for future use. Commented Feb 23, 2012 at 10:24
  • why do you want this cache at all? do you have your query slow or what? most queries require no caching, mysql can cache query results better than you can do. Do you have any certain reason to cache query results? Commented Feb 23, 2012 at 10:28

4 Answers 4

3

I suggest you use PDO class instead. mysql_* functions are not recommended anymore and will be deprecated in the (far) future.

An example is something like:

//Connect to database and whatnot
//...
$PDOquery=$PDO->prepare('SELECT * FROM users WHERE id=?;');
$PDOquery->execute(array(1));
$resultArray=$PDOquery->fetchAll(PDO::FETCH_ASSOC); //Results stored as associative array in ths array.
Sign up to request clarification or add additional context in comments.

Comments

0

Something like this is unfortunately necessary:

$from_db=mysql_query(" SELECT * FROM ... ");

$from_db = array();

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

foreach($from_db as $row) {
  $number_id[]=$row['number_id'];
}

Comments

0

You would better use PDO or MySQLi.

For example, PDOStatement::fetchAll:

<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();

/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:\n");
$result = $sth->fetchAll();
print_r($result);

Comments

-1
mysql_fetch_assoc()
mysql_fetch_object() 

http://php.net/manual/en/function.mysql-fetch-assoc.php

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.