error with output

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dotoho
Forum Newbie
Posts: 14
Joined: Tue Aug 08, 2006 5:12 am

error with output

Post by dotoho »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I wrote it but i have had error.
I want to output data  from a table in database on my web (database : quiz,table : question) 
This is mycode and message error :[size=150] Notice: Trying to get property of non-object in D:\htdocs\quiz\display.php on line 26[/size]

Code: Select all

<?php
@ $db = mysql_connect("localhost", "****","****","****") or die("No way");
mysql_select_db("quiz",$db) or die("Không ch?n co s? d? li?u &#273;u?c");

$query = " select * from question ";
$result = 	mysql_query($query);;
$num_result = $result->num_rows;
 
 for($i = 0;$i <$num_result;$i++)
 {
    $row = $result->fetch_assoc();
    echo '<p><strong>'.($i+1).'.1 : ';
    echo htmlspecialchars(stripslashes($row['title']));
    echo '</strong><br />a .';
    echo stripslashes($row['answer1']);
    echo '<br /> b .';
    echo stripslashes($row['answer2']);
    echo '</p>';
 }
 ?>
Everah | Please do not post your DB connection credentials.

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

it's because $result isn't an object... it's a resource. you would do mysql_num_rows($result) instead of $result->num_rows
dotoho
Forum Newbie
Posts: 14
Joined: Tue Aug 08, 2006 5:12 am

Post by dotoho »

The Ninja Space Goat wrote:it's because $result isn't an object... it's a resource. you would do mysql_num_rows($result) instead of $result->num_rows
U thinked that "$num_result = mysql_num_rows($result->num_rows);" ; i tried but i have had the same error and Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

dotoho wrote:
The Ninja Space Goat wrote:it's because $result isn't an object... it's a resource. you would do mysql_num_rows($result) instead of $result->num_rows
U thinked that "$num_result = mysql_num_rows($result->num_rows);" ; i tried but i have had the same error and Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in

Code: Select all

$num_result = mysql_num_rows($result);
dotoho
Forum Newbie
Posts: 14
Joined: Tue Aug 08, 2006 5:12 am

Post by dotoho »

when ic an use method fetch_assoc()???I read it on internet???
And data from database can be outputted by that code???
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

dotoho wrote:when ic an use method fetch_assoc()???I read it on internet???
And data from database can be outputted by that code???
Image
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

It looks like you've read somebody else's code who's written their own class.

I suggest you jet off to php.net and have a read over the documentation - specifically the functions you're trying to use ;)
dotoho
Forum Newbie
Posts: 14
Joined: Tue Aug 08, 2006 5:12 am

Post by dotoho »

The Ninja Space Goat wrote:
dotoho wrote:when ic an use method fetch_assoc()???I read it on internet???
And data from database can be outputted by that code???
Image
sorry !!!

@d11wtq : thanxxx much :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You might also benefit from reading a tutorial or two on PHP and MySQL. Doing what you want to do is easy, but the way you are doing it in your code in totally wrong.
dotoho
Forum Newbie
Posts: 14
Joined: Tue Aug 08, 2006 5:12 am

Post by dotoho »

I'm newbie in Php.And i am learning follow the book lonely.And so i can something helpful for me in this forum.

Thank for all advice
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

<?php
@$db = mysql_connect("localhost", "****","****","****") or die(mysql_error());
mysql_select_db("quiz",$db) or die(mysql_error());

$query = "select * from question";
$result = mysql_query($query) or die(mysql_error());
while( $row=mysql_fetch_array($result, MYSQL_ASSOC) ) {
	echo htmlentities($row['title']), "<br />\n";
} 
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I know it seems lengthy, but this is how I like to do it...

Code: Select all

<?php
/* Set up database connection details */
$dbhost = "*****";
$dbuser = "*****";
$dbpass = "*****";
$db = "mydb";

/* Hit the database server */
if (!$connect = mysql_connect($dbhost,$dbuser,$dbpass))
{
    die('Could not connect to the database server: ' . mysql_error());
}

/* Connect to the database */
if (!mysql_select_db($db,$connect))
{
    die('Could not connect to the database ' . $db . ': ' . mysql_error());
}

/* Send a query to the database */
$query = "SELECT * FROM `mytable` WHERE `field_name` = '$some_value_to_search_for'";

/* Test to see if the query returned successfully */
if (!$result = mysql_query($query))
{
    die('Could not execute the query: ' . mysql_error());
}

/* If there were results, loop them and assign vars */
if (mysql_num_rows($result))
{
    while ($row = mysql_fetch_array($result))
    {
        /* Set your vars here BASED ON YOUR DB FIELD NAMES */
        $db_field = $row['db_field'];
    }
}
?>
Post Reply