2012-04-25 140 views
-2

可能重複:
PHP Error: mysql_fetch_array() expects parameter 1 to be resource, boolean givenmysql_fetch_array()預計參數1是資源,鑑於布爾

當我運行我的腳本我不斷地得到這些錯誤,我想不通出我需要修復,因爲現在它不顯示任何PHP,我也需要它。

這是我收到的錯誤。

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/WordOfMouth/root/pages/profile_page/profile_page.php on line 38 

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/WordOfMouth/root/pages/profile_page/profile_page.php on line 44 

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/WordOfMouth/root/pages/profile_page/profile_page.php on line 136 

任何幫助將是不可思議的。

<?php 
include_once("../../mysql_server/checkuserlog.php"); 
?> 
<?php 
// Now let's initialize vars to be printed to page in the HTML section so our script does not return errors 
// they must be initialized in some server environments 
$id = ""; 
$firstname = ""; 
$middlename = ""; 
$lastname = ""; 
$country = ""; 
$state = ""; 
$city = ""; 
$zip = ""; 
$bio_body = ""; 
$bio_body = ""; 
$user_pic = ""; 
$blabberDisplayList = ""; 
// If coming from category page 
if ($_GET['id']) { 

$id = $_GET['id']; 

} else if (isset($_SESSION['id'])) { 

$id = $_SESSION['id']; 

} else { 

    include_once "../home_page/home_page.php"; 

    exit(); 
} 

$id = mysql_real_escape_string($id); 
$sql = mysql_query("SELECT * FROM myMembers WHERE id='$id' LIMIT 1"); 

if (mysql_num_rows($sql) === 0) { // evaluate the count 
echo("ERROR"); 
    exit(); 
} 
// ------- END MAKE SURE PERSON EXISTS IN DATABASE --------- 
// Make sure this person a visitor is trying to view actually exists 
while($row = mysql_fetch_array($sql)){ 
$firstname = $row["firstname"]; 
$lastname = $row["lastname"]; 
$country = $row["country"]; 
$state = $row["state"]; 
$city = $row["city"]; 
$sign_up_date = $row["sign_up_date"]; 
    $sign_up_date = strftime("%b %d, %Y", strtotime($sign_up_date)); 
$last_log_date = $row["last_log_date"]; 
    $last_log_date = strftime("%b %d, %Y", strtotime($last_log_date)); 
$bio_body = $row["bio_body"]; 
$bio_body = str_replace("&amp;#39;", "'", $bio_body); 
$bio_body = stripslashes($bio_body); 
$website = $row["website"]; 
$youtube = $row["youtube"]; 
$facebook = $row["facebook"]; 
$twitter = $row["twitter"]; 
$friend_array = $row["friend_array"]; 
/////// Mechanism to Display Pic. See if they have uploaded a pic or not  ////////////////////////// 
$check_pic = "../../members/$id/image01.jpg"; 
$default_pic = "../../members/0/image01.jpg"; 
if (file_exists($check_pic)) { 
$user_pic = "<img src=\"$check_pic?$cacheBuster\" width=\"218px\" />"; 
} else { 
$user_pic = "<img src=\"$default_pic\" width=\"218px\" />"; 
} 

} 

?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html" /> 
<meta name="Description" content="Profile for <?php echo "$username"; ?>" /> 
<meta name="Keywords" content="<?php echo "$username, $city, $state, $country"; ?>" /> 
<meta name="rating" content="General" /> 
<meta name="ROBOTS" content="All" /> 
<title>Profile Page</title> 
<link href="../../style.css" rel="stylesheet" type="text/css" /> 
<link rel="icon" href="#" type="image/x-icon" /> <!-- INSERT ICON --> 
<link rel="shortcut icon" href="#" type="image/x-icon" /> <!-- AND HERE --> 
<script src="../../jquery-1.4.2.js" type="text/javascript"></script> 

</head> 
<body> 
<div class="wrapOverall"> 
<?php include_once '../../templates/decide_header.php'; ?> 
<table width="900" border="0" style="background-color:#F2F2F2; border:#999 1px solid;" align="center" cellpadding="0" cellspacing="0"> 
    <tr> 
    <td width="738"><br /> 
     <table width="90%" border="0" align="center" cellpadding="6"> 
     <tr> 
     <td width="48%" valign="top"> 
     <?php print "$firstname $lastname"; ?> 
     <br /> 
     <?php print "$user_pic"; ?> 
     <br /> 
     Member Since: <?php print "$sign_up_date"; ?> 
     <br /> 
     <?php print "$youtubeChannel"; ?> 
     </td> 
     <td width="52%" valign="top"> 
     <br /> 
     <strong><u><?php print "$firstname $lastname"; ?>'s Blabs:</u></strong> 
     <br /> 
     <?php print "$the_blab_form"; ?> 
     <div style="width:100%; height:180px; overflow:auto; overflow-x:hidden;"> 
     <?php print "$blabberDisplayList"; ?> 
     </div> 
     <br /> 
     <br /> 
     <strong><u><?php print "$firstname $lastname"; ?>'s Location Details:</u></strong> 
    <br /> 
    <?php print "$city"; ?> &bull; <?php print "$state"; ?> &bull; <?php print "$country"; ?> 
    <br /> 
    <br /> 
    <strong><u>About <?php print "$firstname $lastname"; ?></u></strong> 
    <br /> 
    <?php print "$bio_body"; ?> 
    <br /> 


    </td> 
    </tr> 
    <tr> 
    <td colspan="2" valign="top">&nbsp;</td> 
    </tr> 
    </table> 
    <p><br /> 
    <br /> 
    </p></td> 
<td width="160" valign="top"><?php include_once "../../templates/right_AD_template.php"; ? ></td> 
    </tr> 
</table> 
<?php include_once '../../templates/footer_template.php'; ?> 
</div> <!-- END wrapOverall --> 
</body> 
</html> 
+0

很明顯,問題不在於你向我們展示的代碼中,而是在其他一些使用mysql_ *函數的代碼中。 – 2012-04-25 22:36:41

+0

看右邊的相關問題。我的天啊。我確信他們在寫這個問題的時候出現了。 – webbiedave 2012-04-25 22:40:58

+1

如果任何幫助*會令人難以置信*,爲什麼要問? :) – 2012-04-26 21:48:19

回答

1

在代碼的下面兩行:

$sql = mysql_query("SELECT * FROM myMembers WHERE id='$id' LIMIT 1"); 
if (mysql_num_rows($sql) === 0) { // evaluate the count 

如果mysql_query失敗,你沒有一個可行的resource可以傳遞到mysql_num_rows()

你應該先檢查,以確保查詢居然跑:

$sql = mysql_query("SELECT * FROM myMembers WHERE id='$id' LIMIT 1"); 
if (!$sql) { 
    // query failed 
    echo "Query failed: " . mysql_error(); 
} else { 
    $num_rows = mysql_num_rows($sql); 
    // rest of your code 
} 

因爲你有===,因爲空是由函數返回,因爲它是一個壞的值稱爲表達mysql_num_rows($sql) === 0判斷爲真。因此,你有相同的問題,mysql_fetch_array被稱爲一個無效的資源。

主要線索是:mysql_num_rows() expects parameter 1 to be resource, boolean given

它期望一個資源,但它被給了一個布爾值,它不能使用。因此,您可以調查您傳遞給它的變量的值以及爲什麼它是一個布爾而不是資源(在這種情況下,因爲查詢失敗)。

+0

它告訴我錯誤是「沒有選擇數據庫」,但我調試了整個connect_to_mysql,它似乎正在自行連接,並在其他每個頁面上工作。有沒有我正在做的事情會導致它取消選擇數據庫? – 2012-04-26 01:22:07

+0

我在您發佈的代碼中看不到任何內容。如果未選中,可能是其中一個包含的某個代碼,或者您分配連接的變量正在被覆蓋(如果您正在分配一個)。 – drew010 2012-04-26 01:37:29

相關問題