dstnlhhv791576 2011-10-19 19:43
浏览 30
已采纳

将特定技术从mysql转换为mysqli(php)

In mysql I use this code

$somenumber=5;
$result = mysql_query("SELECT * FROM some_table")
while($row = mysql_fetch_array($result)) {
echo $row['id'.$somenumber];
}

Please help me to do the same in the best way using mysqli, probably oop style I think cause everybody say best way to use oop way. Sorry I'm just studding this

  • 写回答

1条回答 默认 最新

  • douzhenggui8171 2011-10-19 19:47
    关注

    To do it with MySQLi OOP, you create an object, then call it's query() method.

    $mysqli = new mysqli('hostname','user','password','databasename');
    if (!$mysqli) {
      // connect failure, check connect_error()
      echo $mysqli->connect_error();
    }
    else {
      // Call query() to execute your SQL
      $result = $mysqli->query("SELECT * FROM some_table");
      if ($result) {
        // $result is an object of type mysqli_result
        // Call fetch_assoc() on $result
        while ($row = $result->fetch_assoc())
          echo $row['id'];
      }
    }
    

    If you have input parameters which you'll pass into your query, escape them with real_escape_string()

    $somevar = $mysqli->real_escape_string($somevar);
    $mysqli->query("SELECT * FROM sometable WHERE somecolumn='$somevar';");
    

    The principle benefits of MySQLi over the older mysql_* API are

    • The ability to use prepared statements instead of dynamic SQL strings
    • Object-oriented behavior which can be extended with your own classes.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?