3

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.

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>
2

4 Answers 4

0

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

replace

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

with

$result = mysql_query($sql, $conn);
Sign up to request clarification or add additional context in comments.

Comments

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.

<?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)) );
}

Comments

0

you can use mysqli extension

<?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);
?>

1 Comment

Are mysqli extensions supported in PHP 4.4.4? That is what is currently running on the server I'm using.
0

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

<?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
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.