php - 更新sql查询不会通过php中的mysqli_query()更新记录

标签 php mysql sql mysqli

我通过mysqli_query()在php中执行update查询,但是update查询不更新记录在 php 脚本中,虽然相同的查询在 phpmyadmin 内运行时确实更新记录...为什么?查询结果为1,没有错误。 示例:

UPDATE `tarex_yhibu` SET `phone` = '7777777777' 
WHERE url = "http://protechautoglass.com/". 

脚本

<?php
$start=microtime(true);
include('../lib.php');
include('../lib/dbconnection.php'); 

echo '<hr>';
$db_handler = init_db();
echo 'db connection is ok!<br />';
// var_dump($db_handler);
try { 
    $records = getTableFields($db_handler, 'tarex_yhibu', ['id','url'], 'phone="" limit 1');
    // echo '<pre>' . print_r($records, true) . '</pre>';

} catch  (Exception $e) {
    echo 'exception: ',  $e->faultstring;
} 

// $res=get_web_page($urls[0]); 
$regex='/tel:([+\d]+)/'; 
echo '<table border=1><tr><th>Url</th><th>Phone</th><th>dump</th><tr>';
foreach($records as $record){
     $res=get_web_page($record['url']);
     preg_match($regex, $res['content'], $matches);
     echo '<tr><td>', $record['url'], '</td><td><b> ' , print_r($matches[1], true), '</b></td><td><b> ' , var_dump($matches[1] ) , '</b></td></tr>';
     settype($matches[1], 'string');
     $phone = (string)$matches[1];
     settype($record['url'], 'string');
     UpdateRecord($db_handler, 'tarex_yhibu', 'phone', $phone,  'url = "' . $record['url'] . '" ' );
}
echo '</tr></table>';
echo '<br >Script execution time  ' , round((microtime(true) - $start),  1), ' seconds.'; //value in seconds

dbconnection.php

    function UpdateRecord($db_handler, $tableName, $field, $value, $condition = '1 = 0' )// we set the false condition to prevent occasional update of all the records (when WHERE <condition> is missing)
{
    $query = "UPDATE `{$tableName}` SET `{$field}` = '{$value}' WHERE {$condition} ";
    $result = mysqli_query($db_handler, $query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysqli_error($db_handler)); 
    if (!$result) {
        echo '<br />Update query - Could not run query: ' . mysql_error();
        exit;
    } else 
        echo '<br />Result: ', $result;
        echo '<br />Query: ', $query;
        printf("<br />Affected rows (by UPDATE query): ", mysqli_affected_rows($db_handler), '</br>');
}

用户srv50213_igor的用户权限正常,参见截图:http://joxi.net/gmvjN1qsx3Xjbm

有什么问题吗?

更新

我已将 " 更改为 ' 并删除了反引号,但没有效果: http://joxi.ru/bmoOyo7IQygKmy

最佳答案

这应该在 PHP 中工作

$query = "UPDATE tarex_yhibu SET phone = '7777777777' WHERE url = 'http://protechautoglass.com/';";

只需删除反引号即可,PHP 不喜欢它们

关于php - 更新sql查询不会通过php中的mysqli_query()更新记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37607374/

相关文章:

php - 特殊字符插入数据库

sql - PostgreSQL 函数数字类型错误

c# - 按参数列表过滤 SQL 查询

php - 使用迁移添加约束 - yii 框架

python - sqlalchemy 总是返回相同的数据

php - 从 htaccess 中删除 php 扩展,但保持 PATH_INFO 功能

php - 尝试在 <select> 中显示数据,但提交时数据库中没有显示任何内容

sql - 数据库审计追踪并重建以前的结果

php - .htaccess Url Rewrite 删除查询字符串键

php - 提交 html 表单,无需刷新或 jquery