SetToLoki Posted November 1, 2006 Share Posted November 1, 2006 I am using a set of functions to generate the simplest of content pulled from a database, something I have done a million times before (all be it that was a few years ago)and I am having problems with a mysql queryfirst here are the functions I am using[code=php:0]<? require_once('mysql_connect.php'); $DB = new DB; function LoadCatagories ($columns = 4) { $result = mysql_query('SELECT * FROM catagories ORDER BY "Name"') or die(mysql_error()); $noresults = mysql_num_rows($result); $rows = $noresults/$columns; $rows = ceil($rows); $i = 0; print ('<table width="100%" border="0" id="content_list">'); while ($row = mysql_fetch_array($result)) { // Now we need to add rows and coloums to the database if($i == 0) print ('<tr>'); // open a new coloum $i++; //while ($i <= $rows)// print results as many times as there is rows printf('<td><a href="%s?cat=%s">%s</a></td>', $_SERVER['PHP_SELF'],$row['catID'], $row['Name']); //$i++; if($i == $columns) { print ('</tr>'); // close the coloum $i = 0; } } print ('</table>'); } function LoadSubCatagories ($sub, $columns = 4) { // Load all catagories under with the catID $sub. $query = printf("SELECT * FROM subcatagories WHERE 'catID' = %s", $sub); $res = mysql_query($query or die(mysql_error())); while (mysql_fetch_array($res)) { //do this } }?>[/code]as you can see when LoadCatagories() is called it prints to the page a list of links from a database, each link name has a link id and when clicking loads the page this time with the extention ?catID=xx this in turn makes the page use the function LoadSubCatagories() which in all respect is the exact same function with minor changes like pulling all subcatagories with the catID taken from stage one.only using the exact same sql query I get the error[code]SELECT * FROM subcatagories WHERE 'catID' = 5Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/nettshop/public_html/logistics/include/catsearch.php on line 38[/code]no matter what my query is or if I use mysql_fetch_array() or not I get a mysql error.Any help? Link to comment https://forums.phpfreaks.com/topic/25824-solved-strange-mysql-error-added-diffrence-between-sprintf-and-printf/ Share on other sites More sharing options...
Barand Posted November 1, 2006 Share Posted November 1, 2006 use$query = [size=12pt][color=red]s[/color][/size]printf("SELECT * FROM subcatagories WHERE 'catID' = %s", $sub); Link to comment https://forums.phpfreaks.com/topic/25824-solved-strange-mysql-error-added-diffrence-between-sprintf-and-printf/#findComment-118104 Share on other sites More sharing options...
SetToLoki Posted November 2, 2006 Author Share Posted November 2, 2006 I made the change also tried [code=php:0]$query = "SELECT * FROM subcatagories WHERE catID = 5"; $res = mysql_query($query or die(mysql_error())); $noresults = mysql_num_rows($res); echo $noresults;[/code]but get the error.[code]Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource [/code]no matter waht my query is I can get a result from database, but when I try the exact same query direct to database in Zend I get the right outcome?Perhaps it is something to do with my DB class [code=php:0]<? class DB { function DB() { $this->host='localhost'; $this->db='db_name'; $this->user='db_user'; $this->pass='db_pass'; $this->link=mysql_connect( $this->host, $this->user, $this->pass); mysql_select_db($this->db); } } ?> [/code]or the way I am calling the functions [code=php:0] if (!isset($_GET['cat'])) { LoadCatagories(); } else if(isset($_GET['cat']) && !isset($_GET['reg'])) { LoadSubCatagories($_GET['cat']); }[/code]I am pulling my hair out here, I can't see a problem, but keep getting one :( Link to comment https://forums.phpfreaks.com/topic/25824-solved-strange-mysql-error-added-diffrence-between-sprintf-and-printf/#findComment-118192 Share on other sites More sharing options...
SetToLoki Posted November 2, 2006 Author Share Posted November 2, 2006 a closer look solved my problemchanging [code=php:0]$res = mysql_query($query or die(mysql_error()));[/code]to [code=php:0]$res = mysql_query($query[b])[/b] or die(mysql_error());[/code]fixed my problem as always with php human error gets in the way and causes hair losson a completly diffrent notecould you please explain the diffrence between sprintf() and printf()thank you for your reply Link to comment https://forums.phpfreaks.com/topic/25824-solved-strange-mysql-error-added-diffrence-between-sprintf-and-printf/#findComment-118199 Share on other sites More sharing options...
Barand Posted November 2, 2006 Share Posted November 2, 2006 printf() printssprintf() returns formatted string Link to comment https://forums.phpfreaks.com/topic/25824-solved-strange-mysql-error-added-diffrence-between-sprintf-and-printf/#findComment-118282 Share on other sites More sharing options...
SetToLoki Posted November 2, 2006 Author Share Posted November 2, 2006 [quote author=Barand link=topic=113482.msg461555#msg461555 date=1162453219]printf() printssprintf() returns formatted string[/quote]Suppose that makes enough sesnse Link to comment https://forums.phpfreaks.com/topic/25824-solved-strange-mysql-error-added-diffrence-between-sprintf-and-printf/#findComment-118287 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.