2013-03-23 81 views
-5

我遇到了mysql_select_db()功能不選擇正確的數據庫的一個問題我的代碼如下:mysql_select_db(不選擇數據庫

global $file_count; 


    $conlivedata = mysql_pconnect("localhost","*****","*****"); 
     if (!$conlivedata) { 
     echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 
    $con = mysql_pconnect("***************","*****","*****"); 
    if (!$con) { 
     echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 
    mysql_select_db("csv_db", $con)or die("A MySQL error has occurred.Your Query: Error: (" . mysql_errno() . ") " . mysql_error()); 

    $result = mysql_query("SELECT * FROM `tbl_name` WHERE EmailAddr <> ' ' ORDER BY `EmailAddr`") or die("<br/>A MySQL Query error has occurred.Your Query: Error: (" . mysql_errno() . ") " . mysql_error()); 

    while($row = mysql_fetch_array($result)) 
    { 
     echo $row['first_name'] . " " . $row['last_name']; 
     $Email = $row['EmailAddr']; 
     if ($Email != NULL){ 
      mysql_select_db("foswood_db", $conlivedata) or die("<br/>A MySQL error has occurred Slecting Database.<br />Your Query: <br/> Error: (" . mysql_errno() . ") " . mysql_error()); 
      $liveresult = mysql_query("SELECT COUNT(*) FROM pledges WHERE EmailAddr = '$Email'") or die("<br/>A MySQL error has occurred.<br />Your Query: <br /> Error: (" . mysql_errno() . ") " . mysql_error()); 
      if(mysql_result($liveresult, 0)) echo ': **Voted**';; 
      mysql_select_db("csv_db", $con) or die("<br/>A MySQL error has occurred Slecting Database.<br />Your Query: <br /> Error: (" . mysql_errno() . ") " . mysql_error()); 
     } 
     echo "<br />"; 
    } 

    mysql_close($con); 
    mysql_close($conlivedata); 

我得到的錯誤信息:

A MySQL error has occurred. 
Your Query: 
Error: (1146) Table 'csv_db.pledges' doesn't exist 

有沒有人任何想法爲什麼請?

+2

我想你已經有了你的答案。 – hjpotter92 2013-03-23 11:54:24

+0

再次檢查你的表名,還爲什麼你聲明瞭mysql_select_db 2次? – 2013-03-23 11:56:35

+0

@DevangRathod有兩個不同的連接。 – hjpotter92 2013-03-23 11:58:04

回答

1

您應該指定用於查詢的數據庫。

$result = mysql_query("SELECT * FROM `tbl_name` WHERE EmailAddr <> ' ' ORDER BY `EmailAddr`", **$con**) ... 
+0

我不得不奇怪地重寫與打開第一個連接的代碼。這解決了這個問題。 – 2013-03-24 19:25:45