简体   繁体   English

有没有更好的方法来处理此PHP脚本? -超时问题

[英]Is there a better way to handle this PHP script? - Time out issue

I put together a simple script that pulls the product name, category name and product id from two tables. 我整理了一个简单的脚本,该脚本从两个表中提取了产品名称,类别名称和产品ID。 Then I take that data and use it to create a page title that's better than what I currently have for SEO purposes. 然后,我获取这些数据并使用它来创建一个页面标题,该页面标题要比我目前用于SEO的标题要好。 For some reason I didn't think it would take as long as it's taking to run. 由于某种原因,我认为运行不会花很长时间。 There are 7k products. 有7k件商品。

My hosting company does allow the creation of a custom php.ini so I was able to override the 30 second time limit and changed it to 6000. But still the script times out. 我的托管公司确实允许创建自定义php.ini,因此我能够覆盖30秒的时间限制并将其更改为6000。但是脚本仍然超时。 So I thought my script my suck. 所以我以为我的剧本很烂。 :) :)

Below is the script. 下面是脚本。 Is there a better way I could write this so it doesn't time out? 有没有更好的方法可以写这个,这样它不会超时? Or is what I'm trying to do just going to take some time and I need to write the script to do one category at a time? 还是我想做的只是花一些时间,而我需要编写脚本一次完成一个类别?

<?php
// Make a MySQL Connection
mysql_connect("localhost", "myusername", "mypassword") or die(mysql_error());
mysql_select_db("mydatabase") or die(mysql_error());

$result = mysql_query("SELECT isc_products.prodcode, isc_products.prodname, isc_categories.catname FROM isc_products, isc_categories WHERE isc_products.prodcatids = isc_categories.categoryid") 
or die(mysql_error());


while($row = mysql_fetch_array($result)){
$pname = mysql_real_escape_string($row['prodname']);
$catname = mysql_real_escape_string($row['catname']);
$sitename = Sitename;
$prodcode = $row['prodcode'];
$result2 = mysql_query("UPDATE isc_products SET prodpagetitle = '$pname - $catname - $sitename' WHERE prodcode = '$prodcode'") 
or die(mysql_error());
}

?>

indexes http://www.threewestcreative.com/indexes.jpg 索引http://www.threewestcreative.com/indexes.jpg

Thanks, your help is appreciated. 谢谢,感谢您的帮助。 :) :)

Thanks SO much everyone! 非常感谢大家! I really appreciate the quick responses. 我非常感谢您的快速回复。 I can't believe I overlooked something so simple as running a direct query against the database (without php). 我不敢相信我忽略了一些简单的事情,例如对数据库运行直接查询(没有php)。 Geez... Thanks again! 真是的...再次感谢!

Just run 赶紧跑

UPDATE isc_products
INNER JOIN isc_categories ON isc_products.prodcatids = isc_categories.categoryid
SET isc_products.prodpagetitle=CONCAT(isc_products.prodname,' - ',isc_categories.catname,' - $sitename');

If it times out, your DB is fishy (missing indices?) 如果超时,则您的数据库混乱(缺少索引?)

You can just use one query to do what you want. 您可以只使用一个查询来执行所需的操作。

UPDATE ssc_products, isc_categories 
SET psc_products.prodpagetitle = CONCAT_WS(' - ',  isc_products.prodname, isc_categories.catname, $sitename)
WHERE isc_products.prodcatids = isc_categories.categoryid;

Can you show your tables(s) structure? 您可以显示表格结构吗? Its important when dealing with that many products, indexing is key. 在处理那么多产品时,索引是很重要的。 Also (while) loop is bad here, it will effect performance. 同样,(while)循环在这里不好,它将影响性能。 Like the guys mention above 1 query should do the trick. 就像上面提到的家伙一样1查询应该可以解决问题。

I'm not sure why you're doing this in PHP, given that you could achieve this with a single UPDATE. 我不确定为什么要用PHP进行此操作,因为您可以通过一个UPDATE来实现。 Perhaps you've left out bits that do things like identify which records have already been changed? 也许您遗漏了一些位来进行诸如标识哪些记录已被更改的事情?

So I'll assume you really do want to do this in PHP, and that you just want to run this script once in order to update the prodpagetitle field table-wide. 因此,我假设您确实要在PHP中执行此操作,并且只想运行一次此脚本以更新整个表的prodpagetitle字段。

One option would be to split this into separate scripts. 一种选择是将其拆分为单独的脚本。 Have a main script that does the SELECT, then skips through the UPDATEs by calling a second script, with the data to use in variables in the GET. 有一个执行SELECT的主脚本,然后通过调用第二个脚本来跳过UPDATE,并使用要在GET中的变量中使用的数据。 For example: 例如:

<?php
// Make a MySQL Connection
mysql_connect("localhost", "myusername", "mypassword") or die(mysql_error());
mysql_select_db("mydatabase") or die(mysql_error());

$result = mysql_query("SELECT isc_products.prodcode, isc_products.prodname, isc_categories.catname FROM isc_products, isc_categories WHERE isc_products.prodcatids = isc_categories.categoryid") 
or die(mysql_error());

while ($row = mysql_fetch_array($result)) {
  $pname = mysql_real_escape_string($row['prodname']);
  $catname = mysql_real_escape_string($row['catname']);
  $sitename = Sitename;
  $title=sprintf("%s - %s - %s", $pname, $catname, $sitename);
  $url=sprintf("http://example.com/update.php?pcode=%s&title=%s", $row['prodcode'], $title);
  $junk=file_get_contents($url)
}

?>

and: 和:

<?php

// This is update.php, called by the script above.
mysql_connect("localhost", "myusername", "mypassword") or die(mysql_error());
mysql_select_db("mydatabase") or die(mysql_error());

$qfmt="UPDATE isc_products SET prodpagetitle = '%s' WHERE prodcode='%s'";
mysql_query(sprintf($qfmt, $_GET['pcode'], urldecode($_GET['title']));

?>

Node that this should be considered EXAMPLE code. 应该认为这是示例代码的节点。 I haven't tested it and don't plan to. 我还没有测试,也没有计划。 You probably want to include some facility to mark your already-changed fields, so that you can continue on from whence you left off if the new script also times out (which it probably will). 您可能想要包括一些功能来标记您已经更改的字段,以便在新脚本也超时的情况下(可能会)从上次中断的地方继续。 This script contains vulnerabilities, and should only be run in a secure environment or with significant modification to make it safe. 该脚本包含漏洞,应仅在安全环境中运行或进行重大修改以使其安全。 </fineprint> </ fineprint>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM