Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Echo field value

Status
Not open for further replies.

Bersani

Programmer
Nov 10, 2011
48
US
I want to be able to show a field value using echo. I have tried $fielname with no success.
 
Can you be a bit less vague?

By "field value" you mean a field from a database?

What does your code look like?

Help us help you.






----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
I want to display the value of a field (lastname) from a table.
 
Again too vague.

What Database? MYSQL? MSSQL?

What connection method are you using? ADO? OBDC? Standard MYSQL functions? Improved MYSQL functions? Some wrapper framework?

Assuming you have already made the connection to the database and have run a query that returned some results, each method has a different way of accessing those results.

For example if you used mysql_query(); to run the query, then you would have a variable that holds the results, and then you would need to use mysq_fetch_array, or mysql_fetch_assocto get an array containing the fields of one row at a time.

For instance:
Code:
$results = mysql_query('SELECT fieldname1,fieldname2,fieldname3 FROM mytable');
while($row=mysql_fetch_array($results)
{
  echo $row['fieldname1'];
}

This would output the fieldname1 field of each row that was returned by the query.

But this is all guess work, you could be using ADO or something else and the example would be completely different.

Look at the PHP Online Manual For lots of examples of how to get data out of a database based on your connection method.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
I am using a MySQL database (thru Xampp) and PHP. I am new to MySQL and am using PHP runner. However, this program makes it very difficult to insert changes
 
Sorry, I've never used PHP Runner. but perhaps learning the basic way to do it before entrusting it to an application would be better.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
I agree with you vacunita. However, I have tried several programs with little success. I have trial versions of Dreamweaver, Embarcadero RAD PHP, Zend studio, PHP Runner, Microsoft Expression web, and others. I love MySQL and find it very easy especially with SQLog, but have difficulty with PHP. I am used to Visual Foxpro. I cannot seem to find a good web design program to use with MySQL and the PHP server side scripts. Do you have any suggestions?
 
Personally I've never used a GUI to build the interactions between MYSQL and PHP. I've always done the connections directly.

At its core, the connections are very simple. And getting data is also very easy.


As I said try the basic approach, code the connection by hand so you know what it needs to do, and how it needs to do it. And what each part returns.

As en example, here's a quick code snippet that will connect to a database, and query a table, and display the results from it.

Code:
<?php
[green]// Setup the connection Paramters:  Host, (either an IP address, or a URL, or even localhost if mysql resides in the same physical server as the PHP processor and webserver.),database, user and password[/green]
$host		='localhost';
$database	= 'dbname';
$user		= 'username';
$pass		='pwasword';

[green]//Initiate the connection, usage of mysql_error in the die statement is suggetsed when in development to catch any potential mysql errors.[/green]
$conn = mysql_connect($host,$user,$pass) or die(mysql_error());
[green]// Select the database to be used [/green]
$db = mysql_select_db($database) or die(mysql_error());

[green]//Setup the query to be run [/green]
$query = 'SELECT * FROM mytable';

[green]// Execute the query[/green]
$result = mysql_query($query) or die (mysql_error());

[green]//Display the results. mysql_fetch_array, mysql_fetchAssoc, mysql_fetch_row etc... retrieve one row at a time from the result set returned by mysql_query.[/green]
echo "<table border=2>";
while($row = mysql_fetch_array($result))
{
	echo "<tr><td>" . $row['idmytable'] . "</td><td>" . $row['myfield1'] . "</td><td>" . $row['myfield2'] . "</td></tr>";
}

echo "</table>";

?>




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
I do not understand if 'idmytable' is the same as 'mytable'. I want to thank you for your patience. I am a beginner in PHP.
 
Idmytable refers to just one of your fields.

In the example I chose the ID or primary key of my table as the first column of the HTML table. But it could be any of your fields, just like the other 2 fields.

My primary key is named idmytable.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
I tried it several times but keep on getting:
"; while($row = mysql_fetch_array($result)) { echo "" . $row['id'] . "" . $row['lastname'] . "" . $row['firstname'] . ""; } echo ""; ?>

This is what I see in Firefox and Chrome ; in IE I see nothing.

I am also using SQLyog and can access the database and table easily.

id = primary key
lastname = field 1
firstname = field 2
 
Looks like the PHP isn't getting parsed.

Are you sure you have the webserver correctly set up to parse PHP, and are accessing the file through the web server, such as


Rather than opening it directly as a file?


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
I am accessing as you indicated. I have tried placing the test file both in the root (C:\xampp) and the htdocs subdirectory where I have other PHP files (open source) that I can access from a browser. I have had no success.
 
I finally got it. I have installed both Xampp and Xampp lite and was using the wrong one. Thank you very much. You have been very helpful. Where are you located?
 
Now that I know the basic way to display field results on a page, how do I set up controls and make it appealing?
 
just the same way as with normal html output.
either echo html or break out of php and have the html directly on the page

Code:
<?php
/* some php goes here */
?>
<form method="post" action="#" >
 <input type="text" value="test" name="textbox"/>
</form>
 
Yup. HTML and CSS to make it appealing, and usage of forms and inputs for controls.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top