Page 1 of 1
Total Newbie PHPer Question
Posted: Wed Feb 22, 2012 7:56 pm
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
- Screen Shot 2012-02-22 at 3.41.36 PM.png (18.73 KiB) Viewed 2674 times
Any help would be GREATLY appreciated.
Re: Total Newbie PHPer Question
Posted: Wed Feb 22, 2012 8:15 pm
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>
Re: Total Newbie PHPer Question
Posted: Wed Feb 22, 2012 8:27 pm
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?
Re: Total Newbie PHPer Question
Posted: Wed Feb 22, 2012 8:34 pm
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.
Re: Total Newbie PHPer Question
Posted: Wed Feb 22, 2012 8:38 pm
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>
Re: Total Newbie PHPer Question
Posted: Wed Feb 22, 2012 8:47 pm
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);
?>
Re: Total Newbie PHPer Question
Posted: Wed Feb 22, 2012 8:51 pm
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>
Re: Total Newbie PHPer Question
Posted: Wed Feb 22, 2012 8:57 pm
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>
Re: Total Newbie PHPer Question
Posted: Wed Feb 22, 2012 9:09 pm
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();
?>
Re: Total Newbie PHPer Question
Posted: Wed Feb 22, 2012 9:14 pm
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()
Re: Total Newbie PHPer Question
Posted: Thu Feb 23, 2012 1:10 am
by Riceman
Thanks Celauran,
Really appreciate it.