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!
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]
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]
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
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
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.
<?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'];
}
}
?>