Mutley Posted July 20, 2006 Share Posted July 20, 2006 Hi there,Having a problem. I want it so if you goto this certain page it displays a row from the table and all the fields.I have done a config.php which is this (but filled in):[code]<?php$host = "localhost";$user = "myusername";$pwd = "mypassword";$db = "mypassword";?>[/code]Then a connection.php file:[code]<?phprequire_once("config.php");$connection = mysql_connect($host, $user, $pwd) or die("&error1=".mysql_error());mysql_select_db($db, $connection);?>[/code]Now I want to display a query in a table from the database but no idea how.I guess it is something like this but alot more complex. The table is called "profiles" and I want it to display all the fields in a table, under the user id 1.[code]<?php>require_once("connection.php");$query = SELECT all fields FROM profiles WHERE id=1?>[/code]Any help, would be much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/15173-using-php-to-display-a-sql-query/ Share on other sites More sharing options...
ChaosXero Posted July 20, 2006 Share Posted July 20, 2006 [code]$query = "SELECT * FROM 'profiles' WHERE 'id'=1";$result = mysql_query($query);$r= mysql_fetch_array($result);while($result) { foreach($r as $var){ echo $var."\n" }}[/code](I'm sure about the query syntax, however the foreach loop I haven't done much. YOu might check the syntax) Quote Link to comment https://forums.phpfreaks.com/topic/15173-using-php-to-display-a-sql-query/#findComment-61150 Share on other sites More sharing options...
Mutley Posted July 20, 2006 Author Share Posted July 20, 2006 Nope, get parse error on line 12. Where } is.Is that all I need? Do I not need any HTML table formatting to display the result?Thanks for the fast reply though. Quote Link to comment https://forums.phpfreaks.com/topic/15173-using-php-to-display-a-sql-query/#findComment-61157 Share on other sites More sharing options...
ChaosXero Posted July 20, 2006 Share Posted July 20, 2006 [code]<?PHP$query = "SELECT * FROM 'profiles' WHERE 'id'=1";$result = mysql_query($query);while($r= mysql_fetch_array($result)) { foreach($r as $var){ echo $var."\n" }}?>[/code]Try that now. Quote Link to comment https://forums.phpfreaks.com/topic/15173-using-php-to-display-a-sql-query/#findComment-61161 Share on other sites More sharing options...
ryanlwh Posted July 20, 2006 Share Posted July 20, 2006 don't use mysql_fetch_array in this case, because it will duplicate the records (returning the numerical indices and the associative indices). use either mysql_fetch_row or mysql_fetch_assoc Quote Link to comment https://forums.phpfreaks.com/topic/15173-using-php-to-display-a-sql-query/#findComment-61165 Share on other sites More sharing options...
Mutley Posted July 20, 2006 Author Share Posted July 20, 2006 [quote author=ChaosXero link=topic=101277.msg400592#msg400592 date=1153415316][code]<?PHP$query = "SELECT * FROM 'profiles' WHERE 'id'=1";$result = mysql_query($query);while($r= mysql_fetch_array($result)) { foreach($r as $var){ echo $var."\n" }}?>[/code]Still get the error on line 8. Parse error: parse error, unexpected '}', expecting ',' or ';' in .Try that now.[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/15173-using-php-to-display-a-sql-query/#findComment-61166 Share on other sites More sharing options...
wildteen88 Posted July 20, 2006 Share Posted July 20, 2006 [code=php:0]<?PHP$query = "SELECT * FROM 'profiles' WHERE 'id'=1";$result = mysql_query($query);while($r= mysql_fetch_assoc($result)) { foreach($r as $var){ echo $var."\n"; //missing ; }}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15173-using-php-to-display-a-sql-query/#findComment-61168 Share on other sites More sharing options...
treilad Posted July 20, 2006 Share Posted July 20, 2006 Missing a ; after the echo. Quote Link to comment https://forums.phpfreaks.com/topic/15173-using-php-to-display-a-sql-query/#findComment-61170 Share on other sites More sharing options...
ryanlwh Posted July 20, 2006 Share Posted July 20, 2006 try this[code]<?PHP$query = "SELECT * FROM 'profiles' WHERE 'id'=1";$result = mysql_query($query);while($r= mysql_fetch_assoc($result)) { foreach($r as $field=>$var){ echo "$field: $var\n"; }}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15173-using-php-to-display-a-sql-query/#findComment-61171 Share on other sites More sharing options...
Mutley Posted July 20, 2006 Author Share Posted July 20, 2006 Error:Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource on line 5.This line:while($r= mysql_fetch_assoc($result)) {Any ideas?Great replies so far guys, thanks. :) Quote Link to comment https://forums.phpfreaks.com/topic/15173-using-php-to-display-a-sql-query/#findComment-61174 Share on other sites More sharing options...
wildteen88 Posted July 20, 2006 Share Posted July 20, 2006 remove the single quotes - ' - from your query.Also make sure you are connected to the database too. Quote Link to comment https://forums.phpfreaks.com/topic/15173-using-php-to-display-a-sql-query/#findComment-61176 Share on other sites More sharing options...
Mutley Posted July 20, 2006 Author Share Posted July 20, 2006 [quote author=wildteen88 link=topic=101277.msg400608#msg400608 date=1153416453]remove the single quotes - ' - from your query.Also make sure you are connected to the database too.[/quote]Awesome it worked, thank-you so much!How do I make it so I can format the result fields? For example each field result is displayed in a html table? Do I use "echo" somehow? Quote Link to comment https://forums.phpfreaks.com/topic/15173-using-php-to-display-a-sql-query/#findComment-61188 Share on other sites More sharing options...
wildteen88 Posted July 20, 2006 Share Posted July 20, 2006 Somthing like this:[code]echo "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n ";while($r = mysql_fetch_assoc($result)){ echo "<tr>\n "; foreach($r as $field => $var) { echo "<td>{$var}</td>\n "; } echo "<tr>\n";}echo "</table>";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15173-using-php-to-display-a-sql-query/#findComment-61190 Share on other sites More sharing options...
Mutley Posted July 20, 2006 Author Share Posted July 20, 2006 Could you give a more detailed explination on how to tie this into the first piece? I presume something needs to go after each echo? Quote Link to comment https://forums.phpfreaks.com/topic/15173-using-php-to-display-a-sql-query/#findComment-61202 Share on other sites More sharing options...
wildteen88 Posted July 20, 2006 Share Posted July 20, 2006 That code replaces the following:[code]while($r= mysql_fetch_assoc($result)) { foreach($r as $field=>$var){ echo "$field: $var\n"; }}[/code]So the full code will now be this:[code]<?PHP$query = "SELECT * FROM 'profiles' WHERE 'id'=1";$result = mysql_query($query);echo "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n ";while($r = mysql_fetch_assoc($result)){ echo "<tr>\n "; foreach($r as $field => $var) { echo "<td>{$var}</td>\n "; } echo "<tr>\n";}echo "</table>";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15173-using-php-to-display-a-sql-query/#findComment-61240 Share on other sites More sharing options...
HeyRay2 Posted July 20, 2006 Share Posted July 20, 2006 Anything within the quotes of...[code]echo " ";[/code]Will be printed to your web browser, so simply layout your table to how wildteen88 mentioned and you should be good to go... :) Quote Link to comment https://forums.phpfreaks.com/topic/15173-using-php-to-display-a-sql-query/#findComment-61244 Share on other sites More sharing options...
Mutley Posted July 20, 2006 Author Share Posted July 20, 2006 Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource on line 8? Quote Link to comment https://forums.phpfreaks.com/topic/15173-using-php-to-display-a-sql-query/#findComment-61362 Share on other sites More sharing options...
Mutley Posted July 20, 2006 Author Share Posted July 20, 2006 Fixed it, similar problem to before, this is correct:[code]<?PHP$query = "SELECT * FROM profiles WHERE id=1";$result = mysql_query($query);echo "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n ";while($r = mysql_fetch_assoc($result)){ echo "<tr>\n "; foreach($r as $field => $var) { echo "<td>{$var}</td>\n "; } echo "<tr>\n";}echo "</table>";?>[/code]Now if I wish to have each part in a row, rather than columns, how would I do that? I would like it to look something like this, with 2 columns, one with the title and the other column with the data:Name: $nameDOB: $dobFavourite food: $food...Thanks alot. :) Quote Link to comment https://forums.phpfreaks.com/topic/15173-using-php-to-display-a-sql-query/#findComment-61383 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.