我正在学习React + php backend + MySQL,遇到了这个问题:
我使用的网络主机使用MySQL 5.7版本。我的电脑使用MySQL 8.2。
代码我有问题:
$query = "SELECT * FROM `usuario` WHERE `login` = ?;";
$stmt = $conn->prepare($query);
$stmt->execute([$login]);
$result = $stmt->get_result();
$results = $result->fetch_assoc();
字符串
当我在localhost上运行后端mysqli->fetch_assistant()时,它会在我的查询中获得所有正确的值。但是,当我在WebHost上运行相同的代码时,它什么也不返回。
我已经做了一些测试,问题是在fetch_asktop + MySQL 5.7上。fetch_all()在这种情况下工作正常
我试着在MySQL 5.7和8.2之间使用fetch_all和fetch_as
编辑:
启用错误报告。没有什么新的。两个系统中的PHP版本都是8.2.13
编辑2:如果有人想尝试复制错误:
_install latest Wamp Server with MySQL 5.7 add-on.
_Clone Github repo: https://github.com/leyed0/testes
_Create a vitual host named 'testes' pointing to the folder 'backend' in the repository.
npm i
npm run start
型
在react应用程序运行时,显示浏览器控制台并单击按钮
编辑3:我是具体的.它返回什么都没有.没有错误,没有空,没有字符.当我登录到一个工作系统的控制台输出:
回波代码:
case 'GET':
echo json_encode(['testado', 'GET', $_SERVER, $results, $gene]);
break;
case 'POST':
echo json_encode(['testado', 'POST', $_SERVER, $results, $gene]);
break;
型
SQL 8的输出:
post or get
line with data
rest of the code
型
SQL 5的输出:
post or get
rest of the code
型
永远不会抛出任何错误
编辑4:所以,我做了vardump,它显示了有意义的代码。
var_dump($stmt):
<pre class='xdebug-var-dump' dir='ltr'>
<small>C:\Leyed\REPO\testes\backend\index.php:76:</small>
<b>object</b>(<i>mysqli_stmt</i>)[<i>2</i>]
<i>public</i> 'affected_rows' <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>1</font>
<i>public</i> 'insert_id' <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>0</font>
<i>public</i> 'num_rows' <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>0</font>
<i>public</i> 'param_count' <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>1</font>
<i>public</i> 'field_count' <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>14</font>
<i>public</i> 'errno' <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>0</font>
<i>public</i> 'error' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>''</font> <i>(length=0)</i>
<i>public</i> 'error_list' <font color='#888a85'>=></font>
<b>array</b> <i>(size=0)</i>
<i><font color='#888a85'>empty</font></i>
<i>public</i> 'sqlstate' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'00000'</font> <i>(length=5)</i>
<i>public</i> 'id' <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>1</font>
</pre>
型
var_dump($result):
<pre class='xdebug-var-dump' dir='ltr'>
<small>C:\Leyed\REPO\testes\backend\index.php:76:</small>
<b>object</b>(<i>mysqli_result</i>)[<i>3</i>]
<i>public</i> 'current_field' <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>0</font>
<i>public</i> 'field_count' <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>14</font>
<i>public</i> 'lengths' <font color='#888a85'>=></font> <font color='#3465a4'>null</font>
<i>public</i> 'num_rows' <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>1</font>
<i>public</i> 'type' <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>0</font>
</pre>
型
var_dump($results):
<pre class='xdebug-var-dump' dir='ltr'>
<small>C:\Leyed\REPO\testes\backend\index.php:76:</small>
<b>array</b> <i>(size=14)</i>
'ID' <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>2</font>
'Login' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'leyed'</font> <i>(length=5)</i>
'Senha' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'$2y$10$fm8FKZ2NfEO3YEkTmW8M6uHMIVCbhwvG8ICtg/hxJ1gSSCVWY5BAq'</font> <i>(length=60)</i>
'Nome' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>''</font> <i>(length=0)</i>
'CPF' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>''</font> <i>(length=0)</i>
'Genero' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>''</font> <i>(length=0)</i>
'Email' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'[email protected]'</font> <i>(length=17)</i>
'Telefone' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>''</font> <i>(length=0)</i>
'Nascimento' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'1994-08-12'</font> <i>(length=10)</i>
'Endere�o' <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>0</font>
'Criado' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'2023-12-18 22:24:10'</font> <i>(length=19)</i>
'UltLogin' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'2023-12-21 23:46:32'</font> <i>(length=19)</i>
'Desde' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'2023-12-18 22:24:10'</font> <i>(length=19)</i>
'Permisses' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>''</font> <i>(length=0)</i>
</pre>
型
我将在SQL 8版本(有效)中复制这一点,看看是否发现任何差异
1条答案
按热度按时间kkbh8khc1#
找到问题了!感谢@user3783243 - var_dump帮助我。
问题是SQL列名中使用了特殊字符(,<$,etc)。我是巴西人,在这里这些字符太常见了,有时我们忘记了它们是不标准的。MySQL 8很容易处理这些字符,但MySQL 5有一些问题,他们返回垃圾-这让php感到困惑。我用简单的字符替换了特殊字符,现在代码工作了。
也许设置一个不同的字符集或排序规则也可以,但我决定采用简单的方法,只更改所需的列