0

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

2
Contributors
13
Replies
17
Views
7 Years
Discussion Span
Last Post by cane23
0
create a function wherein the return value is the $dbarray.

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

Edited by phpbeginners: n/a

0

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

0
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; 
 }
0

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

0

i have tried that already and the result was the same

0
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

0

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");
0
$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

Edited by cane23: n/a

0
$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

0

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);

Edited by phpbeginners: n/a

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.