dongzhuifeng1843 2015-11-05 03:44
浏览 17

MySQL准备语句优化

I'm currently still learning MySQL And prepared statements and i was wondering if this code i've written is the most efficient way.

The process...

  1. There's a query that checks a table called 'websites' for a field called 'websites_pagecount'

The same query then increments the field by 1. (I haven't done this is the database using auto-increment because there will be more than one row with the same number)

  1. Theres a query that then runs and grabs the new number

  2. 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)

However it all seems a bit 'cumbersome'.. this is the code ive got.

        $mysql['stmt'] = $mysql['conn']->prepare("UPDATE websites SET website_pagecount=website_pagecount+? WHERE website_domain=?");
        $mysql['stmt']->bind_param("is", $a = 1, $website['website_id']);
        $result = $mysql['stmt']->execute();
        $mysql['stmt']->close();


        $mysql['stmt'] = $mysql['conn']->prepare("SELECT website_pagecount FROM websites WHERE website_domain = ?");
        $mysql['stmt']->bind_param("s", $website['website_id']);
        $result = $mysql['stmt']->execute();
        //GET ROWS
        $result = $mysql['stmt']->get_result();
        $pagecount = $result->fetch_assoc();
        $mysql['stmt']->close();


        $mysql['stmt'] = $mysql['conn']->prepare("INSERT INTO pages (page_id,page_parent, page_title, page_description,page_keywords, website_id, page_layout) VALUES (?,?,?,?,?,?,?)");
        $mysql['stmt']->bind_param("issssss",$pagecount['website_pagecount'],$item['parentpage'],$item['pagetitle'],$item['pagedescription'],$item['pagekeywords'],$website['website_id'],$item['pagelayout'] );
        $result = $mysql['stmt']->execute();
        $mysql['stmt']->close();

Any suggestions or help would be greatly appreciated.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等
    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
    • ¥20 测距传感器数据手册i2c