944,185 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 3713
  • PHP RSS
Jul 12th, 2005
0

Php code confusion. Not sure how to describe

Expand Post »
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

PHP Syntax (Toggle Plain Text)
  1. <?
  2. include('dbconnect.php');
  3. //Get the article title
  4. function article_title ()
  5. {
  6. $getarticletitlequery = "select title from article where id='$id'";
  7. $printtitle = mysql_query($getarticletitlequery);
  8. $fetchtitle = @mysql_fetch_object($printtitle);
  9. $fetchtitle = ucfirst(strtolower($fetchtitle));
  10. echo("$fetchtitle");
  11. }
  12. ?>

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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zeshon is offline Offline
1 posts
since Jul 2005
Jul 13th, 2005
0

Re: How to query MySQL database using PHP

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]
<?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";
}
?>
[/PHP]
Reputation Points: 36
Solved Threads: 6
Posting Whiz
Troy is offline Offline
354 posts
since Jun 2005
Jul 14th, 2005
0

Re: Php code confusion. Not sure how to describe

[php]
<?
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 ;
}
?>
[/php]
You forgot to use $variable-> (since you are using fetch_object).

or

[php]
<?
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 ;
}
?>
[/php]
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
RamiroS is offline Offline
57 posts
since Mar 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Problem with displaying data..
Next Thread in PHP Forum Timeline: How to manipulate PHP date?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC