Web Hosting Talk







View Full Version : how can i display news on outside page?


terran11355@
06-24-2005, 12:17 PM
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
$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;
}
?>

but this code seems doesn't work at all.

i need help to show news on outside pages like anywhere in html or php pages.

Thanks guys!!!

sean

kuja
06-24-2005, 01:12 PM
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.

terran11355@
06-24-2005, 01:15 PM
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.

Hi Kuja,


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

kuja
06-24-2005, 01:20 PM
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
// 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.
}
?>

terran11355@
06-24-2005, 01:34 PM
Thank you so much. kuja


sean