2011-09-15 69 views
2

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select在php中处理数组?

我似乎遇到了与我的PHP的问题,收到此错误:

Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in /home/nightl7/public_html/demos/autocompletejquery/submit.php on line 27

Warning: mysql_query() [function.mysql-query]: Access denied for user 'nightl7'@'localhost' (using password: NO) in /home/nightl7/public_html/demos/autocompletejquery/submit.php on line 36

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/nightl7/public_html/demos/autocompletejquery/submit.php on line 36 Access denied for user 'nightl7'@'localhost' (using password: NO) SELECT * FROM markers WHERE select3 = ''

这是我的代码有:

<?php 
require("db_access.php"); 

function parseToXML($htmlStr) 
{ 
$xmlStr=str_replace('<','&lt;',$htmlStr); 
$xmlStr=str_replace('>','&gt;',$xmlStr); 
$xmlStr=str_replace('"','&quot;',$xmlStr); 
$xmlStr=str_replace("'",'&#39;',$xmlStr); 
$xmlStr=str_replace("&",'&amp;',$xmlStr); 
return $xmlStr; 
} 

$name=$_POST['name']; 
$address=$_POST['address']; 
$type=$_POST['type']; 
$request=$_POST['$_REQUEST']; 

// Select all the rows in the markers table 
$inputs = array('select3'); 
$where = array(); 


foreach($inputs as $input) 
{ 
    if(!empty($_POST[$input])) { 
     $where[] = "{$input} = '" . mysql_real_escape_string($_POST[$input]) . "'"; 
    } 
} 

if ($where) { 
    $query = 'SELECT * FROM markers WHERE ' . implode(' AND ', $where); 
} else { 
    user_error("No rows returned by:<br />\n$query"); 
} 
$result = mysql_query($query); 
if($result == false) { 
    die(mysql_error() . "<br />\n$query"); 
} 
if(mysql_num_rows($result) == 0) { 
    user_error("No rows returned by:<br />\n$query"); 
} 

// Fetch the result  
$rowset = array(); 
while ($row = mysql_fetch_array($result)) { 
    $rowset[] = $row; 
} 

// Look at your rowset structure: 
print_r($rowset); 


header("Content-type: text/xml"); 

// Start XML file, echo parent node 
echo '<markers>'; 

// Iterate through the rows, printing XML nodes for each 
while ($row = @mysql_fetch_assoc($result)){ 
    // ADD TO XML DOCUMENT NODE 
    echo '<marker '; 
    echo 'name="' . parseToXML($row['name']) . '" '; 
    echo 'address="' . parseToXML($row['address']) . '" '; 
    echo '/>'; 
} 

// End XML file 
echo '</markers>'; 

?> 

而且我不知道为什么我得到一个密码错误,因为这是我的db_access.php看起来像:

<? 
$username="nightl7_mapus"; 
$password="MYPASSWORD - is is correct :)"; 
$database="nightl7_map"; 
?> 
+0

请修复您的标题:技术列表不是您问题的描述。这个问题是......呃,究竟是什么? –

+0

你好,我的问题是我如何使上述代码工作。我读了可能会有不同类型的数组,我仍然无法确定我做的是否正确。 – user944589

+0

我的感觉是,这太“太”了。你有关于编程语言的具体问题吗? –

回答

4

你到目前为止看起来相当不错。构建WHERE子句时,您已正确转义SQL输入。您已正确检查查询结果中的错误。看起来你需要做的下一件事就是取回您的查询结果行:你的db_access.php

// You already have.... 
$result = mysql_query($query); 
if($result == false) { 
    die(mysql_error() . "<br />\n$query"); 
} 
if(mysql_num_rows($result) == 0) { 
    user_error("No rows returned by:<br />\n$query"); 
} 

// Fetch the result  
$rowset = array(); 
while ($row = mysql_fetch_array($result)) { 
    $rowset[] = $row; 
} 

// Look at your rowset structure: 
print_r($rowset); 

// then do something with the $rowset array 

UPDATE问题:

如果您的用户名和密码,在这个文件是正确的,也许它不是” t正在被正确解析,因为short_open_tags在您的Web服务器上被禁用。

尝试更改<?<?phpdb_access.php

+0

谢谢迈克尔,我会试试这:)) – user944589

+0

你好迈克尔,你知道我为什么可能会得到一个密码错误,当我包括我的数据库访问在PHP?谢谢:) – user944589

+0

@ user944589看我的更新。它可能是短的PHP开放标签'<?' –