Jump to content

trouble with mysql data in arrays


xjermx

Recommended Posts

I've tried re-reading and re-re-reading the available info on arrays, but I'm just not understanding what I'm doing wrong, or how I can effectively troubleshoot it.

 

My question is this:

How do I do a mysql query inside a function, and then let the main page, or another function on the main page use the array that the query produces?

 

Here is some code:

 

<?php

myfunction1();

  $rowCheck = mysql_num_rows($gNames);
  print $rowCheck;
while ($row = mysql_fetch_assoc($gNames)){

echo is_array($row) ? 'Array<br>' : 'not an Array<br>';
print_r($row);

  foreach ($row as $col=>$val){
    //  if ($col == 'name') {
    print " $val, \n";
     // }
} // end foreach

    foreach ($row as$val){
          print " $val, \n";
        } // end foreach
}// end while


function myfunction1() {
    global $gNames;

    $db = mysql_connect("blahblahblah", "myname", "mypass") or die ("Error connecting to database.");
mysql_select_db("databasename", $db) or die ("Couldn't select the database.");
$result = mysql_query("SELECT * FROM characters WHERE whoschar='1'",$db);   // modified to only show for user 1
$gNames = $result;
$rowCheck = mysql_num_rows($result);

if($rowCheck > 0){
    echo 'You do have characters, they should be listed here..<br>';

    while ($row = mysql_fetch_assoc($result)){

	echo is_array($row) ? 'Array<br>' : 'not an Array<br>';  //debugging
	print_r($row);  //debugging
	echo "<br>";

  foreach ($row as $col=>$val){
		  if ($col == 'name') {
		print " $val, \n";
		  }
	} // end foreach

}// end while
print "<br><br>";

}
  else {

  echo 'You have no characters.  You should create one.';

  }


  } /////////////////////////////// end function

?>

 

Basically, the query goes just fine inside the function, it displays its data, it works like a dream.  But I cannot seem to be able to pass it off to the main part of the page.  the rowCheck in the main part returns "3", indicating that it has data in it, but I can't touch/view it.

 

Is there a trick that I don't know, or something basic that I'm overlooking/not understanding?

Link to comment
Share on other sites

when reading this the only thing i though of was returning the value from the function.

 

<?php

$returnVal = myfunction1(); //collect data here

  $rowCheck = mysql_num_rows($returnVal);
  print $rowCheck;
while ($row = mysql_fetch_assoc($returnVal)){

echo is_array($row) ? 'Array<br>' : 'not an Array<br>';
print_r($row);

  foreach ($row as $col=>$val){
    //  if ($col == 'name') {
    print " $val, \n";
     // }
   } // end foreach

    foreach ($row as$val){
          print " $val, \n";
        } // end foreach
   }// end while


function myfunction1() {
    global $gNames;

    $db = mysql_connect("blahblahblah", "myname", "mypass") or die ("Error connecting to database.");
mysql_select_db("databasename", $db) or die ("Couldn't select the database.");
$result = mysql_query("SELECT * FROM characters WHERE whoschar='1'",$db);   // modified to only show for user 1
$gNames = $result;
$rowCheck = mysql_num_rows($result);

if($rowCheck > 0){
    echo 'You do have characters, they should be listed here..<br>';

    while ($row = mysql_fetch_assoc($result)){

      echo is_array($row) ? 'Array<br>' : 'not an Array<br>';  //debugging
      print_r($row);  //debugging
      echo "<br>";

     foreach ($row as $col=>$val){
           if ($col == 'name') {
         print " $val, \n";
           }
      } // end foreach

   }// end while
print "<br><br>";

}
  else {

  echo 'You have no characters.  You should create one.';

  }

  return $gNames; // return value here
  } /////////////////////////////// end function

?>

 

Good luck and have fun

Link to comment
Share on other sites

When I read this, it looked like you were having an ordering issue... this part of the code:

 

myfunction1();

  $rowCheck = mysql_num_rows($gNames);
  print $rowCheck;
while ($row = mysql_fetch_assoc($gNames)){

echo is_array($row) ? 'Array<br>' : 'not an Array<br>';
print_r($row);

  foreach ($row as $col=>$val){
    //  if ($col == 'name') {
    print " $val, \n";
     // }
   } // end foreach

    foreach ($row as$val){
          print " $val, \n";
        } // end foreach
   }// end while

 

Has to go after this part:

function myfunction1() {
    global $gNames;

    $db = mysql_connect("blahblahblah", "myname", "mypass") or die ("Error connecting to database.");
mysql_select_db("databasename", $db) or die ("Couldn't select the database.");
$result = mysql_query("SELECT * FROM characters WHERE whoschar='1'",$db);   // modified to only show for user 1
$gNames = $result;
$rowCheck = mysql_num_rows($result);

if($rowCheck > 0){
    echo 'You do have characters, they should be listed here..<br>';

    while ($row = mysql_fetch_assoc($result)){

      echo is_array($row) ? 'Array<br>' : 'not an Array<br>';  //debugging
      print_r($row);  //debugging
      echo "<br>";

     foreach ($row as $col=>$val){
           if ($col == 'name') {
         print " $val, \n";
           }
      } // end foreach

   }// end while
print "<br><br>";

}
  else {

  echo 'You have no characters.  You should create one.';

  }


  } /////////////////////////////// end function

 

Since you haven't defined the function yet. As far as passing the array into a new function, you have to include it in the function($var) and make sure anything you want out of the function is set as a global variable.

Link to comment
Share on other sites

use the array that the query produces?

 

By returning the array from the function - i.e return $row;

 

Functions are designed to accept (optional) parameters in the function call, perform some operation and in most cases return the results so that you can either assign the results to a program variable or use the returned results as a parameter in another function call -

 

$program_variable = your_function('parameter1','parameter2');

 

or

 

$program_variable = your_function1(your_function2('parameter1','parameter2'));

Link to comment
Share on other sites

Thanks for the responses.

 

I tried what BillyBob and PFMaBiSmAd suggested.  I stuck

 

  return $gNames; // return value here

  at the tail end of my function

 

 

Here is part is my code again, with a few more printed debug lines:

 

<?php

print "<h5>Debug line 1.  Top of program.</h5>";

$returnVal = myfunction1(); //collect data here

  $rowCheck = mysql_num_rows($returnVal);
  print $rowCheck;
while ($row = mysql_fetch_assoc($returnVal)){
    
    print "<h5>Debug line 2.  Start of WHILE</h5>";


echo is_array($row) ? 'Array<br>' : 'not an Array<br>';
print_r($row);

  foreach ($row as $col=>$val){
    //  if ($col == 'name') {
    print " $val, \n";
     // }
} // end foreach

    foreach ($row as$val){
          print " $val, \n";
        } // end foreach
}// end while


function myfunction1() {
    global $gNames;

  $db = mysql_connect("blahblahblah", "myuser", "mypass") or die ("Error connecting to database.");
mysql_select_db("databasename", $db) or die ("Couldn't select the database.");
$result = mysql_query("SELECT * FROM characters WHERE whoschar='1'",$db);   // modified to only show for user 1
$gNames = $result;
$rowCheck = mysql_num_rows($result);

    print "<h5>Debug line 3.  inside MYFUNCTION1</h5>";

if($rowCheck > 0){
    echo 'You do have characters, they should be listed here..<br>';

    while ($row = mysql_fetch_assoc($result)){

            print "<h5>Debug line 4.  inside MYFUNCTION1, inside WHILE</h5>";

	echo is_array($row) ? 'Array<br>' : 'not an Array<br>';  //debugging
	print_r($row);  //debugging
	echo "<br>";

  foreach ($row as $col=>$val){
		  if ($col == 'name') {
		print " $val, \n";
		  }
	} // end foreach

}// end while
print "<br><br>";

}
  else {

  echo 'You have no characters.  You should create one.';

  }

  return $gNames; // return value here
  } /////////////////////////////// end function

?>

 

Note that I'm not seeing "Debug Line 2", which means that it is not getting into the while statement there at the top.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.