How do I echo query results to browser?

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
calvinmicklefinger
Forum Newbie
Posts: 10
Joined: Thu May 03, 2007 2:50 pm

How do I echo query results to browser?

Post by calvinmicklefinger »

Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello, 

Newbie here trying to write a MySQL query and show on screen.

I built the following from what I understood of a tutorial or guide, modified to my site.

As a result, I get a page with no source.  I guess that means it crashed.

Help, help, help .... please, and Thank You.

***********

Code: Select all

<?php 
 $link = mysql_connect("my_host", "my_user", "my_password")
         or die('Could not connect: ' . mysql_error());

 mysql_select_db("my_database") or die('could not select database'); 

 $sql_query = "Select * From my_table WHERE userid='my_username'";

 $result = mysql_query($sql_query)or die('query failed'. mysql_error());

 while($row = mysql_fetch_array($result))
 {
	 echo "Your User ID is " . "$row['my_field1']\n";
	 echo "Your Name is " . "$row['my_field2']\n";
	 echo "Your Email is " . "$row['my_field3']\n";
	 echo "Your Address is " . "$row['my_field4']\n";
 }
 
 mysql_free_result($result);

 mysql_close($link);
?>
*****************

Any help will be appreciated.

Kirk


Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post by guitarlvr »

if you are using an apache server, go into your program directory/logs and open error.log. Is there an error at the bottom of the file? Also, the echo statements in your while loop should be similar to this:

Code: Select all

while($row = mysql_fetch_array($result))
{
echo "Your User ID is " . $row['my_field1'] . "\n";
echo "Your Name is " . $row['my_field2']  . "\n";
echo "Your Email is " . $row['my_field3'] . "\n";
echo "Your Address is " . $row['my_field4'] . "\n";
}
Wayne
calvinmicklefinger
Forum Newbie
Posts: 10
Joined: Thu May 03, 2007 2:50 pm

Post by calvinmicklefinger »

Thanks Wayne,

There was nothing in the error log related to this script.

I moved the variable out of the double quotes with newline operator and I still get no result.

When I run the code as shown, I get a single line of text, with a space character in place of the expected newline.

I am wondering if attempting to use the newline while in a loop is causing the problem.

Kirk
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post by guitarlvr »

You could probably try changing them to line breaks instead of new line. <br />

Wayne
calvinmicklefinger
Forum Newbie
Posts: 10
Joined: Thu May 03, 2007 2:50 pm

Post by calvinmicklefinger »

That worked!

Excellent, many thanks.

Now I wonder why?

Oh well.

Again, Thanks Wayne!
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post by guitarlvr »

I'm glad that worked for you. I've never had any luck in getting the \n character to work. I don't know if you have to do something special in order for it to work or not. Can anyone clear that up for us?

Wayne
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
$link = mysql_connect("my_host", "my_user", "my_password")
         or die('Could not connect: ' . mysql_error());

mysql_select_db("my_database") or die('could not select database');

$sql_query = "Select * From my_table WHERE userid='my_username'";

$result = mysql_query($sql_query)or die('query failed'. mysql_error());

while($row = mysql_fetch_array($result))
{
    echo "Your User ID is {$row['my_field1']}<br />\n";
    echo "Your Name is {$row['my_field2']}<br />\n";
    echo "Your Email is {$row['my_field3']}<br />\n";
    echo "Your Address is {$row['my_field4']}<br />\n";
}
mysql_free_result($result);
mysql_close($link);
?>
Your problem was in how you assembled the strings.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

guitarlvr wrote:I'm glad that worked for you. I've never had any luck in getting the \n character to work. I don't know if you have to do something special in order for it to work or not. Can anyone clear that up for us?

Wayne
\n is an ascii newline character. It does nothing in HTML, but if you view the textual output, it will place everything after the \n on a new line. It is most commonly found to work on *nix systems. \r is the carriage return character, most commonly found on Macs. \n\r is a combination found in Windows. I think I may have the last two confused, but you get the point.

To test how they work, run this script and view the source:

Code: Select all

<?php
$v1 = 'This is the first var... <br />';
$v2 = 'This is the second var... <br />';
$v3 = 'This is the third var... <br />';
$v4 = 'This is the fourth var... <br />';

echo 'This is without newlines in the output....<br /><br />';
echo $v1.$v2.$v3.$v4;

echo 'This is with newlines in the output....<br /><br />';
echo "$v1\n$v2\n$v3\n$v4";
?>
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post by guitarlvr »

I ran that code but honestly i don't see any difference between the two outputs, maybe I'm missing something.

Wayne
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Everah meant the first version of the script
calvinmicklefinger wrote:"$row['my_field1']\n"
syntax error.
This indicates that the script isn't running with error_reporting=E_ALL and/or display_(startup_)errors=true.
Do you use this php installation for development puurposes only, calvinmicklefinger?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

guitarlvr wrote:I ran that code but honestly i don't see any difference between the two outputs, maybe I'm missing something.

Wayne
Everah wrote:To test how they work, run this script and view the source:
Output, when viewing the source:

Code: Select all

This is without newlines in the output....<br /><br />This is the first var... <br />This is the second var... <br />This is the third var... <br />This is the fourth var... <br />This is with newlines in the output....<br /><br />This is the first var... <br />
This is the second var... <br />
This is the third var... <br />
This is the fourth var... <br />
See how the '\n' wraps the output to the next line? That is what I meant. And be aware those characters only work when wrapped inside double quotes.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

oops, sorry.
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post by guitarlvr »

I see now. i must have missed the view source part. Thanks Everah.

Wayne
calvinmicklefinger
Forum Newbie
Posts: 10
Joined: Thu May 03, 2007 2:50 pm

Post by calvinmicklefinger »

Thanks to all,

Sorry to be slow in responding, I just received the topic notifications this morning.

I will ask my host to help me with error reporting during development.

I am using the host for development until I go live, and since I am such a newbie, I had not thought about turning on error reporting.

Before trying, I think I now understand the difference between the html line brak and the newline. I expect that when I run the sample code, newline will give me a new line in the view source, while the html line break will display items on a new line in the normal browser view. They each depend on the application that is rendering the output.

I expect this newfound knowledge will be very useful in reading source and rendering output.

Many thanks to all. This was very interesting.

Kirk
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Might I suggest doing your development locally (on your own computer) then, after testing it on your machine, upload the finished product to your hosted server? Then you don't need to wrestle with your host to get them to do things to their shared servers that they shouldn't be doing anyway.
Post Reply