Page 1 of 1

error with output

Posted: Tue Aug 29, 2006 4:30 pm
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]

Posted: Tue Aug 29, 2006 4:33 pm
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

Posted: Tue Aug 29, 2006 4:55 pm
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

Posted: Tue Aug 29, 2006 4:58 pm
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);

Posted: Tue Aug 29, 2006 5:14 pm
by dotoho
when ic an use method fetch_assoc()???I read it on internet???
And data from database can be outputted by that code???

Posted: Tue Aug 29, 2006 5:22 pm
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

Posted: Tue Aug 29, 2006 5:29 pm
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 ;)

Posted: Tue Aug 29, 2006 5:33 pm
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 :)

Posted: Tue Aug 29, 2006 6:56 pm
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.

Posted: Wed Aug 30, 2006 6:25 am
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

Posted: Wed Aug 30, 2006 6:44 am
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";
} 
?>

Posted: Wed Aug 30, 2006 8:40 am
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'];
    }
}
?>