我正在尝试从mySQL数据库中提取信息。 我正在使用的代码是:
<?php global $current_user;
get_currentuserinfo();
//echo 'Username: ' . $current_user->user_login . "\n";
//echo 'User email: ' . $current_user->user_email . "\n";
//echo 'User first name: ' . $current_user->user_firstname . "\n";
//echo 'User last name: ' . $current_user->user_lastname . "\n";
//echo 'User display name: ' . $current_user->display_name . "\n";
echo $current_user->display_name . "\n";
//echo 'User ID: ' . $current_user->ID . "\n";
?>
<?php
$members_settings = get_option('members_db');
$host = $members_settings['members_db_server']; // ''; //replace your IP or hostname
$user = $members_settings['members_db_user']; // ''; //database user
$password = $members_settings['members_db_pass']; //'';//database password
$database = $members_settings['members_db_database']; /*''; //database name
$prefix = ''; //prefix if any else just give any random value
$driver = 'mysql'; //here u can also have ms sql database driver, postgres, etc
$options = array ( 'driver' => $driver, 'host' => $host, 'user' => $user, 'password' => $password, 'database' => $database, 'prefix' => $prefix );
//$db = JDatabase::getInstance($options);
$con = mysql_connect($host,$user,$password);
$db_selected = mysql_select_db($database, $con);
// if (!$db_selected) { die ("Can\'t use ".$database." : " . mysql_error()); }
$userid = $current_user->user_login;
$sql = "SELECT * FROM ibew_Members WHERE Card = $userid";
//echo $sql;
$result = mysql_query($sql);
if ($result) {
list($id) = mysql_fetch_array(mysql_query($sql));
//echo $id;
} else {
echo "query failed";
}
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
目前我收到的查询失败是我的错误,我不确定问题出在哪里,因为在这方面的专业知识方面,我仍然有点茫然。
对于如何解决这个问题的任何见解将不胜感激。
答案 0 :(得分:0)
不推荐使用Mysql_和mysqli_,您应该考虑迁移到PDO框架。这是一个例子:
function createWindow() {
var note_window = document.createElement("div");
note_window.setAttribute("id", "note_window");
note_window.setAttribute("class", "notification-window");
note_window.style.visibility = 'visible';
note_window.innerHTML = 'Text';
var title = document.createElement("div");
title.setAttribute("id", "title");
title.setAttribute("class", "col-md-12");
title.innerHTML = "More Text";
note_window.appendChild(title);
var navbar = document.getElementById("navbar");
navbar.appendChild(note_window);
}
createWindow();
这为您提供了更清晰的数据库事务,并有助于防止使用PDO对象进行注入。 Try / Catch还会报告错误并提供更详细的系统错误。
答案 1 :(得分:0)
顺便说一下,我看到你正在评论JDatabase :: ...如果这是在joomla中,你仍然可以使用Joomla框架来获取你的查询数据:
define( '_JEXEC', 1 );
define( 'JPATH_BASE', '/Your local path to root/' );
require_once ( JPATH_BASE.'/includes/defines.php' );
require_once ( JPATH_BASE.'/includes/framework.php' );
$mainframe = JFactory::getApplication('site');
$mainframe->initialise();
$user = JFactory::getUser();
$config =& JFactory::getConfig();
$session =& JFactory::getSession();
// get the Joomla DBO config
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$cardsQuery = "SELECT * FROM ibew_Members WHERE Card = ".$db->quote($user->id);
$db->setQuery($cardsQuery);
try {
$results = $db->loadAssocList();
if (count($results) > 0) { // we have results
foreach($results as $result) {
echo "Relevant Info: ".$result['fieldname'];
}
} else {
echo "No results found in the database for this user<br>\n";
}
}
catch (Exception $e) {
echo "Unable to complete Transaction: ".$e."<br>\n";
}
与我的其他答案基本相同,只是在Joomla PDO框架下。