开发者

Why would a script not like using MySQLi but is fine with MySQL?

I'm having some issues using mysqli to execute a script with SELECT,DELETE,INSERT and UPDATE querys. They work when using norm mysql such as mysql_connect but im getting strange results when using mysqli. It works fine with a lot of the SELECT querys in other scripts but when it comes to some admin stuff it messes up.

开发者_运维技巧

Its difficult to explain without attaching the whole script.

This is the function for modifying...


function database_queryModify($sql,&$insertId)
  {
    global $databaseServer;
    global $databaseName;
    global $databaseUsername;
    global $databasePassword;
    global $databaseDebugMode;

    $link = @mysql_connect($databaseServer,$databaseUsername,$databasePassword);

    @mysql_select_db($databaseName,$link);

    $result = mysql_query($sql,$link);

    if (!$result && $databaseDebugMode)
    {
      print "[".$sql."][".mysql_error()."]";
    }

    $insertId = mysql_insert_id();

    return mysql_affected_rows();
  }

and heres what I changed it to for mysqli


function database_queryModify($sql,&$insertId)
  {
    global $databaseServer;
    global $databaseName;
    global $dbUser_feedadmin;
    global $dbUser_feedadmin_pw;
    global $databaseDebugMode;

    $link = @mysqli_connect($databaseServer,$dbUser_feedadmin,$dbUser_feedadmin_pw,$databaseName);
    $result = mysqli_query($link, $sql);

    if (!$result && $databaseDebugMode)
    {
      print "[".$sql."][".mysqli_error()."]";
    }
    $insertId = mysqli_insert_id();
    return mysqli_affected_rows();
  }

Does that look right?

It isn't actually producing an error but its not functioning in the same way as when using mysql. any ideas?


Here is the way I normally use Mysqli

$mysqli = new mysqli($myServer, $myUser, $myPass, $myDB);
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit;
} 
$result = $mysqli->query($query);

while ($row=$result->fetch_assoc()) {
  print_r($row);
}

Note: mysqli is a class, $mysqli is the variable that holds the instance of that class, query is a method of that class that returns a result class that has methods of its own.


You are running all statements at once as far as php is concerned, so functions like affected rows or last inserted id will not work as expected. (Correct me if I'm wrong I haven't been using mysqli for a while.)


some of the mySqli functions needed some tinkering such as including $link

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜