Forum Moderators: coopster
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?
$data = mysql_fetch("SELECT * FROM userfield ORDER BY field55 ASC LIMIT 0,50;")
or die(mysql_error());
$data = mysql_query("SELECT * FROM userfield ORDER BY field55 ASC LIMIT 0,50;") or die(mysql_error());
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.
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
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.
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.