我是数据库世界的新手......我试图从搜索查询连接到twitter API,而不是将结果发布到mysql中的数据库(在数据库中创建数据库)如果没有数据库,则进程... ...如果我的代码分散且分散,我很抱歉,这是一个班级的小组项目,我的小组并不是最干净的编码员......任何想法/想法或者指出任何看似不正确的事情将是一个巨大的帮助。感谢...
输入查询后,收到错误:
The twitter
Database does not exist. Creating it now ....
Warning: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES)' in C:\xampp\htdocs\TwitterDB\database\database.php:17 Stack trace: #0 C:\xampp\htdocs\TwitterDB\database\database.php(17): PDO->__construct('mysql:host=loca...', 'root', 'sesame') #1 C:\xampp\htdocs\TwitterDB\searchDB.php(8): Database->__construct('twitter') #2 {main} thrown in C:\xampp\htdocs\TwitterDB\database\database.php on line 17
Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\TwitterDB\database\database.php on line 17
这是我的database.php
class Database {
var $db;
public function __construct($dbname) {
$dsn = 'mysql:host=localhost;dbname=' .$dbname;
$username = 'root';
$password = 'sesame';
try {
$this ->db = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
echo '<br> The <b>' .$dbname . '<br> Database does not exist. Creating it now .... <b>';
try {
$this ->db = new PDO('localhost', $username, $password);
$sql = "create database twitter;
use twitter;
create table tweets(
id VARCHAR(30) NOT NULL,
date dateTime,
from_user_id INT,
from_user_name VARCHAR(30),
to_user_id INT,
to_user_name VARCHAR(30),
geo VARCHAR(30),
profile_image_url VARCHAR(200),
text VARCHAR(150),
PRIMARY KEY (id, date, from_user_id)
)";
$this ->db ->exec($sql);
echo 'Done!<br>';
} catch (PDOException $e) {
echo $e ->getMessage();
exit();
}
}
}
public function close() {
try {
$this ->db - null;
} catch (PDOException $e) {
echo $e ->getMessage() . "Exit!";
exit();
}
}
public function insertTweets($tweets) {
$sql = "INSERT INTO TWEETS
(id, date, from_user_id, from_user_name, profile_image_url, text)
VALUES (:id, :date, :from_user_id, :from_user_name, :profile_image_url, :text)";
try {
$x = $this ->db ->prepare($sql);
foreach($tweets as $t) {
$parameters = array(
':id'=> $t ->id,
':date'=> date('Y-m-d- H:i:s', strtotime($t ->date)),
':from_user_id'=> $t ->from_user_id,
':from_user_name'=> $t ->from_user_name,
':profile_image_url'=> $t ->profile_image_url,
':text'=> $t ->text
);
$x ->execute($parameters);
}
} catch (PDOException $e) {
die ('Insert attempt failed:' . $e -> getMessage());
}
}
public function clearTable() {
try {
$x = $this ->db ->prepare('TRUNCATE TABLE tweets');
$x = execute();
} catch (PDOException $e) {
die('Attempt failed:' . $e ->getMessage());
}
}
public function search($query) {
try {
$x = $this ->db ->prepare($query);
$x -> execute();
} catch (PDOException $e) {
die ('Query failed:' . $e -> getMessage());
}
echo '<table border = 1>';
$heading = true;
while ( ($row = $x -> fetch(PDO::FETCH_ASSOC))) {
echo '<tr>';
if ($heading) {
$keys = array_keys($row);
foreach ($keys as $k) {
echo '<th>' .$k . '<th>';
}
echo '</tr><tr>';
$heading = false;
}
foreach($row as $r => $v) {
echo '<td>' . $v . '</td>';
}
echo '</tr>';
}
echo '</table>';
}
}
这是我的insertDB.php
<?php
ini_set('display_errors', 1);
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "3219120843-cvff5Rj8LeFnmm6aM2eSdWN7bEKRwmjnJW64Wms",
'oauth_access_token_secret' => "MZS2Om2TRr9e56fgLAjWx7iwX3n7Svz18ya5iAXyWeMQS",
'consumer_key' => "y4sArn9Zl4DaA8KTPLIxFXVh8",
'consumer_secret' => "5Yj9soIQtLoQqbiXIgo1PhMSvI5sExsg13REHWGa1pPTzhTr2p"
);
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$getfield = '?q='.$_POST['keyword'];
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$result = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$tweets = json_decode($result);
$tweet = $tweets->statuses;
echo $result;
require 'data_model.php';
$records = tweets2array($tweet);
require 'view.php';
echo tweet_obj_array2table($records);
$tweets = json_decode($result)->results;
$objects = tweets2array($tweets);
echo 'Total tweets:' . count($objects);
$database = new Database('twitter');
$table = 'tweets';
$database -> insertTweets($objects);
$database -> close();
?>
这是我的datamodel.php
<?php
function tweets2array($tweets) {
$records = array();
foreach($tweets as $tweet) {
$t = new stdClass();
$t->sender = $tweet->user->name;
$t->text = $tweet->text;
$t->time = $tweet->created_at;
$t->image = $tweet->user->profile_image_url;
$records[] = $t;
}
return $records;
}
?>
答案 0 :(得分:0)
您的数据库凭据似乎没有对数据库的正确权限。我建议再次验证它们并刷新特权。这应该可以解决问题。