SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
Thread: MySQl query mistake?
-
Apr 24, 2001, 06:07 #1
- Join Date
- Mar 2001
- Location
- the Netherlands
- Posts
- 519
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I have the following problem:
---
<?php
mysql_connect("localhost","username","passw")
or die ("unable to connect to database");
mysql_select_db("db_name")
or die ("unable to select database");
// Query
$sql = "SELECT * FROM medewerker";
$result = mysql_query($sql);
echo "$result";
?>
---
What I get on the page is:
---
Resource id #2
---
I don't understand why I won't show all the records I have in the database.
What am I doing wrong?
-
Apr 24, 2001, 07:28 #2
- Join Date
- Jun 2000
- Location
- Sydney, Australia
- Posts
- 3,798
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
According to your code, $result is in fact an int.
$result = mysql_query($sql);
mysql_query() returns an int. This int is a handle ( a resource identifier) to the actual result set. So your are missing the code required to extract the data from the result set. You can only extract one row at a time from the result set, so you need to do this within a loop to extract all the rows.
Below is an example of how to do this. I don't know what the names of the fields are in your table medewerker (sounds Deutsche?) so I'll assume there are three fields "foo", "bar" and "funk". Note my use of the function mysql_fetch_array($result). This returns an associative array that contains the next row of the result set.
PHP Code:<?php
mysql_connect("localhost","username","passw")
or die ("unable to connect to database");
mysql_select_db("db_name")
or die ("unable to select database");
$sql = "SELECT * FROM medewerker";
$result = mysql_query($sql);
while ( $row = mysql_fetch_array($result)) {
echo $row["foo"] . $row["bar"] . $row["funk"] . '<br>';
}
?>
-
Apr 25, 2001, 04:11 #3
- Join Date
- Mar 2001
- Location
- the Netherlands
- Posts
- 519
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
thanks a lot...
it was exactly what I needed to make some progress!
-
Apr 25, 2001, 06:39 #4
- Join Date
- Jul 2000
- Location
- Perth Australia
- Posts
- 1,717
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
or...
while ( $row = mysql_fetch_row($result)) {
echo "$row[0] $row[1]<br>";//etc//
}
only mention it as I am a mysql_fetch_row() freak ! - mysql_fetch_row is 'supposed' to be faster than mysql_fetch_array() - not that I have ever noticed the difference!
-
Apr 25, 2001, 06:57 #5
- Join Date
- Jun 2000
- Location
- Sydney, Australia
- Posts
- 3,798
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
or ...
PHP Code:while ( $row = mysql_fetch_object($result)) {
echo "$row->foo $row->bar <br>";//etc//
}
PHP Code:while ( $row = mysql_fetch_row($result)) {
extract($row);
echo "$foo $bar <br>";//etc//
}
Although I concede that technically mysql_fetch_row is supposed to be more efficient.
Bookmarks