Welcome to WebmasterWorld Guest from 54.196.232.162

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

List results as they happen

     
2:53 pm on Jun 29, 2006 (gmt 0)

Junior Member

5+ Year Member

joined:May 18, 2006
posts:68
votes: 0


Is it possible to list UPDATE or INSERT results as they are entered into the database with PHP, rather than echoing out the whole thing at the end.
Example

Added product one....
Added product two....
Added product three...

As opposed to:
Your products have been updated and we have added
product one....
product two....
product three...

Is javascript required?

3:27 pm on June 29, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


I guess it would depend on whether or not your are executing multiple queries. If so, then you can echo the progress out after each successful query. If not, then echoing the progress at the end seems best. I wouldn't go out of your way to achieve this, though; you may lose some efficiency. In the end, it will pretty much look the same to the viewer.

Good luck!

5:16 pm on June 29, 2006 (gmt 0)

Junior Member

5+ Year Member

joined:May 18, 2006
posts:68
votes: 0


Thanks for replying.
Its just on a long query the user would have to sit there and wait until the whole thing has completed.
For me, from a usability point of view this is not a good thing as the user should be informed of what is going on as it happens. It also makes identifying problems easier, say with the import of a csv into mysql.

Not sure if I understand you correctly regarding echoing multiple queries out one at a time as I have never been able to achieve this when the queries are within a single page?

Any pointers would be much appreciated.

6:51 pm on June 29, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


>>It also makes identifying problems easier
For this, I would suggest using mysql_error [us2.php.net] to identify any problems. If the query returns NULL, then you can account for that too.
8:01 pm on June 29, 2006 (gmt 0)

Junior Member

5+ Year Member

joined:May 18, 2006
posts:68
votes: 0


What I meant was that if the results of a INSERT were echoed to the screen as they happened for say a 700 row csv import it would be very easy to see where the error had occured.
Mysql_error as a form of trapping is ok if you build your own error reporting system, but can mean absolutely nothing to an end user of system.

Does anyone know a way of echoing results as they are inputted?

4:10 am on June 30, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


If you post the code specifically for the db insert than I would be able to help you a little more. I'm still unsure whether or not you are inserting everything at once or individually. Post some code and we'll get it working one way or another ;)
9:33 pm on July 1, 2006 (gmt 0)

Junior Member

5+ Year Member

joined:May 18, 2006
posts:68
votes: 0


Not sure if this is going to help? but here you go.

Query within a for loop:

for($i=1; $i<count($contents); ) {

$query = "UPDATE products SET price = $price WHERE model = '$checkmodel'";
mysql_query($query) or die(mysql_error());
echo 'Updated Price of Model: <strong>'. $checkmodel.'</strong> To <strong>'.$price.'</strong><br>';

$i++;
}

4:51 pm on July 4, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


$price and $checkmodel have to be changed within the loop in order for mulitiple records to be updated.
8:49 am on July 5, 2006 (gmt 0)

Junior Member

5+ Year Member

joined:May 18, 2006
posts:68
votes: 0


Sorry but we are going a bit off topic here.
I purposely omitted code after the for loop because it is not relevant to the question?

Anyone know a way of echoing results to the screen as they are inputted to the database?

Thanks

11:00 am on July 5, 2006 (gmt 0)

Preferred Member

10+ Year Member

joined:May 4, 2004
posts:489
votes: 0


I put the following function into scripts that I want to have output as they go.
function flush_echo($string="")
{
echo $string;
ob_flush();
flush();
}

And then call is after the update - flush_echo('Table 123 updated...');
2:36 pm on July 5, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


What you have will work to show what stage you are in, but it will not work correctly if one of the queries do not successfully execute. For this, I would do something like:

$count = count($content);
for($i=1; $i<$count; $i++) {

$query = "UPDATE products SET price = $price WHERE model = '$checkmodel'";
$update = mysql_query($query) or die(mysql_error());
if($update) {
echo 'Updated Price of Model: <strong>'. $checkmodel.'</strong> To <strong>'.$price.'</strong><br>';
}
else {
echo '<b>DID NOT UPDATE</b>';
}
//change $price and checkmodel here
}


For other type of SQL statements, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

Good luck. Sorry about my last post ;)