Log in

View Full Version : Display Error



Titan85
02-05-2007, 06:38 PM
Hello, I am attempting to make a page that will display all the jobs that have been done for a certain user and give them a link to download the files for each job. I have it mostly working, but ran into a small error. The error is that ecen though my sql query returns results, they are not displayed. Here is my code:
<?php

$user = $_SESSION['user'];

// Query to get client data
$sql = "SELECT * FROM `jobs` WHERE username = '$user' ORDER BY id DESC";
$result = mysql_query($sql) or die ('Error Getting Job Data! <br />' .mysql_error());

// Get number of results
$results = mysql_num_rows($result);

// Put data in array
$row = mysql_fetch_array($result);

// If no results found
if($results < 1) {
echo '
You have no files to download because no jobs have been done for you. Click <a href="http://echo-designes.com/contact.php">here</a> to have a job done.
';
}

// If there are results
if($results >= 1) {

echo '
<b>Select the files you would like to download.</b>
<br /><br />';

// For each result found
while($row = mysql_fetch_array($result)) {
extract($row);

echo '
<a href="'.$download.'">'.$title.'</a>
<br />';
}
}

?>When I take out the while statement and just use
echo '
<a href="'.$row['download'].'">'.$row['title'].'</a>
<br />';It works fine. But because I want there to be multiple links, I need to use the while statement. Any ideas?

EDIT: I removed the first $row = mysql_fetch_array($result); statement and it worked. But for another script, I need to get the data from the query before I run my while statement, any way to do that?

blm126
02-05-2007, 08:04 PM
For the second all you have to do is save your data in an array, and then reference back to it. Look at this example(based on the code you added above)


<?php

$user = $_SESSION['user'];

// Query to get client data
$sql = "SELECT * FROM `jobs` WHERE username = '$user' ORDER BY id DESC";
$result = mysql_query($sql) or die ('Error Getting Job Data! <br />' .mysql_error());

// Get number of results
$results = mysql_num_rows($result);

// If no results found
if($results < 1) {
echo '
You have no files to download because no jobs have been done for you. Click <a href="http://echo-designes.com/contact.php">here</a> to have a job done.
';
}

// If there are results
if($results >= 1) {
$rows = array();//Set up storage array
echo '
<b>Select the files you would like to download.</b>
<br /><br />';

// For each result found
while($row = mysql_fetch_array($result)) {
$len = count($rows);//get new index
$rows[$len]['download'] = $row['download'];//add data
$rows[$len]['title'] = $row['title'];//add data
extract($row);

echo '
<a href="'.$download.'">'.$title.'</a>
<br />';
}

}

?>

The $rows array will contain all the data you. Also, I wouldn't abuse extract() to much. It may be handy, but it reduces readability and I don't see any real benefit to it.

Titan85
02-06-2007, 03:13 PM
Well, It seems that I get an error every time I try to get an array from more than one sql query. For example, I run query 1 and then use: $row = mysql_fetch_array($result). Then I run query 2 and use: $row1 = mysql_fetch_array($result2) to put the data in the array. It seems that it does not work for the second array. Any idea why?

blm126
02-07-2007, 01:38 AM
There shouldn't be any problem with that(if I understand you right). Showing us the code will help us help you better.

mburt
02-07-2007, 01:42 AM
Shouldn't $results = mysql_num_rows($result);
be: $results = mysql_numrows($result);
?

thetestingsite
02-07-2007, 02:11 AM
Shouldn't $results = mysql_num_rows($result);
be: $results = mysql_numrows($result);
?

No, that's right. It should be mysql_num_rows. Sorry, just got into the thread and need to re-read the original post to get a better idea of what's going on.

Added Later: Post the code that you get an error in (in your second post). That will help out a lot.

Titan85
02-08-2007, 02:34 AM
Ok, I will be posting the code later tonight, I have to go through it again to remember what I was doing since I started some other stuff at the same time ;).

EDIT: Never mind, I figured out the problem, I had $row['$title'] instead of $row['title'], so it messed it all up. You gatta love stupid mistakes. Thanks for the help :)