从while循环插入到mysql不起作用

时间:2014-09-17 02:28:09

标签: php loops

我有一个用户access_token的数据库,我做了代码,确保如果数据库中已经存在新的用户访问令牌,它应该显示错误我试图做一个循环并将if语句放在其中但它不起作用。

<?php

mysql_connect("localhost", "___", "___");
mysql_select_db("___");
session_start();

require_once ('twitteroauth/twitteroauth.php');
require_once ('config.php');

if (empty($_SESSION['access_token']) || 
    empty($_SESSION['access_token']['oauth_token']) || 
    empty($_SESSION['access_token']['oauth_token_secret'])) {
  header('Location: ./clearsessions.php');
}

$access_token = $_SESSION['access_token'];
$connection = new TwitterOAuth(CONSUMER_KEY, 
                               CONSUMER_SECRET,
                               $access_token['oauth_token'],
                               $access_token['oauth_token_secret']);
$content = $connection->get('account/verify_credentials');
$u = "SELECT * FROM users";
$uu = mysql_query($u);

while ($uuu = mysql_fetch_object($uu)) 
{
  foreach($uuu->access_token as $ali)
  {
    if ($access_token['oauth_token'] != $ali)
    {
      $newuser = "INSERT INTO users(access_token,access_token_secret)
                  VALUES ('{$access_token['oauth_token']}',
                          '{$access_token['oauth_token_secret']}' )";
      $donewuser = mysql_query($newuser);
      include ('html.inc');
    }
    else
    {
      echo "error";
    }
  }
}

1 个答案:

答案 0 :(得分:0)

您需要将连接分配给变量,否则它实际上没有做任何事情。

$link = mysql_connect('localhost','user','password');
mysql_select_db('database_name', $link);

*snip*

$uu = mysql_query($u, $link);

另外,请不要使用mysql函数,因为它们已被弃用。您应该使用PDOMysqli