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
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)
Link to comment
Share on other sites

[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]
Link to comment
Share on other sites

[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?
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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. :)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.