为什么我的mysql_query没有返回结果? [英] Why is my mysql_query returning no result?
问题描述
echo $ row ['>'')编码正在与非法字符串偏移乐华']。 ' - '。 $行[ rowB中。 ' - '。 $行[ ROWC]。 ' - '。 $行[ rowd]。 ' - '。 $ row ['rowe']。'< br />';
$ connection $ mysqli_connect(localhost/ * hostname * /,
username/ * username * /,
密码/ *密码* /,
dbname/ *数据库名称* /);
try {
//设置查询并运行它...
$ result = mysqli_query($ connection,SELECT * FROM table);
if(mysqli_num_rows($ result)> 0)
){
foreach(mysqli_fetch_array($ result)as $ row){
echo $ row ['rowa']。 ' - '。 $行[ rowB中。 ' - '。 $行[ ROWC]。 ' - '。 $行[ rowd]。 ' - '。 $ row ['rowe']。'< br />';
}
} else {
echoQUERY is not OKAY;
} //关闭连接。
$ connection = null;
} catch(PDOException $ e){
echo $ e-> getMessage();
}
?>
用户名,密码,数据库和表名是不是实际名称用于代码中。
您正在将 mysqli
与PDO例外 try / catch
,另外在 try
块中还有一个额外的括号。
请参阅代码中的注释:(以及下面的固定代码)
($行= mysqli_fetch_assoc($结果)) foreach(mysqli_fetch_array($ result) )
$ b 使用
foreach(mysqli_fetch_array($ result)as $ row)
try {
//设置查询并且重复结果的行和只有每行的第一个字母。 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']。 ' - '。 $行[ rowB中。 ' - '。 $行[ ROWC]。 ' - '。 $行[ rowd]。 ' - '。 $ row ['rowe']。'< br />';
}
} else {
echoQUERY is not OKAY;
} //关闭连接。
$ connection = null;
} catch(PDOException $ e){//这应该是catch(Exception $ e)
echo $ e-> getMessage();
$ / code $ / $ p
更改为:
pre $
try {
//设置查询并运行它...
$ result = mysqli_query($ connection,SELECT * FROM`table` );
if(mysqli_num_rows($ result)> 0)
{
foreach(mysqli_fetch_array($ result)as $ row){
echo $ row ['rowa']。 ' - '。 $行[ rowB中。 ' - '。 $行[ ROWC]。 ' - '。 $行[ rowd]。 ' - '。 $ row ['rowe']。'< br />';
}
} else {
echoQUERY is not OKAY;
$ catch(Exception $ e)
{
echo $ e-> getMessage();
$ b 在文件顶部添加错误报告
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/ *数据库名称* /);
try {
//设置查询并运行它...
$ result = mysqli_query($ connection,SELECT * FROM`table`);
if(mysqli_num_rows($ result)> 0)
{
//使用while而不是foreach
// while($ row = mysqli_fetch_assoc($ result )){
foreach(mysqli_fetch_array($ result)as $ row){
echo $ row ['rowa']。 ' - '。 $行[ rowB中。 ' - '。 $行[ ROWC]。 ' - '。 $行[ rowd]。 ' - '。 $ row ['rowe']。'< br />';
}
} else {
echoQUERY is not OKAY;
$ catch(Exception $ e)
{
echo $ e-> getMessage();
}
The coding is comming up with Illegal string offset for:
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();
}
?>
The username, password, database and table name are not the actual names used in the code.
解决方案 You're mixing mysqli
with PDO exceptions try/catch
, plus there's an extra bracket in your try
block. Those two APIs do not mix.
See the comments in your code: (and the fixed code below that)
Sidenotes: You may want to change foreach(mysqli_fetch_array($result) as $row)
to while ($row = mysqli_fetch_assoc($result))
Using foreach(mysqli_fetch_array($result) as $row)
will produce repeated results of the rows and only the first letter from each 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();
}
Change it to this:
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";
}
}
catch (Exception $e)
{
echo $e->getMessage();
}
Add error reporting at the top of your file(s)
error_reporting(E_ALL);
ini_set('display_errors', 1);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
Rewrite:
<?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_query没有返回结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!