Home Backend Development PHP Tutorial 有人遇到mysqli的fetch_assoc()方法出错吗?解决方案

有人遇到mysqli的fetch_assoc()方法出错吗?解决方案

Jun 13, 2016 pm 01:38 PM
array gt mysqli quot

有人遇到mysqli的fetch_assoc()方法出错吗?
配置是phpnow5.1.6 :windowsXP + php5.2.14 + apache2.2.6 + MySQL5.1.50

在用mysqli类的fetch_assoc()时,Apache好像歇菜了。见如下代码:

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->while($row =$rs->fetch_assoc())
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['HomeTown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
Copy after login

IE提示无法显示页面,FF提示载入页面时到服务器的连接被重置。
如果把上面的代码去掉则恢复正常。
如果把fetch_assoc关联数组代换为fetch_row()的索引数组也没事,对应把$row['FirstName']换成$row[0]就可以。
但有时这样代码会变得不清晰,所以上来救助大家有无解决方法?!
我在网上google了很久,发现很少人遇到这样的问题,即使有,也只是书写错误什么的,不适合解决我这个问题。

------解决方案--------------------
使用 $rs->fetch_array 方法吧
------解决方案--------------------
用fetch_array()加上参数就好了:MYSQLI_ASSOC[关联], MYSQLI_NUM[数字], MYSQLI_BOTH[全部]
PHP code

<?php $mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query = "SELECT Name, CountryCode FROM City ORDER by ID LIMIT 3";
$result = $mysqli->query($query);

/* numeric array */
$row = $result->fetch_array(MYSQLI_NUM);
printf ("%s (%s)\n", $row[0], $row[1]);

/* associative array */
$row = $result->fetch_array(MYSQLI_ASSOC);
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);

/* associative and numeric array */
$row = $result->fetch_array(MYSQLI_BOTH);
printf ("%s (%s)\n", $row[0], $row["CountryCode"]);

/* free result set */
$result->close();

/* close connection */
$mysqli->close();
?>
<br><font color="#e78608">------解决方案--------------------</font><br>没有错误信息吗?<br>看样子是没有打开错误显示功能
<br><font color="#e78608">------解决方案--------------------</font><br>
那只能是你的关联字段没有和数据库中的对应上。<br>实在没办法,用预编译处理吧:<br>
Copy after login
PHP code

$sql = "SELECT mid,movName,movType,movStar,movImg,movPlay,director,producter FROM mov_film";
        $stmt = $this->mysqli->prepare($sql);
        $stmt->execute();
        $stmt->store_result();
         $stmt->bind_result($mid,$movName,$movType,$movStar,$movImg,$movPlay,$director,$producter);
        $rows = $stmt->affected_rows;
        if ($rows > 0){
            $arrMovie = array();
            while($stmt->fetch()){
                //将文件名从路径中取出并转换为相对路径
                $movImg = "../../images/".basename($movImg);
                //截取文件名
                $arrStr = explode(".",basename($movPlay));
                $movPlay = "../../videos/".$arrStr[0];

                $arr = array(
                    "mid" => $mid,
                    "movName" => $movName,
                    "movType" => $movType,
                    "movStar" => $movStar,
                    "movImg" => $movImg,
                    "movPlay" => $movPlay,
                    "director" => $director,
                    "producter" => $producter
                );
                array_push($arrMovie,$arr);
            }
            return $arrMovie;
        } <div class="clear">
                 
              
              
        
            </div>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Solution to PHP Fatal error: Call to undefined function mysqli_connect() Solution to PHP Fatal error: Call to undefined function mysqli_connect() Jun 23, 2023 am 09:40 AM

When writing web applications using PHP, a MySQL database is often used to store data. PHP provides a way to interact with the MySQL database called MySQLi. However, sometimes when using MySQLi, you will encounter an error message, as shown below: PHPFatalerror:Calltoundefinedfunctionmysqli_connect() This error message means that PHP cannot find my

What should I do if php cannot connect to mysqli? What should I do if php cannot connect to mysqli? Nov 09, 2022 am 10:07 AM

Solution to php unable to connect to mysqli: 1. Open the "php.ini" file; 2. Find "mysqli.reconnect"; 3. Change "mysqli.reconnect = OFF" to "mysqli.reconnect = on".

What is the running file of mysql What is the running file of mysql Apr 11, 2023 am 10:38 AM

The running file of mysql is mysqld; mysqld is an executable file, which represents the Mysql server program. Executing this file can directly start a server process; and mysqld_safe is a startup script, which will indirectly call mysqld and also start a monitor. process.

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

Sort array using Array.Sort function in C# Sort array using Array.Sort function in C# Nov 18, 2023 am 10:37 AM

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

PHP PDO vs. mysqli: compare and contrast PHP PDO vs. mysqli: compare and contrast Feb 19, 2024 pm 12:24 PM

PDOPDO is an object-oriented database access abstraction layer that provides a unified interface for PHP, allowing you to use the same code to interact with different databases (such as Mysql, postgresql, oracle). PDO hides the complexity of underlying database connections and simplifies database operations. Advantages and Disadvantages Advantages: Unified interface, supports multiple databases, simplifies database operations, reduces development difficulty, provides prepared statements, improves security, supports transaction processing Disadvantages: performance may be slightly lower than native extensions, relies on external libraries, may increase overhead, demo code uses PDO Connect to mysql database: $db=newPDO("mysql:host=localhost;dbnam

See all articles