需要帮助显示记录

时间:2011-03-09 06:46:26

标签: php mysql joomla

我需要帮助才能使用php和mysql显示记录。虽然我能正确阅读和解释代码,但我的知识非常基础。我目前正在使用joomla开发一个网站作为CMS ..我需要php的自定义代码来显示我的mysql数据库中表的记录内容。

这里是代码..我已经提供了帐户的正确详细信息但我无法访问它。

$user_name = "kansai_ksadmin";
$password = "sample123";
$database = "kansai_ksdb";
$server = "localhost";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
  $SQL = "SELECT jos_djcf_categories.name AS category, jos_djcf_items.name AS title, jos_djcf_items.description FROM jos_djcf_categories INNER JOIN jos_djcf_items ON jos_djcf_categories.id = jos_djcf_items.cat_id";
  $result = mysql_query($SQL);
  while ($db_field = mysql_fetch_assoc($result)) {
    print $db_field['category'] . "<BR>";
    print $db_field['title'] . "<BR>";
    print $db_field['description'] . "<BR>";
  }
  mysql_close($db_handle);
}
else {
   print "Database NOT Found ";
   mysql_close($db_handle);
}

这是错误

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'kansai_ksadmin'@'localhost' (using password: YES) in /home/kansai/domains/kansaiscene.com/public_html/beta/modules/mod_php/mod_php.php(36) : eval()'d code on line 9

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /home/kansai/domains/kansaiscene.com/public_html/beta/modules/mod_php/mod_php.php(36) : eval()'d code on line 10
Database NOT Found
Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in /home/kansai/domains/kansaiscene.com/public_html/beta/modules/mod_php/mod_php.php(36) : eval()'d code on line 23

我需要帮助来解决这个问题...... 谢谢!

2 个答案:

答案 0 :(得分:1)

由于你在Joomla,你应该利用他们的API,所以你不会遇到这种问题,并将更加无缝地集成到整个框架中。我相信这适用于1.5.xx系列和1.6。

$db =& JFactory::getDBO();

$query = "SELECT #_djcf_categories.name AS category, #_djcf_items.name AS title, #_djcf_items.description FROM #_djcf_categories INNER JOIN #_djcf_items ON #_djcf_categories.id = #_djcf_items.cat_id";

$db->setQuery($query);

$rows = $db->loadAssocList();

foreach ($rows as $row)
{
    print $row['category'] . "<BR>";
    print $row['title'] . "<BR>";
    print $row['description'] . "<BR>";
}

答案 1 :(得分:0)

更好,复制你的index.php。 从代码中间开始(比如在$ mainframe-&gt; triggerEvent('onAfterRoute')之后;),您可以使用数据库查询然后退出。

您可以在下面的代码中使用jos_content而不是#__content。 假设您的代码名为db.php,只需从浏览器运行:http://localhost/website/db.php

我这样做:

{
$db =& JFactory::getDBO(); 

#insert the last article and category
$myquery=<<<EOF
insert into #__content SET `title`='Last Forms Article',`alias`='last-forms-article-234',`introtext`='A',id=149999,`state`=0,`sectionid`=0,catid=0;


EOF;


$db->setQuery($myquery);

$db->query() ;

$myquery=<<<EOF

insert INTO #__categories set `section`=42,`id`='149999',`alias`='last-forms-category-234',`published`='0',`title`='Last Category';

EOF;

$db->setQuery($myquery);
$db->query() ;
//Abort if id 149999 does not exist in both category and article table

$myquery=<<<EOF

select * from #__content,#__categories where #__content.id='149999' and #__categories.id='149999';
EOF;

$db->setQuery($myquery);

if($db->query() ==FALSE ||   $db->getNumRows()!=1)
{
    die ("Last article and category could not be insertted!");
}

...