简体   繁体   English

PHP不显示MYSQL结果

[英]PHP not displaying MYSQL result

I created a script on a windows platform which connects to the mysql database and returns the results of a table. 我在Windows平台上创建了一个脚本,该脚本连接到mysql数据库并返回表的结果。 A very basic script which I wrote to simply test my connection worked. 我写的一个非常基本的脚本只是用来测试我的连接是否有效。 The script works fine on my windows machine but not on my new mac. 该脚本在Windows计算机上运行正常,但在新的Mac上运行不正常。 On the mac it simply does not display any records at all. 在Mac上,它根本不显示任何记录。

I know that the database connection has been established because there is no error but I can not see why the result set is not being displayed on screen, as I said it worked fine on my windows machine. 我知道已经建立了数据库连接,因为没有错误,但是我看不到为什么结果集没有显示在屏幕上,因为我说它在Windows计算机上运行良好。

The Mac has mysql (with data) and apache running for php. Mac有mysql(带有数据)和apache为php运行。

Please could someone help as I have no idea what to do now? 请问有人可以帮忙,因为我不知道该怎么办?

Script below: 脚本如下:

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'test';

mysql_select_db($dbname);

mysql_select_db("test", $conn);

$result = mysql_query("SELECT * FROM new_table");

while($row = mysql_fetch_array($result))
  {
  echo $row['test1'] . " " . $row['test2'] . " " . $row['test3'];
  echo "<br />";
  }

mysql_close($con);

There are various things you could do to debug this. 您可以执行各种操作来调试它。

  • Show all PHP errors. 显示所有PHP错误。

     ini_set('display_errors','On'); ini_set('error_reporting',E_ALL); 
  • Catch all possible MySQL errors, not only the ones concerning whether you connected successfully. 捕获所有可能的MySQL错误,不仅捕获与您是否连接成功有关的错误。

     mysql_select_db("test", $conn) or die(mysql_error()); mysql_select_db($dbname) or die(mysql_error()); $result = mysql_query("SELECT * FROM new_table") or die(mysql_error()); 

Side note : There's no reason to select which database you wish to use twice. 旁注 :没有理由选择两次使用哪个数据库。

Its very difficult to see what is wrong ... so add some basic error checking, like changing this 很难找出问题所在……因此添加一些基本的错误检查,例如更改此内容

$result = mysql_query("SELECT * FROM new_table");

to

$result = mysql_query("SELECT * FROM new_table") or die(mysql_error());

This will show you the error you are getting from your query (if there is one) .. you ill see in the documentation for mysql_query that it returns a boolean if there was an error 这将向您显示您从查询中得到的错误(如果有一个错误)..您在mysql_query文档中看不到如果发生错误则返回boolean

Also note that you have a mistake in the variable name for closing the MySQL Connection : 还要注意,您在关闭MySQL连接的变量名中有一个错误:

mysql_close($con);

should be 应该

mysql_close($conn);

Check to see if the SELECT query was successful or not before fetching the rows. 在获取行之前,请检查SELECT查询是否成功。

<?php
$result = mysql_query("SELECT * FROM new_table");
if(!$result)
     die('SQL query failed: ' . mysql_error());

The only thing i can think of is that Mac file system is case sensitive while windows isn't so it might be that you mispelled the name of the table. 我唯一能想到的是Mac文件系统区分大小写,而Windows不区分大小写,因此可能是因为您拼写了表名。 In any cas you should do 在任何情况下,您都应该这样做

$result = mysql_query("SELECT * FROM new_table") or die("error:".mysql_error());

to view the error 查看错误

I think you should use the improved PHP mysql driver 我认为您应该使用改进的PHP mysql驱动程序

        try
        {

            $db = new mysqli("localhost","root","root","test");

            if ($db->connect_errno) {
                throw new Exception($db->connect_error);
            }

            if ($result = $db->query("SELECT * FROM new_table")) {
                printf("Select returned %d rows.\n", $result->num_rows);


                while($row = $result->fetchAssoc())
                  {
                    echo $row['test1'] . " " . $row['test2'] . " " . $row['test3'];
                    echo "<br />";
                  }


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


            $db->close();   
        }
        catch(Exception $e)
        {
                printf("Database Error: %s\n", $e->getMessage());
                exit();

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM