Member Avatar for zeshon

Ok, I am fairly new to php, so please bear with me if I have done something stupid.

I am trying to write a CMS, right now I am writing a piece of code to get the article title from the database. I used

<?
include('dbconnect.php');
//Get the article title
    function article_title ()
{
$getarticletitlequery = "select title from article where id='$id'";
$printtitle = mysql_query($getarticletitlequery);
$fetchtitle = @mysql_fetch_object($printtitle);
$fetchtitle = ucfirst(strtolower($fetchtitle));
echo("$fetchtitle");
}
?>

to call the database, and when I do, nothing comes up. When I tell it to pull from printtitle, I get recourse id #4. There are things in the database. Any help would be great.

It is easier for me to simply post a good, working code example rather than debug yours, so here is a simple example of how to connect and query data from a MySQL database using PHP.

<?php 
$db_server = "localhost"; 
$db_user = "root"; //Change if your username is not 'root'.
$db_pwd = ""; //Enter your password here. 
$db_db = "mysql"; //Change to the database name you want to use.

if (!$cnn = mysql_connect($db_server, $db_user, $db_pwd)) { 
  echo mysql_error(); 
  exit(); 
} 
if (!mysql_select_db($db_db, $cnn)) { 
  echo mysql_error(); 
  exit(); 
} 

$sql = "select help_category_id, name from help_category"; 
if (!$res = mysql_query($sql, $cnn)) { 
  echo mysql_error(); 
  exit(); 
} 
while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) { 
  echo "<br>".$row['name']." (".$row['help_category_id'].")\n";
}
?>
<?
include('dbconnect.php');
//Get the article title
    function article_title ()
{
$getarticletitlequery = "select title from article where id='$id'";
$printtitle = mysql_query($getarticletitlequery);
$fetchtitle_tmp = @mysql_fetch_object($printtitle);
$fetchtitle = ucfirst(strtolower($data->$fetchtitle));
echo $fetchtitle ;
}
?>

You forgot to use $variable-> (since you are using fetch_object).

or

<?
include('dbconnect.php');
//Get the article title
    function article_title ()
{
$getarticletitlequery = "select title from article where id='$id'";
$printtitle = mysql_query($getarticletitlequery);
$fetchtitle = ucfirst(strtolower(mysql_result($printtitle,0,"title")));
echo $fetchtitle ;
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.