need help on displaying records
i need help on displaying records using php and mysql. My knowledge with is very basic although i can read and interpret codes correctly. Im currently developing a website using joomla as the CMS..i need custom code for php to display the records contents of the table in my mysql database.
here's the code..i have give the correct details of the account but then i cannot access it.
$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);
}
here's the error
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
I need help in resolving this problem... thanks!
Since you're in Joomla, you should take advantage of their API, so you won't run into this kind of problems and will be more seamlessly integrated in the whole framework. I believe this works both for 1.5.xx series and 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>";
}
Better, duplicate your index.php. From the middle of your code( say after $mainframe->triggerEvent('onAfterRoute');) you can use database queries and then exit.
You can use jos_content instead of #__content in the code below. Say your code is named db.php, just run from your browser: http://localhost/website/db.php
I'm doing like this:
{
$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!");
}
....
精彩评论