title: MySQL Prepared Statement optimization
body: <p>I'm currently still learning MySQL And prepared statements and i was wondering if this code i've written is the most efficient way. </p>
  
  <p>The process...</p>
  
  <ol>
  <li>There's a query that checks a table called 'websites' for a field called 'websites_pagecount'</li>
  </ol>
  
  <p>The same query then increments the field by 1. <em>(I haven't done this is the database using auto-increment because there will be more than one row with the same number)</em></p>
  
  <ol start="2">
  <li><p>Theres a query that then runs and grabs the new number</p></li>
  <li><p>Then another query runs and creates a row in a table called 'pages' and sets the new 'pagecount' number in to the 'page_id' field (Again, i haven't used auto-increment on this as there will be more than one row with the same number)</p></li>
  </ol>
  
  <p>However it all seems a bit 'cumbersome'.. this is the code ive got. </p>
  
  <pre><code>        $mysql['stmt'] = $mysql['conn']-&gt;prepare("UPDATE websites SET website_pagecount=website_pagecount+? WHERE website_domain=?");
          $mysql['stmt']-&gt;bind_param("is", $a = 1, $website['website_id']);
          $result = $mysql['stmt']-&gt;execute();
          $mysql['stmt']-&gt;close();
  
  
          $mysql['stmt'] = $mysql['conn']-&gt;prepare("SELECT website_pagecount FROM websites WHERE website_domain = ?");
          $mysql['stmt']-&gt;bind_param("s", $website['website_id']);
          $result = $mysql['stmt']-&gt;execute();
          //GET ROWS
          $result = $mysql['stmt']-&gt;get_result();
          $pagecount = $result-&gt;fetch_assoc();
          $mysql['stmt']-&gt;close();
  
  
          $mysql['stmt'] = $mysql['conn']-&gt;prepare("INSERT INTO pages (page_id,page_parent, page_title, page_description,page_keywords, website_id, page_layout) VALUES (?,?,?,?,?,?,?)");
          $mysql['stmt']-&gt;bind_param("issssss",$pagecount['website_pagecount'],$item['parentpage'],$item['pagetitle'],$item['pagedescription'],$item['pagekeywords'],$website['website_id'],$item['pagelayout'] );
          $result = $mysql['stmt']-&gt;execute();
          $mysql['stmt']-&gt;close();
  </code></pre>
  
  <p>Any suggestions or help would be greatly appreciated. </p>