当表有数据时,PHP MySQL查询返回零结果

时间:2021-11-08 01:08:47

My PHP query is returning zero results when the table has 10 rows. What is actually displayed is a portion of the PHP table formatting code.

当表有10行时,我的PHP查询返回零结果。实际显示的是PHP表格式代码的一部分。

ACTUAL OUTPUT:

0) { // output data of each row while($row = mysql_fetch_assoc($result)) { echo "in: " . $row["in"]. " - Name: " . $row["Name"]. " - Email: " . $row["Email"]. "
"; } } else { echo "0 re
sults"; } mysql_close($conn);? 

HTML/PHP CODE:

<div class="about-text">

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db";

// Create connection
$conn = mysql_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysql_connect_error());
}

$sql = "SELECT * FROM tablename";
$result = mysql_query($conn, $sql);

if (mysql_num_rows($result) > 0) {
    // output data of each row
    while($row = mysql_fetch_assoc($result)) {
        echo "in: " . $row["in"]. " - Name: " . $row["Name"]. " - Email: " . $row["Email"]. "<br>";
    }
} else {
    echo "0 results";
}

mysql_close($conn);
?>

</div>

4 个解决方案

#1


1  

http://php.net/manual/en/function.mysql-query.php

replace

$result = mysql_query($conn, $sql);

with

$result = mysql_query($sql, $conn);

#2


0  

Passing the connection resource/instance as the first parameter is the style of the mysqli extension, which you should use, instead of passing the resource as the second parameter (optional) - which is how the old and now deprecated mysql_* extension does it. mysql_* will be removed with the upcoming php version 7.

将连接资源/实例作为第一个参数传递是mysqli扩展的样式,您应该使用它,而不是将资源作为第二个参数(可选)传递 - 这是旧的和现在已弃用的mysql_ *扩展的功能。即将推出的php版本7将删除mysql_ *。

<?php
$mysqli = mysqli_connect('localhost', 'root', '', 'db');
if ($mysqli->connect_errno) {
    die("Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error);
}

$sql = "SELECT `in`,`Name`,`Email` FROM tablename";
$result = mysqli_query($mysqli, $sql);
if ( !$result ) {
    die('query failed: '.$mysqli->error);
}

$row = mysqli_fetch_array($result);
if ( !$row ) {
    echo '0 results';
}
else {
    do {
        echo "in: " . $row["in"]. " - Name: " . $row["Name"]. " - Email: " . $row["Email"]. "<br>\r\n";
    }
    while( NULL!=($row=mysqli_fetch_array($result)) );
}

#3


0  

you can use mysqli extension

你可以使用mysqli扩展

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "your_db";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM mytable";
$result = mysqli_query( $conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo "in: " . $row["id"]. " - Name: " . $row["Name"]. " - Email: " . $row["Email"]. "<br>";
    }
} else {
    echo "0 results";
}
mysqli_close($conn);
?>

#4


0  

Try this code. This is your confic.php file:

试试这个代码。这是你的confic.php文件:

<?php
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "database_name";

$connection = mysql_connect($hostname, $username, $password, $dbname);

if (!$connection) {

    die('Could not connect: ' . mysql_error());
}

And make new php file and include your config file:

并制作新的PHP文件并包含您的配置文件:

<?php
include('config.php');
$sql = "SELECT * FROM table_name";
$result = mysql_query($sql);

if (mysql_num_rows($result) > 0) {

    while($row = mysql_fetch_assoc($result)) {

        ?>
            <div class="about-text">
                <?php
                    echo "In: ".$row["in"]."Name: ". $row['Name'].","."Email: ".$row['Email']."</br>";
                ?>
            </div>
        <?php
    }
}else{
    echo "There is no Result!!!!";
}
mysql_close($connection);

i think this will help you.

我想这会对你有所帮助。

#1


1  

http://php.net/manual/en/function.mysql-query.php

replace

$result = mysql_query($conn, $sql);

with

$result = mysql_query($sql, $conn);

#2


0  

Passing the connection resource/instance as the first parameter is the style of the mysqli extension, which you should use, instead of passing the resource as the second parameter (optional) - which is how the old and now deprecated mysql_* extension does it. mysql_* will be removed with the upcoming php version 7.

将连接资源/实例作为第一个参数传递是mysqli扩展的样式,您应该使用它,而不是将资源作为第二个参数(可选)传递 - 这是旧的和现在已弃用的mysql_ *扩展的功能。即将推出的php版本7将删除mysql_ *。

<?php
$mysqli = mysqli_connect('localhost', 'root', '', 'db');
if ($mysqli->connect_errno) {
    die("Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error);
}

$sql = "SELECT `in`,`Name`,`Email` FROM tablename";
$result = mysqli_query($mysqli, $sql);
if ( !$result ) {
    die('query failed: '.$mysqli->error);
}

$row = mysqli_fetch_array($result);
if ( !$row ) {
    echo '0 results';
}
else {
    do {
        echo "in: " . $row["in"]. " - Name: " . $row["Name"]. " - Email: " . $row["Email"]. "<br>\r\n";
    }
    while( NULL!=($row=mysqli_fetch_array($result)) );
}

#3


0  

you can use mysqli extension

你可以使用mysqli扩展

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "your_db";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM mytable";
$result = mysqli_query( $conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo "in: " . $row["id"]. " - Name: " . $row["Name"]. " - Email: " . $row["Email"]. "<br>";
    }
} else {
    echo "0 results";
}
mysqli_close($conn);
?>

#4


0  

Try this code. This is your confic.php file:

试试这个代码。这是你的confic.php文件:

<?php
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "database_name";

$connection = mysql_connect($hostname, $username, $password, $dbname);

if (!$connection) {

    die('Could not connect: ' . mysql_error());
}

And make new php file and include your config file:

并制作新的PHP文件并包含您的配置文件:

<?php
include('config.php');
$sql = "SELECT * FROM table_name";
$result = mysql_query($sql);

if (mysql_num_rows($result) > 0) {

    while($row = mysql_fetch_assoc($result)) {

        ?>
            <div class="about-text">
                <?php
                    echo "In: ".$row["in"]."Name: ". $row['Name'].","."Email: ".$row['Email']."</br>";
                ?>
            </div>
        <?php
    }
}else{
    echo "There is no Result!!!!";
}
mysql_close($connection);

i think this will help you.

我想这会对你有所帮助。