Results 1 to 3 of 3

Thread: Optimising this PHP/MySQLi code?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Arrow Optimising this PHP/MySQLi code?

    Hi all,

    I have some code to open a table and display it's contents:

    Code:
    /*DISPLAY ALL FOODS*/
    $query="SELECT foodid, foodname, foodbrand, foodsize FROM food";
    $stmt = $mysqli->prepare($query);
    $stmt->execute(); 
    $stmt->bind_result($foodid, $foodname, $foodbrand, $foodsize);
    $stmt->store_result();
    if ($stmt->num_rows<1) {
    echo '<p>No food items exist yet</p>';
    } else {
    /*LIST ALL FOODS*/
    echo '<table class="item">
    	<tr><th>foodid</th><th>size</th><th>foodname</th><th>foodbrand</th><th></th><th></th></tr>';
    while ($stmt->fetch()) {
    $query1="SELECT brandname FROM brand WHERE brandid=?";
    $stmt1 = $mysqli->prepare($query1);
    $stmt1->bind_param('i', $foodbrand); 
    $stmt1->execute(); 
    $stmt1->bind_result($brandname1);
    $stmt1->fetch();
    $stmt1->close();
    
    	echo '<tr><td>'.$foodid.'</td><td>'.$foodsize.'g</td><td>'.$foodname.'</td><td>'.$brandname1.'</td><td><a href="'.$PHP_SELF.'?edit='.$foodid.'">edit</a></td><td><a href="'.$PHP_SELF.'?remove='.$foodid.'">remove</a></td></tr>';
    }
    echo '</table>';
    }
    $stmt->close();
    I have a page like this for each table in my database.

    I'm trying to be smart now and have just one page where I can select a table from a dropdown list and show that.

    The bit I'm getting stuck with is how to bind the fields and display each one. Is there a way of doing what I want??

    Thanks for your help

  2. #2
    Addicted Member
    Join Date
    Feb 2006
    Location
    Hyderabad, India
    Posts
    233

    Re: Optimising this PHP/MySQLi code?

    One thing you can do for sure is having a JOIN statement to get the brand name along with the foodid etc.
    Code:
    $query = "SELECT f.foodid, f.foodname, f.foodbrand, f.foodsite, b.brandname 
    			FROM food f
    			INNER JOIN brand b ON (b.brand_id = f.foodbrand)
    		";
    If you different column names and column count for different tables, you will need lot of if statements to do what you want.

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Optimising this PHP/MySQLi code?

    Another thing you can do is tidy it up. You should not be using echo to produce HTML and the query and any other logic should take place at the top o the script prior to any HTML.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width