Home > Backend Development > PHP Tutorial > PHP基础有关问题

PHP基础有关问题

WBOY
Release: 2016-06-13 13:21:22
Original
731 people have browsed it

PHP基础问题

PHP code
<!--

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

-->
<?php $conn = mysql_connect('localhost', 'root', '');
    if(!$conn)
    {
        echo "数据库连接失败".mysql_error();
        exit();
    }
    echo "数据库连接成功</br>";
    $sql = "show databases";  //查找某个库是否存在
    print("$sql");
    $sql_return = mysql_query($sql);
    echo "$sql_return ";
    while($row = mysql_fetch_array($sql_return))
    {
        for($i = 0; $i != count($row); ++$i)
        {
            printf("%s", $row[0]);
            echo "";
        }
    }
    //mysql_select_db('web', $conn);
    
?>
Copy after login


输出的结果是这样的:
PHP code
<!--

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

-->
数据库连接成功
show databases
Resource id #4 
information_schema
information_schema
mysql
mysql
performance_schema
performance_schema
test
test
web
web
Copy after login


????为什么,每个库名都输出了两遍,我也用print_r($row);执行过,结果如下:
PHP code
<!--

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

-->数据库连接成功
show databases
Resource id #4 
Array ( [0] => information_schema [Database] => information_schema ) Array ( [0] => mysql [Database] => mysql ) Array ( [0] => performance_schema [Database] => performance_schema ) Array ( [0] => test [Database] => test ) Array ( [0] => web [Database] => web )
Copy after login


------解决方案--------------------
mysql_fetch_array()返回的既有数字数组,又有关联数组。你可以换用 mysql_fetch_row()
------解决方案--------------------
http://cn2.php.net/manual/zh/function.mysql-fetch-array.php

你自己看看你这程序写的:

PHP code
        for($i = 0; $i != count($row); ++$i)
        {
            printf("%s", $row[0]);
            echo "";
        } <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template