Php 为什么我的mysql\u查询没有返回结果?
编码与以下内容的非法字符串偏移量发生冲突:Php 为什么我的mysql\u查询没有返回结果?,php,mysql,sql,foreach,Php,Mysql,Sql,Foreach,编码与以下内容的非法字符串偏移量发生冲突: echo $row['rowa']. ' - '. $row['rowb']. ' - '. $row['rowc']. ' - '. $row['rowd']. ' - '. $row['rowe'].'<br />'; <?php $connection=mysqli_connect("localhost"/*hostname*/, "username"/*us
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();
}
?>
echo$row['rowa'].-'$第['rowb'].-'行$行['rowc'].-'$行['rowd'].-'$行['rowe']。
;
我想您应该使用mysqli\u查询
以及mysqli\u连接
,而不是mysql\u查询
$result=mysql\u查询($connection,“SELECT*fromtable”)代码>
使用
$result=mysqli_query($connection,“SELECT*fromtable”)代码>
而不是
mysql\u fetch\u数组($result)
使用
mysqli\u fetch\u数组($result)
试试这个
<?php
$mysqli = new mysqli("localhost", "username", "password", "Database name");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
// Setting the query and runnin it...
$result = $mysqli->prepare("SELECT * FROM table");
/* execute query */
$result->execute();
$result = $mysqli->get_result();
while ($row= $result->fetch_assoc())
{
echo $row['rowa']. ' - '. $row['rowb']. ' - '. $row['rowc']. ' - '. $row['rowd']. ' - '. $row['rowe'].'<br />';
}
?>
很可能是您的连接有问题。由于mysqli*不抛出异常,因此无法捕获它们
这些函数在出错时返回有效对象或FALSE、0或null。所以你需要检查一下
这是代码,它应该告诉您与DB的通信有什么问题
<?php
function ctest() {
$connection=mysqli_connect("localhost"/*hostname*/,
"username"/*username*/,
"password"/*password*/,
"dbname"/*database name*/);
if($connection) {
$result = mysqli_query($connection,"SELECT * FROM `table`");
if($result) {
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 />';
}
return;
}
echo "0 rows";
return;
}
echo "No result";
return;
}
echo "No connection";
$connection = null;
}
?>
您正在将mysqli
与PDO异常try/catch
混合,并且在try
块中还有一个额外的括号。这两个API不混合
查看代码中的注释:(以及下面的固定代码)
旁注:您可能需要将foreach(mysqli\u fetch\u数组($result)作为$row)
更改为while($row=mysqli\u fetch\u assoc($result))
使用foreach(mysqli\u fetch\u array($result)作为$row)
将生成行的重复结果,并且只生成每行的第一个字母
try {
// Setting the query and runnin it...
$result = mysqli_query($connection,"SELECT * FROM table");
if(mysqli_num_rows($result)>0)
){
// ^-- one bracket too many
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) { // this should be catch (Exception $e)
echo $e->getMessage();
}
重写:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$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)
{
// use while instead of foreach
// while ($row = mysqli_fetch_assoc($result)){
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";
}
}
catch (Exception $e)
{
echo $e->getMessage();
}
您正在混合使用不同的数据库连接器:mysql\uu…
和mysqli\uu…
是两个完全不同的东西。你不能把它们混在一起,你必须决定用哪一种。由于旧的mysql\uu…
连接器不久前已被弃用,您应该选择更新、更安全、更灵活的mysqli\u…
连接器。您还想了解“准备好的语句”在防止sql注入漏洞方面的优势。您的密码真的是password
,用户名真的是username
,数据库名真的是dbname
?如果你的答案是肯定的,那么你是我一生中见过的最粗心的程序员。table
是一个保留字。如果确实想将其用作表名,则需要将其用反勾号包装。您最好使用不同的表名。@MikeW我没有使用实际的变量。您在中有一个括号)
太多请尝试{//设置查询并运行它…$result=mysqli\u query($connection,“SELECT*FROM table”);如果(mysqli\u num\u rows($result)>0)){
更改为请尝试{//设置查询并在其中运行…$result=mysqli_query($connection,“SELECT*FROM table”);if(mysqli_num_rows($result)>0){
@theokyman带有“i”的字符,我得到的错误是对行echo$row['Name'.-'$row['Club'.-'.$row['Level'.-'.-.$row['App'.-'.$row['Score'.-.$row['“
”;
检查列的名称。mysqli_fetch_array()构建一个包含数字和文本索引的混合数组。使用mysqli_fetch_assoc()获取一个关联数组,这样数组的大小就不会增加一倍。您的列可能不相同,这样索引就不存在。我得到的错误与DhruvPathak的答案相同。put foreach()在这种if条件下,如果(mysqli_num_rows($result)>0){foreach(mysql_fetch_array($result)as$row){……}非法的字符串偏移量,我已经在“`$result->execute()中更新了对非对象的成员函数execute()的编码调用;
您的表名是什么表名是分数
您是否在查询中添加了任何where条件或按原样使用?请保持原样,除非它能使编码工作正常。非法的字符串偏移量列名区分大小写。列名拼写正确,大小写正确。它应该告诉您,哪一个偏移量是inco正确。从那里开始。时间和时间+1effort@EliasVanOotegem谢谢Elias,非常感谢,干杯。
error_reporting(E_ALL);
ini_set('display_errors', 1);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$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)
{
// use while instead of foreach
// while ($row = mysqli_fetch_assoc($result)){
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";
}
}
catch (Exception $e)
{
echo $e->getMessage();
}