Jump to content

*solved* - strange mysql error *added* - diffrence between sprintf and printf?


SetToLoki

Recommended Posts

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 query
first 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' = 5
Warning: 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?
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 :(
a closer look solved my problem

changing

[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 loss

on a completly diffrent note
could you please explain
the diffrence between sprintf() and printf()

thank you for your reply

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.