Total Newbie PHPer Question

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
Riceman
Forum Newbie
Posts: 5
Joined: Wed Feb 22, 2012 7:40 pm

Total Newbie PHPer Question

Post by Riceman »

This is probably very simple for most of you, but I am new to PHP and am having trouble finding the answer, so here I am. Please don't laugh. :)

I have so far successfully connected to my DB, and I think successfully performed a SQL Query to my TABLE, and closed the DB.

What I am trying to do next is to add some COLUMN data from a particular ROW in a particular TABLE to my html5 page.

TABLE name is "PropForm"
ROW name, I guess we can go from the cf_id for a particular row, so say cf_id=1
COLUMN name is "company"

I am trying to get the "company" data into the area where it says "Company Name" in the html5 code below.

Code: Select all

<head>
	<!-- 348974 -->
	<title>Company Name</title>
	<meta charset="UTF-8">
here is my ROW in MySQL:
Row Data
Row Data
Screen Shot 2012-02-22 at 3.41.36 PM.png (18.73 KiB) Viewed 2672 times
Any help would be GREATLY appreciated.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Total Newbie PHPer Question

Post by Celauran »

Code: Select all

<?php
...
$query = "SELECT company FROM PropForm WHERE cf_id = 1";
$result = $sql->query($query)->fetchColumn();
...
<title><?php echo $result; ?></title>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Total Newbie PHPer Question

Post by RobertGonzalez »

Are you in a loop or have your read your result into its own variable? Are you using the PDO, MySQL or MySQLi extension? Have any code you can post to help us along a little?
Riceman
Forum Newbie
Posts: 5
Joined: Wed Feb 22, 2012 7:40 pm

Re: Total Newbie PHPer Question

Post by Riceman »

Thanks...but I am getting an Error:

Parse error: syntax error, unexpected '<' in /home/rems808/public_html/propertylistings/348974test.php on line 9

line 9 is the last line...

Code: Select all

	<?php
	$query = "SELECT company FROM PropForm WHERE cf_id = 1";
	$result = $sql->query($query)->fetchColumn();
	<title><?php echo $result; ?></title>
And, is it possible to only do the QUERY once for the whole page? I will be putting more information form the other columns in the rest of the page.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Total Newbie PHPer Question

Post by RobertGonzalez »

Because you never come out of PHP in that example.

Code: Select all

<?php
$query = "SELECT company FROM PropForm WHERE cf_id = 1";
$result = $sql->query($query)->fetchColumn();
?>
<title><?php echo $result; ?></title>
Riceman
Forum Newbie
Posts: 5
Joined: Wed Feb 22, 2012 7:40 pm

Re: Total Newbie PHPer Question

Post by Riceman »

OK,

Now I get this error, I am probably missing somethng simple:
Fatal error: Call to a member function query() on a non-object in /home/rems808/public_html/propertylistings/348974test.php on line 8

Line 8 is

Code: Select all

$result = $sql->query($query)->fetchColumn();
Here is all the code I have:

Code: Select all

<?php
// Connecting, selecting database
	$link = mysql_connect('localhost', 'XXXXX', 'XXXXX');
	if ($connection) {
		die('Could not connect' . mysql_error());
	}
	
	$db_select = mysql_select_db('XXXX',$link);
	if (!$db_select) {
		die('Could not connect' . mysql_error());
	}

?>

then this new code from the form:

Code: Select all

<?php
$query = "SELECT company FROM PropForm WHERE cf_id = 1";
$result = $sql->query($query)->fetchColumn();
?>
<title><?php echo $result; ?></title>
and this at the bottom of my page:

Code: Select all

<?php
// Closing connection
mysql_close($link);
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Total Newbie PHPer Question

Post by Celauran »

I used ellipses to show that other code may go in between. You showed none of your code, so I had nothing to work with. My $sql variable is a PDO object, so you may need to adjust your code accordingly.

Here's a more complete example, if that helps:

Code: Select all

<?php

$sql = new PDO('mysql:host=localhost;dbname=whatever', 'obfuscated', 'obviously');

$query = "SELECT company FROM PropForm WHERE cf_id = 1";
$result = $sql->query($query)->fetchColumn();

?>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
        <title>Debug</title>
    </head>
    <body>
        <?php echo $result; ?>
    </body>
</html>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Total Newbie PHPer Question

Post by Celauran »

Here, I redid it using mysql_ (you really shouldn't use those functions, by the by).

Code: Select all

<?php

$sql = mysql_connect('localhost', '****', '****');
mysql_select_db('whatever');

$query = "SELECT company FROM PropForm WHERE cf_id = 1";
list($company) = mysql_fetch_row(mysql_query($query));

?>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
        <title><?php echo $company; ?></title>
    </head>
    <body>
    </body>
</html>
Riceman
Forum Newbie
Posts: 5
Joined: Wed Feb 22, 2012 7:40 pm

Re: Total Newbie PHPer Question

Post by Riceman »

YES...Awesome. Thanks.

Now one more question. If I want to pull results from other columns in the same row, and echo them through the page, there are about 10 columns. Do I need to put the code below in everytime, or is there a way to only query the row once, and just echo the results?

Code: Select all

	<?php
	$query = "SELECT company FROM jos_chronoforms_PropertySubmissionMobile WHERE cf_id = 1";
	$result = $sql->query($query)->fetchColumn();
	?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Total Newbie PHPer Question

Post by Celauran »

Use a comma-separated list of columns in your query.

Code: Select all

SELECT company, foo, bar FROM PropForm WHERE whatever
You'll want to avoid fetchColumn() here, preferring instead fetch() or fetchAll()
Riceman
Forum Newbie
Posts: 5
Joined: Wed Feb 22, 2012 7:40 pm

Re: Total Newbie PHPer Question

Post by Riceman »

Thanks Celauran,
Really appreciate it.
Post Reply