Jump to content

Recommended Posts

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.

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

 

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");
?>                

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.