OAFC_Rob Posted June 14, 2011 Share Posted June 14, 2011 Hey, i'm being really really really stoopid I have written a database connection class, got some SQL I want to implement got it all working fine and dandy. But I've realised that I should do a check first to say if mysql_num_rows > 0 then do the while loop else come back with some default data. I first did the following; <?php require_once ("commonResources/includes/headerArea.php"); #FUNCTIONS IS A TEMP FILE UNTIL OO PHP COMES INTO PLAY require_once ("commonResources/includes/navigationArea.php"); ?> <div class="mainContent"><!-- OPEN DIV FOR MAIN CONTENT --> <div class="paraBlockLeft"><!-- OPEN PARA-BLOCK-LEFT (MAIN BODY OF CONTENT) --> <div class="leftTop"> </div> <div class="leftMiddle"> <?php $id = $database->escapeValue($_GET["portfolio_id"]); $sql = " SELECT clientName, clientType, jobDescription, clientURL, shortURL, imgPathway, imgSmPathway, imgAlt FROM tbl_portfolio LEFT JOIN tbl_portfolio_img ON (tbl_portfolio.tbl_portfolio_id = tbl_portfolio_img.tblPorfolioId) WHERE tbl_portfolio_id = $id "; $portfolio = $database->sqlQuery($sql); if (numRows($portfolio) > 0) { while ($portfolioResult = $database->fetchArray($portfolio) ) { echo "<h4>" .strtoupper($portfolioResult['clientName'])."</h4>"; ?> <a href="<?php echo $portfolioResult['clientURL']; ?>"><h4><?php echo $portfolioResult['shortURL']; ?></h4></a> <h4>Client Type:</h4> <p> <?php echo $portfolioResult['clientType']; ?> </p> <h4>Job Description:</h4> <?php echo $portfolioResult['jobDescription']; ?> <a href="<?php echo $_SERVER["DOCUMENT_ROOT"]."/".$portfolioResult['imgPathway']; ?>" rel="lightbox[roadtrip]" ><img src="<?php echo $_SERVER["DOCUMENT_ROOT"]."/".$portfolioResult['imgPathway']; ?>" alt="<?php echo $portfolioResult['imgAlt']; ?>" class="thumbnail2" /></a> <?php } } else { echo "nope"; } ?> </div> <div class="leftBottom"> </div> </div><!-- CLOSE PARA-BLOCK-LEFT (MAIN BODY OF CONTENT) --> <?php require_once ("commonResources/includes/topRightArea.php"); require_once ("commonResources/includes/bottomRightArea.php"); require_once ("commonResources/includes/footerArea.php"); ?> This works no problem, then I realised I did this earlier in my dbconnection class; public function numRows($result) { return mysql_num_row($result); } I thought it was a a matter of passing in the results in this case $portfolio through to numRows via the object like this; if($database->numRows($portfolio) > 0) What am I doing wrong? It's 7pm in the UK now and my brain is fried after a long day of coding, can anyone give me a little helping hand. Link to comment https://forums.phpfreaks.com/topic/239358-tired-mind-cant-figure-out-simple-solution/ Share on other sites More sharing options...
redixx Posted June 14, 2011 Share Posted June 14, 2011 Well, in the code you posted you have if (numRows($portfolio) > 0) Shouldn't it be if ($database->numRows($portfolio) > 0) EDIT: By the way, you should be using mysqli which is already OO, so you don't need the abstraction layer. Link to comment https://forums.phpfreaks.com/topic/239358-tired-mind-cant-figure-out-simple-solution/#findComment-1229611 Share on other sites More sharing options...
AbraCadaver Posted June 14, 2011 Share Posted June 14, 2011 if (numRows($portfolio) > 0) // is not the same as if($database->numRows($portfolio) > 0) EDIT: Beat me too it! Link to comment https://forums.phpfreaks.com/topic/239358-tired-mind-cant-figure-out-simple-solution/#findComment-1229612 Share on other sites More sharing options...
OAFC_Rob Posted June 14, 2011 Author Share Posted June 14, 2011 my bad I didn't do edit undo enough to get back to the working one. I had this coidng working; if (mysql_num_rows($portfolio) > 0) And if you look at the last coding snippet I had it as; if($database->numRows($portfolio) > 0) { However, $database->numRows($portfolio) > 0 is causing the following error: Fatal error: Call to undefined function mysql_num_row() in C:\xampp\htdocs\innovationation1\commonResources\dbConnection\dbConnection.php on line 93 This would suggest the error is in the dbconnection class, line 93 is below. return mysql_num_row($result); So no idea whats wrong Link to comment https://forums.phpfreaks.com/topic/239358-tired-mind-cant-figure-out-simple-solution/#findComment-1229640 Share on other sites More sharing options...
redixx Posted June 14, 2011 Share Posted June 14, 2011 mysql_num_rowS return mysql_num_rows($result); Link to comment https://forums.phpfreaks.com/topic/239358-tired-mind-cant-figure-out-simple-solution/#findComment-1229642 Share on other sites More sharing options...
OAFC_Rob Posted June 14, 2011 Author Share Posted June 14, 2011 changed that, still not working Warning: mysql_num_rows() expects parameter 1 to be resource, string given in C:\xampp\htdocs\innovationation1\commonResources\dbConnection\dbConnection.php on line 93 nope Link to comment https://forums.phpfreaks.com/topic/239358-tired-mind-cant-figure-out-simple-solution/#findComment-1229647 Share on other sites More sharing options...
OAFC_Rob Posted June 14, 2011 Author Share Posted June 14, 2011 fixed! was passing the wrong arguement through Link to comment https://forums.phpfreaks.com/topic/239358-tired-mind-cant-figure-out-simple-solution/#findComment-1229652 Share on other sites More sharing options...
OAFC_Rob Posted June 14, 2011 Author Share Posted June 14, 2011 Okay, fixed that problem but it's not working like I had planned :'( If in the URL I take away the $_GET data I get the following error Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\innovationation1\commonResources\dbConnection\dbConnection.php on line 93 Which is basically saying I'm not passing through a return mysql_query that because I have killed off the where clause, what is the best way to prevent this issue? dbconnection: public function sqlQuery($sql) { #NEED TO ADD EXTRA FAIL SAFES IF TABLE DOESN'T EXIST $result = mysql_query($sql, $this->dbConnection) OR trigger_error(mysql_error($sql),E_USER_ERROR); if(mysql_num_rows($result)>0) { $this->confirmResult($result); return $result; } else { return false; } } private function confirmResult($result) { if (!$result) { $output = "Database query failed: " . mysql_error(); //$output .= "Last SQL query: " . $this->last_query; die( $output ); } } Page side: <?php require_once ("commonResources/includes/headerArea.php"); #FUNCTIONS IS A TEMP FILE UNTIL OO PHP COMES INTO PLAY require_once ("commonResources/includes/navigationArea.php"); ?> <div class="mainContent"><!-- OPEN DIV FOR MAIN CONTENT --> <div class="paraBlockLeft"><!-- OPEN PARA-BLOCK-LEFT (MAIN BODY OF CONTENT) --> <div class="leftTop"> </div> <div class="leftMiddle"> <?php $id = $database->escapeValue($_GET["portfolio_id"]); $sql = " SELECT clientName, clientType, jobDescription, clientURL, shortURL, imgPathway, imgSmPathway, imgAlt FROM tbl_portfolio LEFT JOIN tbl_portfolio_img ON (tbl_portfolio.tbl_portfolio_id = tbl_portfolio_img.tblPorfolioId) WHERE tbl_portfolio_id = $id "; $portfolio = $database->sqlQuery($sql); if($database->numRows($portfolio) > 0) { while ($portfolioResult = $database->fetchArray($portfolio) ) { echo "<h4>" .strtoupper($portfolioResult['clientName'])."</h4>"; ?> <a href="<?php echo $portfolioResult['clientURL']; ?>"><h4><?php echo $portfolioResult['shortURL']; ?></h4></a> <h4>Client Type:</h4> <p> <?php echo $portfolioResult['clientType']; ?> </p> <h4>Job Description:</h4> <?php echo $portfolioResult['jobDescription']; ?> <a href="<?php echo $_SERVER["DOCUMENT_ROOT"]."/".$portfolioResult['imgPathway']; ?>" rel="lightbox[roadtrip]" ><img src="<?php echo $_SERVER["DOCUMENT_ROOT"]."/".$portfolioResult['imgPathway']; ?>" alt="<?php echo $portfolioResult['imgAlt']; ?>" class="thumbnail2" /></a> <?php } } else { echo "nope"; } ?> </div> <div class="leftBottom"> </div> </div><!-- CLOSE PARA-BLOCK-LEFT (MAIN BODY OF CONTENT) --> <?php require_once ("commonResources/includes/topRightArea.php"); require_once ("commonResources/includes/bottomRightArea.php"); require_once ("commonResources/includes/footerArea.php"); ?> Link to comment https://forums.phpfreaks.com/topic/239358-tired-mind-cant-figure-out-simple-solution/#findComment-1229665 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.