Jump to content

Recommended Posts

Hi

 

Simply, what is wrong with this function?  I know the SELECT statement works, but when the $qResult is returned, I get an error.

 

function sql_browse_and_select ( $code )
{
   include_once ( "db_login.php" ) ;

   $sql = "SELECT   last_name   AS last_name
                  , first_name  AS first_name
                  , grade       AS gr
                  , ethnic      AS eth
                  , sex         AS sex
                  , student_id  AS id_num
                  , reason      AS reason
                  , mon_init    AS since
             FROM t_tims0809
            WHERE tag <> '' AND
                  tag IS NOT NULL AND
                  schcode = {$code} " ;

   $qResult = mysql_query ( $sql ) ;

   if ( mysql_num_rows ( $qResult ) == 0 )
   {
      exit ( trigger_error ( "SQL", E_USER_ERROR ) ) ;
   }
   return $qResult ;
}

 

As soon as the code below gets hold of the $qResult, an error is thown.  Why?

 

$fields_num   = mysql_num_fields ( $qResult ) ;
$nRowCount    = mysql_num_rows ( $qResult ) ;

 

Thanks

(In my own defense, I am stupid.)

Thanks.  Here is my code up until I call the function (near the bottom).  The function itself is in user_defined.php.

 

<?php include ( "user_defined.php" ) ; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" 
      type="text/css" 
      href="css/style.css" 
      media="screen" 
      title="mysite (screen)" />
<link rel="stylesheet" 
      type="text/css" 
      href="css/print.css" 
      media="print" />
<title>Data Site</title>
</head>

<body>   
<div id="wrap"> 

<?php 
// "Think of $_SESSION as a global array that can be 
// used when session_start is called."
session_start () ;
$code  = "320" . $_SESSION [ 'school_code' ] ;
?>

<!--================================================== HEADER -->
<div id="header">
<h1>Data Browse - Edit - Export</h1>
<p><strong>"This is some text.  Change as necessary."</strong><br /></p>
</div> <!--*header*--> 


<img id    = "frontphoto" 
    src    = "images/fullsize/front_760x100.jpg" 
    width  = "760" 
    height = "100" 
    alt    = "" 
/>

<!--========================================== CENTER CONTENT -->

<div id="browsetitle"> 
<h2>School: <?php echo $code .  
    " - Current students receiving services" ; ?> </h2>

<!-- Start HTML form -->
<form name = 'form1'
    method = 'post'
    action = ''> 

<?php
// CALL FUNCTION HERE
sql_browse_and_select ( $code ) ;

$fields_num   = mysql_num_fields ( $qResult ) ;
$nRowCount    = mysql_num_rows ( $qResult ) ;

mysql_num_fields(): supplied argument is not a valid MySQL result resource in E:\Wamp\www\Andreas01\browse.php on line 85

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in E:\Wamp\www\Andreas01\browse.php on line 8

 

NOTE: If I place the two pieces of code within the function, they work fine.  But when the $qResult is returned to the regular program flow, the above errors are thrown.

 

Thanks!

 

 

you are not using the function in the way you think you are.

 

you call the function, but are then assuming $qResult has respective scope, which it does not.

 

try this:

 

<?php
//get rid of function call;

$fields_num = mysql_num_fields ( sql_browse_and_select ( $code ) ) ;
$nRowCount = mysql_num_rows ( sql_browse_and_select ( $code ) ) ;
?>

User defined functions work exactly the same as php's built-in functions. The function call is essentially replaced by the returned value.

 

For the same reason that you are assigning the value that mysql_num_fields() returns to your $fields_num variable, you must assign (or use directly in your code) the value that your sql_browse_and_select() function returns.

 

$qResult = sql_browse_and_select($code);

 

The $qResult variable that you define inside the function only exists inside of the function. Otherwise coding would be a nightmare because you would need to keep track of every variable inside every built-in and user defined function to avoid conflict.

Yes, that works perfectly, mrMarcus!!  Thanks!

 

I was just going to ask the question that PFMaBiSmAd answered, so many thanks (!) there as well.

 

PHP is an evasive language -- at least for me.  Just when I think I absolutely know something, it disappears from under the thimble.  Ah, well, back to the keyboard.

 

Thanks again!

You do realize that the code that mrMarcus posted is extremely inefficient as it will call your function twice, thereby executing the query twice. Your web host will not be pleased with the unnecessary load that places on the mysql server.

yes, PFMaBiSmAd is correct.

 

while what i posted will work, it is inefficient.

 

$qResult = sql_browse_and_select($code);

 

is the way to go:

 

<?php
$qResult = sql_browse_and_select( $code );

$fields_num = mysql_num_fields ( $qResult ) ;
$nRowCount = mysql_num_rows ( $qResult ) ;
?>

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.