为什么我的mysql

编程入门 行业动态 更新时间:2024-10-26 01:19:46
本文介绍了为什么我的mysql_query没有返回结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

本文发布于:2023-11-25 13:45:28,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:mysql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!