...

View Full Version : Print out results from a MySQL Query



digicrime
11-03-2007, 02:44 PM
Rather new to php so I'm trying to learn how to print out some results. The column I want to query has 24,113 rows so it has a lot of data but I want to sort it via a specific field so Im trying this. Sorting I know ASC DSC but I will learn how to do this another way just want to get past the basic part.



<?
$dbhost = 'localhost';
$dbuser = 'zackman_;
$dbpass = '1083';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = 'zackman_';
mysql_select_db($dbname);
$data = mysql_query("SELECT * FROM userfield ORDER BY field55 LIMIT 0,20;") or die(mysql_error());

echo "$data";
mysql_close($conn);
?>


Problem is I am getting a Resource ID 3 error which I am not familar with. I realize there is a lot of data to print so I want to learn how to to format it to look a little more nicer when it is viewed on the web which Ill figure out eventually but I can't get past this part. I have MySQL5 and apache2/php5 this is my own server so I can configure it any way I like in the event the problem is more then just a coding/syntax error

abduraooft
11-03-2007, 03:00 PM
A similar problem is in MySQL forum http://www.codingforums.com/showthread.php?t=127034

digicrime
11-03-2007, 03:06 PM
Ive read the manual and still dont really get it, at best I im guessing I need to define an array?

abduraooft
11-03-2007, 03:09 PM
Scroll down to see some examples


$data = mysql_query("SELECT * FROM userfield ORDER BY field55 LIMIT 0,20;") or die(mysql_error());
while ($row = mysql_fetch_assoc($data)) {
echo $row['firstname'];
echo $row['lastname'];
echo $row['address'];
echo $row['age'];
}

digicrime
11-03-2007, 03:30 PM
I changed it to



$data = mysql_query("SELECT * FROM userfield ORDER BY field55 ASC LIMIT 0,20;") or die(mysql_error());
$info = mysql_fetch_array( $data );

while ($info = mysql_fetch_array($data)) {
echo '<p><pre>';
print_r($info);
echo '</pre>';
}



Result

Array
(
[0] => 11257
[userid] => 11257
[1] =>
[field1] =>
[2] => Michigan
[field2] => Michigan
[3] =>
[field3] =>
[4] =>
[field4] =>
[5] => 93 Z28 a4
[field5] => 93 Z28 a4
[6] =>
[field6] =>
[7] =>
[field50] =>
[8] =>
[field51] =>
[9] =>
[field52] =>
[10] =>
[field53] =>
[11] =>
[field54] =>
[12] =>
[field55] =>
[13] =>
[field56] =>
[14] =>
[field57] =>
[15] =>
[field58] =>
[16] =>
[field59] =>
[17] =>
[field60] =>
[18] =>
[field61] =>
[19] =>
[field62] =>
[20] =>
[field63] =>
[21] =>
[temp] =>
)



Array

And that worked Now I need to learn how to format it nicely where could I read up on that stuff. Basically Ill need to set this information to print horizontally rather then vertically

abduraooft
11-03-2007, 05:02 PM
Instead of print_r(), just print your values of fields like

echo $info['name_field1'];
echo '<br/>'.$info['name_field2']; (use your table'd filed names.)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum