Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in

Joined
Sep 12, 2008
Messages
1,204
Reaction score
50
Code:

PHP:
<?php
	// 1. Create a connection
	$connection = mysql_connect("localhost", "root", "");
	
?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header.php"); ?>
	
	<div id="content">
	
		<table id="table">
			<tr>
				<td id="nav">
<?php
	// 3. Ask database question
$result = mysql_query("SELECT * FROM information", $connection);
	
?>
<?php
	// 4. Use Data
	while ($row = mysql_fetch_array($result)){
		echo $row[1]." ". $row[2]."<br />";
	}
	
?>
				</td>
			</tr>
		</table>
	</div>
<?php

	require("includes/footer.php");

?>
<?php
	// 5. Close Connection
	mysql_close($connection);
	
?>

I am new to PHP, I can't figure out what is wrong. I appreciate it if you can fix the problem.

Regards,
NTV
 
Simple, you haven't selected a database yet.

Also, before someone else jumps into this thread and starts screaming, I have to inform you that MySQL functions are deprecated and you should use MySQLi or PDO instead.

I see. I will get into MySQLi after I finish learning the basics, thank you for that suggestion.

But where would I put mysql_select_db() ? Do you mind changing the code?

EDIT:

This worked:
PHP:
<?php
	// 1. Create a connection
	$connection = mysql_connect("localhost", "root", "");
				  mysql_select_db ("xepk");
?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header.php"); ?>
	
	<div id="content">
	
		<table id="table">
			<tr>
				<td id="nav">
<?php
	// 3. Ask database question
$result = mysql_query("SELECT * FROM information", $connection);
	
?>
<?php
	// 4. Use Data
	while ($row = mysql_fetch_array($result)){
		echo $row[1]." ". $row[2]."<br />";
	}
	
?>
				</td>
			</tr>
		</table>
	</div>
<?php

	require("includes/footer.php");

?>
<?php
	// 5. Close Connection
	mysql_close($connection);
	
?>
 
Last edited:
Go ahead and switch to MySQLi. It makes these things easier. Also moved around your PHP code. There is no need to initiate the database connection until you're actually going to use it. Be sure to close it once you're finished as well.

PHP:
<div id="content">
    <table id="table">
        <tr>
            <td id="nav">
			<?php
			// Open database connection
			$connection = mysqli_connect("localhost", "root", "", "xepk") or die("DB connection failure");

			// Includes
			require_once("includes/functions.php"); // Not sure why you're including this, page doesn't seem to need it
			include("includes/header.php");

			// Query the database and run through results
			$result = $connection->query("SELECT * FROM information", $connection);
			while ($row = mysqli_fetch_array($result))
			{
			    echo $row[1]." ". $row[2]."<br />";
			}
			// Close database connection
			mysqli_close($connection);   
			?>
            </td>
        </tr>
    </table>
</div>
<?php require("includes/footer.php"); ?>
 
  • Like
Reactions: NTV
Go ahead and switch to MySQLi. It makes these things easier. Also moved around your PHP code. There is no need to initiate the database connection until you're actually going to use it. Be sure to close it once you're finished as well.

PHP:
<div id="content">
    <table id="table">
        <tr>
            <td id="nav">
            <?php
            // Open database connection
            $connection = mysqli_connect("localhost", "root", "", "xepk") or die("DB connection failure");

            // Includes
            require_once("includes/functions.php"); // Not sure why you're including this, page doesn't seem to need it
            include("includes/header.php");

            // Query the database and run through results
            $result = $connection->query("SELECT * FROM information", $connection);
            while ($row = mysqli_fetch_array($result))
            {
                echo $row[1]." ". $row[2]."<br />";
            }
            // Close database connection
            mysqli_close($connection);   
            ?>
            </td>
        </tr>
    </table>
</div>
<?php require("includes/footer.php"); ?>

To note out it's:

PHP:
$result = mysqli_query($connection, $sql);

or

PHP:
$result = $connection->query($sql);

Also, you placed his header inside the table tag, when it actually was above the content divider!
 
Back