Forum Moderators: coopster

Message Too Old, No Replies

Displaying a table from MySQL

         

digicrime

6:19 pm on Nov 2, 2007 (gmt 0)

10+ Year Member



I am new to mysql and php and I'm confused on how I can display my results in PHP

For example:

<?php
$dbhost = 'localhost';
$dbuser = 'user123';
$dbpass = 'password';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die
('Error connecting to mysql');

$dbname = 'database1234;
mysql_select_db($dbname);

$data = mysql_fetch("SELECT * FROM userfield ORDER BY field55 ASC LIMIT 0,50;")
or die(mysql_error());

$info = mysql_fetch_array( $data );

echo $data ;

mysql_close($conn);

?>

Getting an error, at first I was getting a Resource ID 3 error then I get invalid mysql_fetch I'm just completely lost lol I use mysql5 and apache2/php5 incase anyone wants to know. Could someone help?

PHP_Chimp

8:05 pm on Nov 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I assume that $dbname = 'database1234; is missing the final ' before the ; as part of you removing the actual name from the code for the forum.

$data = mysql_fetch("SELECT * FROM userfield ORDER BY field55 ASC LIMIT 0,50;")
or die(mysql_error());

Should be -

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

cameraman

8:06 pm on Nov 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



These threads in the library should also help:
Commonly used MySQL Commands [webmasterworld.com]
Basics of extracting data from MySQL using PHP [webmasterworld.com]

digicrime

11:52 am on Nov 3, 2007 (gmt 0)

10+ Year Member



PHP_Chimp yea forum issue or I left it off by accident one of the two but what you have is what I had before and got a Resource ID error

Resource id #3

Ill continue to toy with it see what I can do anyone else has any input that would be great

jatar_k

1:09 pm on Nov 3, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld digicrime,

you are getting the correct value for this

echo $data;

$data contains a resource id which is what is returned when you make a query. You need to get the actual data from the resource by using a function as you did here

$info = mysql_fetch_array( $data );

so now $ibfo should hold an array containing the first returned row, though if you echo it you should get 'Array' as output. You need to use an array output function like print_r. Try this

replace
echo $data;

with
echo '<pre>';
print_r($info);
echo '</pre>';

I use the pre tags around the print_r to make it more readable, it will still work without them, try both ways and see.

for outputting each row you will have to wrap your
$info = mysql_fetch_array( $data );
in a loop, like so

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

that should loop through your results and use print_r to dump each row individually.

You can then use array syntax to access each individual column from any given row. Inside your loop you would use something like

$info['column_name']

I don't know your column names to give you an actual example since you used * in your query.

play with that a bit and see if it helps.

digicrime

1:28 pm on Nov 3, 2007 (gmt 0)

10+ Year Member



Thank you very much that was helpful how I need to learn how to format the output

This is how it came out

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] =>
)

To learn formatting now is another trip

jatar_k

1:47 pm on Nov 3, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you can reference any of those individually by using the array syntax and adding them one by one to your while loop

such as your first field would be

while ($info = mysql_fetch_array($data)) {
echo '<p>user id is ' . $info['userid'];
echo '<br>and field 2 is equal to ' . $info['field2'];
}

if you add them in individually to start it makes it easier and you can see the changes.

digicrime

2:09 pm on Nov 3, 2007 (gmt 0)

10+ Year Member



Thank you for your help that was much appreciated. How could I correspond userid to username specifically? Like user id 11257 is username jackbronx for example so rather then outputing the actual ID how about the name? username and user are two seperate tables

jatar_k

2:22 pm on Nov 3, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you would need to work out a mysql query to get the data from both tables at the same time

though while you are learning that you can do a second query using userid from your first return to query again and get the username

also a useful process to learn

PHP_Chimp

2:25 pm on Nov 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As the username is in another table then you will need to use the id from this table in another query to select the user name.

something like -
"SELECT name FROM table WHERE id='".$data['userid']."';";
obviously change the table and name, in the above, for the correct names.

Im not sure if mysql supports SELECT statements from multiple tables, but you may be able to combine all of this into 1 select statement.

digicrime

2:57 pm on Nov 3, 2007 (gmt 0)

10+ Year Member



Something to re-learn selecting from multiple tables. I'll have to study this to figure it out. Thanks for your help.