Hey everyone! I am working on a project that involves querying a mysql database using php, and I am running into some issues. html form is in list_files.php and the query is in query.php .. i also attached my mySQL database which has 2 tables: only the file table is being used for this part of the project.
I just cant seem to get php to echo any results from the query.. i keep getting this syntax error-->
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\query.php on line 41
I am able to upload to the database just fine, but i am hitting a wall when it comes to the query.
This is what i have so far-->
query.php
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="informationdb"; // Database name
$tbl_name="file"; // Table name
// Connect to the database
$con = mysql_connect("$host", "$username", "$password", "$db_name");
if(!$con) {
die("MySQL connection failed: ". mysql_error());
}
$playerfname=$_POST["playerfname"];
$playerlname=$_POST["playerlname"];
$sport=$_POST["sport"];
$season=$_POST["season"];
$number=$_POST["number"];
//$playerfname=trim($playerfname);
//$playerlname=trim($playerlname);
//$sport=trim($sport);
//$season=trim($season);
//$number=trim($number);
$result = mysql_query("
SELECT *
FROM file
WHERE playerfname LIKE '%".$playerfname."%'
AND playerlname LIKE '%".$playerlname."%'
AND sport LIKE '%".$sport."%'
AND season LIKE '%".$season."%'
AND number LIKE '%".$number."%'
");
while($row = mysql_fetch_array($result))
{
echo $row['playerfname'] . " " . $row['playerlname'] . " " . $row['sport'] . " " . $row['season'] . " " . $row['number'];
echo "<br />";
}
mysql_close($con);
?>
list_files.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<title>ASU Sports Information</title>
<link rel='stylesheet' type='text/css' href='menu.css' />
<link rel='stylesheet' type='text/css' href='form.css' />
</head>
<body>
<div class="header">
<img src="BlockA.png" alt="BlockA" height="110px" />
<img src="Header.png" alt="Header" height="100px"/>
</div>
<div class="body">
<div class="link">
<ul class="nav red">
<li><a href="login_success.php">Home</a></li>
<li><a href="contact.html">Contact</a></li>
<li><a href="uploadform.php">Upload Photos</a></li>
<li><a href="list_files.php">Search Photos</a></li>
</ul>
</div>
<br /> <br /><br />
<div class="searchform">
<form action="query.php" id="loginform" method="post">
<p style="text-align:center">
<label>First Name:</label>
<input type="text" name="playerfname" size="30" /> <br>
<label>Last Name:</label>
<input type="text" name="playerlname" size="30" /> <br>
<label>Sport:</label>
<input type="text" name="sport" size="30" /> <br>
<label>Season:</label>
<input type="text" name="season" size="30" /> <br>
<label>Jersey Number:</label>
<input type="text" name="number" size="30" /> <br>
</p>
<p style="text-align:center"> <input id="submitbutton" type="submit" value="Search"/> </p>
</div>
</div>
</body>
</html>
<?php
//show files in database and download them
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="informationdb"; // Database name
$tbl_name="file"; // Table name
// Connect to the database
$dbLink = new mysqli("localhost", "$username", "$password", "$db_name");
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}
// Query for a list of all existing files
$sql = 'SELECT `id`, `name`, `mime`, `size`, `playerfname` FROM `file`';
$result = $dbLink->query($sql);
// Check if it was successfull
if($result) {
// Make sure there are some files in there
if($result->num_rows == 0) {
echo '<p>There are no files in the database</p>';
}
else {
// Print the top of a table
echo '<table width="100%">
<tr>
<td><b>Name</b></td>
<td><b>File Type</b></td>
<td><b>Size (bytes)</b></td>
<td><b>Player First Name</b></td>
<td><b> </b></td>
</tr>';
// Print each file
while($row = $result->fetch_assoc()) {
echo "
<tr>
<td>{$row['name']}</td>
<td>{$row['mime']}</td>
<td>{$row['size']}</td>
<td>{$row['playerfname']}</td>
<td><a href='get_file.php?id={$row['id']}'>Download</a></td>
</tr>";
}
// Close table
echo '</table>';
}
// Free the result
$result->free();
}
else
{
echo 'Error! SQL query failed:';
echo "<pre>{$dbLink->error}</pre>";
}
// Close the mysql connection
$dbLink->close();
?>
Database layout:
db name: informationdb
table: file
fields:
id name mime size data playerfname playerlname sport season number