2011-03-09 142 views
0

我需要使用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 

我需要解決此問題... 謝謝幫助!

+1

「拒絕訪問」是不是一種編程問題。 – 2011-03-09 06:47:29

+0

您是否能夠使用mysqladmin使用這些憑證訪問數據庫? – rkg 2011-03-09 06:48:14

+0

btw你應該使用Joomla!的dtabase類,用'$ db =&Jfactory :: getDBO()'實例化它,所以你不會遇到這種問題。這是v。1.5.x,你使用這個還是1.6? – 2011-03-09 06:52:36

回答

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>"; 
} 
0

更好的是,重複你的index.php。 從你的代碼中間(比如在$ mainframe-> 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!"); 
} 

....