dougaimian1143 2012-05-28 22:15
浏览 84
已采纳

将Mysql转换为Mysqli

I was told to stop using MySQL and now learning MySQLi. However I ran into a problem using fetch array and don't know what I did wrong.

//Connect to db
include "mysqli_connect.php";

// Construct our join query
$sql = "SELECT userID, username, lastlogin FROM users";

//Crate results
$result = mysqli_query($link, $sql);

// Print out the contents of each row into a table 
$row = mysqli_fetch_array($result, MYSQLI_BOTH);

// Free result set
mysqli_free_result($result);

// Close connection
mysqli_close($link);


Messages I see:
  • [Connection Successful] Host info: db413417616.db.1and1.com via TCP/IP
  • Warning: mysqli_query() [function.mysqli-query]: Couldn't fetch mysqli in /homepages/9/d413002686/htdocs/maintenance/testsession.php on line 9
  • Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /homepages/9/d413002686/htdocs/maintenance/testsession.php on line 12
  • Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, null given in /homepages/9/d413002686/htdocs/maintenance/testsession.php on line 15
  • Warning: mysqli_close() [function.mysqli-close]: Couldn't fetch mysqli in /homepages/9/d413002686/htdocs/maintenance/testsession.php on line 18 ()

I used this to for mysqli_connect.php: http://www.php.net/manual/en/mysqli.construct.php

<?php
// $link (host, username, password, database)
$link = mysqli_connect('host', 'username', 'password', 'database');

//If connection is successful, otherwise show error message. 
if (!$link) {
    die('[Connect Fail] Error: (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
}
// Echo success message
echo '[Connection Successful] Host info: ' . mysqli_get_host_info($link) . "
";

// Close the link
mysqli_close($link);
?>
  • 写回答

1条回答 默认 最新

  • duanlanzhi5509 2012-05-28 22:51
    关注

    This line in your include file should be removed:

    // Close the link
    mysqli_close($link);
    

    You're closing $link, a.k.a. your connection to the database, and then trying to use it in your query. You only want to close it when you're all done with it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化