Jump to content

Using PHP to display a SQL Query


Mutley

Recommended Posts

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]<?php
require_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.
Link to comment
https://forums.phpfreaks.com/topic/15173-using-php-to-display-a-sql-query/
Share on other sites

[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 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 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?
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]
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]
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: $name
DOB: $dob
Favourite food: $food
...

Thanks alot. :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.