Results 1 to 5 of 5
Hybrid View
-
06-24-2005, 12:17 PM #1WHT Addict
- Join Date
- Sep 2002
- Posts
- 110
how can i display news on outside page?
Hi guys,
i am currently write a few lines code to display all news from mysql by date.
db is abc, and there is table called news.
here is the code:
PHP Code:<?php
$username = "xxx";
$password = "xxx";
$hostname = "localhost";
$db = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db("abc",$db)
or die("Could not select abc");
$result = mysql_query('SELECT * FROM news' . 'ORDER BY date DESC');
$news = array();
while ($result = $db->row_array()) {
$news[] = $result;
}
?>
i need help to show news on outside pages like anywhere in html or php pages.
Thanks guys!!!
sean
-
06-24-2005, 01:12 PM #2Newbie
- Join Date
- Jun 2005
- Posts
- 21
I don't understand how $db->row_array() is supposed to work.
But what example is the problem, "doesn't work" is a very vague description of it.
If the result is derived directly from mysql_query() then why not use the standard mysql_fetch_[array|assoc|row]() functions.
-
06-24-2005, 01:15 PM #3WHT Addict
- Join Date
- Sep 2002
- Posts
- 110
Originally posted by kuja
I don't understand how $db->row_array() is supposed to work.
But what example is the problem, "doesn't work" is a very vague description of it.
If the result is derived directly from mysql_query() then why not use the standard mysql_fetch_[array|assoc|row]() functions.
it shows this errors:
Fatal error: Call to a member function on a non-object in /home/xxx/public_html/index.php on line 78
so would you please tell me some detail info since i am beginner.
Thanks
sean
-
06-24-2005, 01:20 PM #4Newbie
- Join Date
- Jun 2005
- Posts
- 21
Your problem is with the line: $db = mysql_connect(...)
mysql_connect() does not return an object. It returns a resource that identifies a specific connection to the SQL server.
All you have to do is change '$result = $db->row_array()' to '$row = mysql_fetch_assoc($result)'
Within the loop, you may access fields through $row['fieldName']
Example:
PHP Code:<?php
// other stuff here
$db = mysql_connect(...);
mysql_select_db(..., $db);
$result = mysql_query(..., $db);
while ($row = mysql_fetch_assoc($result, $db)) {
echo "{$row['my_field_name_here']}<br>\n"; // output field data.
}
?>
-
06-24-2005, 01:34 PM #5WHT Addict
- Join Date
- Sep 2002
- Posts
- 110
Thank you so much. kuja
sean