如何在mySQL PHP上获取更新的行数

时间:2017-05-29 08:44:10

标签: sql php-mysqlidb

我正在使用PHP 5.2.17,以及这个mySQL连接:

<?php
function getdbcon()
{
  $db_user = "user"; 
  $db_password = "pw";
  $db_name = "dbname"; 
  $db_host = "host.com"; 

  //connecting to mysql database
  $con = new mysqli($db_host, $db_user, $db_password, $db_name);

  //prints error message if connection is not successful
  if ($con -> connect_error > 0) 
  {
    die('Unable to connect to database [' . $con -> connect_error . ']');
  }
  else
  {
    //set names utf8
    $con->query("set names utf8");
    return $con;
  }
}
?>

这是我的UPDATE行:

$conn = getdbcon();

$sql = "UPDATE Games SET Game_1 = '14' WHERE Games.Index = '194'";
if ($conn->query($sql) === TRUE) 
{
    echo mysql_affected_rows();
    //   ^this does not work      
}
$conn->close();

我得到: 警告:mysql_affected_rows()[function.mysql-affected-rows]:无法建立指向服务器的链接

但如果我尝试使用mySQLi,它确实有效:

<?php
$link = mysqli_connect("host.com", "user", "pw", "dbname");

if (!$link) {
    printf("Can't connect to localhost. Error: %s\n", mysqli_connect_error());
    exit();
}

mysqli_query($link, "UPDATE Games SET Game_1 = (14) WHERE Games.Index = '194'");
printf("Affected rows (UPDATE): %d\n", mysqli_affected_rows($link));

mysqli_close($link);
?>

我是否应该使用mysqli和mysql的混合来获取更新的行数?因为这看起来不对我,虽然它有效。 或者 - 是否有另一种方法可以使用mySQL获取更新的行数?

谢谢。

0 个答案:

没有答案