我有这个脚本:
<?php
// remplacer par vos valeurs de connexion
$host="*****";
$user="*****";
$pwd="******";
$base="*****";
$mysql_link = mysql_connect($host,$user,$pwd);
mysql_select_db($base);
$dbsite="arfooo"; // prefixe de votre table sans "_" par exemple : "arfooo"
echo "TRUNCATE TABLE `".$dbsite."_categoryparents`;<br>\n";
$sql = "SELECT * FROM ".$dbsite."_categories";
$result=mysql_query($sql);
while ($row=mysql_fetch_object($result)) {
$depth=0;
echo "INSERT INTO `".$dbsite."_categoryparents` (`parentId`, `childId`, `depth`) VALUES ($row->categoryId,$row->categoryId,$depth);<br>\n";
$parentId = $row->parentCategoryId;
while ($parentId<>0){
$sql2 = "SELECT * FROM ".$dbsite."_categories WHERE categoryId = $parentId";
$result2=mysql_query($sql2);
while ($row2=mysql_fetch_object($result2)) {
$depth=$depth+1;
echo "INSERT INTO `".$dbsite."_categoryparents` (`parentId`, `childId`, `depth`) VALUES ($row2->categoryId,$row->categoryId,$depth);<br>\n";
$parentId = $row2->parentCategoryId;
}
}
$depth=$depth+1;
echo "INSERT INTO `".$dbsite."_categoryparents` (`parentId`, `childId`, `depth`) VALUES (0,$row->categoryId,$depth);<br>\n";
}
?>
我有以下错误:
TRUNCATE TABLE `arfooo_categoryparents`;
PHP Error Message
Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/******/public_html/genereParentChildDepth.php on line 14
答案 0 :(得分:1)
您在第一个选择语句
上遇到mysql错误"SELECT * FROM ".$dbsite."_categories"
表名必须格式错误。验证arfooo_categories
是否存在。
如果是,请试试这个:
$l = mysql_connect($host,$user,$pwd);
mysql_select_db($base);
$sql = "SELECT * FROM arfooo_categories";
$result = mysql_query($sql);
echo $result ? 'all good' : mysql_errno($l) . ': ' . mysql_error($l) . "\n";
答案 1 :(得分:0)
您需要检查是否已建立连接
$mysql_link = mysql_connect($host,$user,$pwd);
if (!$mysql_link) {
echo "Couldn't connect to the database. Check your credentials";
die();
}
//code
$result = mysql_query($sql);
if (!$result) {
echo "Oops, something seems to be wrong.";
die();
}
答案 2 :(得分:0)
我建议尝试这个。您需要准确确定错误的来源。
<?php
// remplacer par vos valeurs de connexion
$host="*****";
$user="*****";
$pwd="******";
$base="*****";
$mysql_link = mysql_connect($host,$user,$pwd);
if (!$mysql_link) {
die(mysql_error());
}
$db_name = mysql_select_db($base);
if (!$db_name) {
die(mysql_error());
}
$dbsite="arfooo"; // prefixe de votre table sans "_" par exemple : "arfooo"
$sql = "SELECT * FROM ".$dbsite."_categories";
$result=mysql_query($sql) or die(mysql_error());
while ($row=mysql_fetch_object($result)) {
$depth=0;
echo "INSERT INTO `".$dbsite."_categoryparents` (`parentId`, `childId`, `depth`) VALUES ($row->categoryId,$row->categoryId,$depth);<br>\n";
$parentId = $row->parentCategoryId;
while ($parentId<>0){
$sql2 = "SELECT * FROM ".$dbsite."_categories WHERE categoryId = $parentId";
$result2=mysql_query($sql2);
while ($row2=mysql_fetch_object($result2)) {
$depth=$depth+1;
echo "INSERT INTO `".$dbsite."_categoryparents` (`parentId`, `childId`, `depth`) VALUES ($row2->categoryId,$row->categoryId,$depth);<br>\n";
$parentId = $row2->parentCategoryId;
}
}
$depth=$depth+1;
echo "INSERT INTO `".$dbsite."_categoryparents` (`parentId`, `childId`, `depth`) VALUES (0,$row->categoryId,$depth);<br>\n";
}
?>