mrherman Posted April 26, 2010 Share Posted April 26, 2010 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.) Quote Link to comment https://forums.phpfreaks.com/topic/199731-php-function-with-a-mysql-select-statement-will-not-work/ Share on other sites More sharing options...
PFMaBiSmAd Posted April 26, 2010 Share Posted April 26, 2010 Where in your main code are you calling sql_browse_and_select() and assigning the returned value to the $qResult variable that you are using in the two lines of code you did post? Quote Link to comment https://forums.phpfreaks.com/topic/199731-php-function-with-a-mysql-select-statement-will-not-work/#findComment-1048314 Share on other sites More sharing options...
mrherman Posted April 26, 2010 Author Share Posted April 26, 2010 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 ) ; Quote Link to comment https://forums.phpfreaks.com/topic/199731-php-function-with-a-mysql-select-statement-will-not-work/#findComment-1048318 Share on other sites More sharing options...
mrMarcus Posted April 26, 2010 Share Posted April 26, 2010 you mentioned several times you get an error, but you haven't given the error. what is the error you are receiving? Quote Link to comment https://forums.phpfreaks.com/topic/199731-php-function-with-a-mysql-select-statement-will-not-work/#findComment-1048319 Share on other sites More sharing options...
mrherman Posted April 26, 2010 Author Share Posted April 26, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/199731-php-function-with-a-mysql-select-statement-will-not-work/#findComment-1048321 Share on other sites More sharing options...
mrMarcus Posted April 26, 2010 Share Posted April 26, 2010 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 ) ) ; ?> Quote Link to comment https://forums.phpfreaks.com/topic/199731-php-function-with-a-mysql-select-statement-will-not-work/#findComment-1048325 Share on other sites More sharing options...
PFMaBiSmAd Posted April 26, 2010 Share Posted April 26, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/199731-php-function-with-a-mysql-select-statement-will-not-work/#findComment-1048326 Share on other sites More sharing options...
mrherman Posted April 26, 2010 Author Share Posted April 26, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/199731-php-function-with-a-mysql-select-statement-will-not-work/#findComment-1048330 Share on other sites More sharing options...
PFMaBiSmAd Posted April 26, 2010 Share Posted April 26, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/199731-php-function-with-a-mysql-select-statement-will-not-work/#findComment-1048331 Share on other sites More sharing options...
mrMarcus Posted April 26, 2010 Share Posted April 26, 2010 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 ) ; ?> Quote Link to comment https://forums.phpfreaks.com/topic/199731-php-function-with-a-mysql-select-statement-will-not-work/#findComment-1048333 Share on other sites More sharing options...
mrherman Posted April 26, 2010 Author Share Posted April 26, 2010 Did I catch that?....Ah....As Sancho said, "I come from my vineyards. I know nothing." Thanks again for your help! Quote Link to comment https://forums.phpfreaks.com/topic/199731-php-function-with-a-mysql-select-statement-will-not-work/#findComment-1048336 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.