i want to store the result of an mysql_fetch_array function in an array for future use. here is my code

  1. $sql= "SELECT parameters FROM rules WHERE type='prerequisite' ";
  2. //$result=mysql_query($sql);
  3. if ($result=mysql_query($sql))
  4. echo '<p>nice</p>';
  5. else
  6. echo 'good shit';
  7. //$row = mysql_fetch_row($result)
  8. //
  9. while ( $row = mysql_fetch_array($result))
  10. {
  11. $parm= array ($row[0]); }

how can i achieve this

Recommended Answers

All 13 Replies

create a function wherein the return value is the $dbarray.

		$dbarray = mysql_fetch_array($result);
      		return $dbarray;

can you elaborate on this a bit more please with heavy emphasis to the creation of the function

function getPrerequisites($prerequisite) {

//your sql statement here
  $q= 'SELECT parameters FROM rules WHERE type="$prerequisite"';

//your connection here
		$results = mysql_query($q, $connection); --> put your type of database connection here.

//fetch the results as $dbarray
		$dbarray = mysql_fetch_array($results);

//return an array of data
      		return $dbarray; 
 }

i have tried this function but however the array is some how still empty
i jusy am getting
"$dbarray" on the screen

to use that it should be like this

$arr=getPrerequisites($yourprerequisite);
print_r($arr);

i have tried that already and the result was the same

check your mysql connection.

or if you want post your code here and I will help you.

function getPrerequisites() {
 
//your sql statement here
  $q= 'SELECT parameters FROM rules WHERE type="$prerequisite"';
 
//your connection here
		$results = mysql_query($q);  
//fetch the results as $dbarray
		$dbarray = mysql_fetch_array($results);
 
//return an array of data
      	return $dbarray; 
 }
$all= getPrerequisites();

echo'$all';

this is the code and like i said i just see $all on the screen

hi, you don't have mysql connection.
you need host, username, password and the name of your database you are connecting to.

//add this inside the function and fill-up properly.
mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");
$con = mysql_connect("$hostname","$username","$password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("$database", $con);

function getPrerequisites() {
 
//your sql statement here
  $q= 'SELECT parameters FROM rules WHERE type="$prerequisite"';
 
//your connection here
		//$results = mysql_query($q,$con);  
		if ($result=mysql_query($q))
	echo '<p>nice</p>';
	else
	echo 'good shit';
//fetch the results as $dbarray
		$dbarray = mysql_fetch_array($result);
 
//return an array of data
      	return $dbarray; 
 }
$all= getPrerequisites();

echo'$all';

this is what i am running now and i am still getting the same thing

ps i am getting nice on the test i have for the query

$con = mysql_connect("$hostname","$username","$password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("$database", $con);


function getPrerequisites() {
 
//your sql statement here
  $q= 'SELECT parameters FROM rules WHERE type="$prerequisite"';
 
//your connection here
		//$results = mysql_query($q,$con);  
		if ($result=mysql_query($q))
	echo '<p>nice</p>';
	else
	echo 'good shit';
//fetch the results as $dbarray
		$dbarray = mysql_fetch_array($result);
 
//return an array of data
      	return $dbarray; 
 }
$all= getPrerequisites();

echo'$all';

this is what i am running at the moment however i am still getting the same thing also i am getting nice on the little test for the query that i have

put the database connection statement inside the function.

what is the value of the $prerequisite in your function you lack of function parameters and arguments.

function getPrerequisites(your changing prerequisite here) { //<-------------see this you need to put parameters/arguments

$con = mysql_connect("$hostname","$username","$password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("$database", $con);

 
//your sql statement here
  $q= 'SELECT parameters FROM rules WHERE type="$prerequisite"'; //<-------- what is the value of this $prerequisite?
 
//your connection here
		//$results = mysql_query($q,$con);  
		if ($result=mysql_query($q))
	echo '<p>nice</p>';
	else
	echo 'good shit';
//fetch the results as $dbarray
		$dbarray = mysql_fetch_array($result);
 
//return an array of data
      	return $dbarray; 
 }





$all= getPrerequisites(); //<--put parameters/arguments here

print_r($all);

hi, you don't have mysql connection.
you need host, username, password and the name of your database you are connecting to.

//add this inside the function and fill-up properly.
mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

thanks alot that did the trick

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.