【发布时间】:2014-06-29 13:17:33
【问题描述】:
编码出现非法字符串偏移:
echo $row['rowa']. ' - '. $row['rowb']. ' - '. $row['rowc']. ' - '. $row['rowd']. ' - '. $row['rowe'].'<br />';
<?php
$connection=mysqli_connect("localhost"/*hostname*/,
"username"/*username*/,
"password"/*password*/,
"dbname"/*database name*/);
try {
// Setting the query and runnin it...
$result = mysqli_query($connection,"SELECT * FROM table");
if(mysqli_num_rows($result)>0)
){
foreach(mysqli_fetch_array($result) as $row) {
echo $row['rowa']. ' - '. $row['rowb']. ' - '. $row['rowc']. ' - '. $row['rowd']. ' - '. $row['rowe'].'<br />';
}
}else{
echo "QUERY IS NOT OKAY";
} // Closing the connection.
$connection = null;
}catch(PDOException $e) {
echo $e->getMessage();
}
?>
用户名、密码、数据库和表名不是代码中使用的实际名称。
【问题讨论】:
-
您正在混合使用不同的数据库连接器:
mysql_...和mysqli_...是两个完全不同的东西。你不能混合它们,你必须决定使用哪一个。由于旧的mysql_...连接器不久前已被弃用,您应该选择更新、更安全、更灵活的mysqli_...连接器。您还想了解“准备好的语句”在防止 sql 注入漏洞方面的优势。 -
你的密码真的是
password,用户名真的是username,dbname真的是dbname吗?如果你的答案是肯定的,那么你是我这辈子见过的最粗心的程序员。 -
table是保留字。如果您真的想将其用作表名,则需要将其包装在反引号中。您最好使用不同的表名。 -
@MikeW 我不使用实际的变量。
-
你有一个括号
)太多try {// Setting the query and runnin it... $result = mysqli_query($connection,"SELECT * FROM table"); if(mysqli_num_rows($result)>0) ){更改为try {// Setting the query and runnin it... $result = mysqli_query($connection,"SELECT * FROM table"); if(mysqli_num_rows($result)>0) {@TheOkayMan