0

Hi,

I have a query, the output of which I need to display in my page.

say, if I do ... select col1 as column from table1;

how could I display 'column' in my echo statement or else how could I display it in my PHP code?

Thanks for your assistance!

2
Contributors
8
Replies
9
Views
7 Years
Discussion Span
Last Post by diafol
0
$r = mysql_query("SELECT col1 FROM table1");
while($d = mysql_fetch_array($r)){
  echo $d['col1'] . "<br />";
}

This will give a list of values from col1 in table table1. You could equally place the values into an unordered or ordered list or place each value into its own paragraph.

$r = mysql_query("SELECT col1 FROM table1");
$output = "\n<ul>"; //change this to <ol> if you want a numbered list
while($d = mysql_fetch_array($r)){
  $output .= "\n\t<li>" . $d['col1'] . "</li>";
}
$output .= "\n</ul>"; //change this to </ol> if you want a numbered list
echo $output;

If you're wondering about the "\n" and the "\t" - these are just to beautify the html. The "\n" = new line; the "\t" = tabspace.

The second example also makes use of the concatenator (.=), which simply adds the following string to the existing string. If you leave out the dot, it'll simply be overwritten.

Apologies if you know all this, I'm just assuming you're new to php/mysql.

The above is a simple description, it DOESN'T include any "unsanitising" of data, e.g. stripslashes/html_entity_decode. Neither does it check for any existing records before you loop.

Edited by diafol: n/a

0

My question is different..

I have gone through the loop executing my query.. Now I am connecting to a different data base from inside the loop and executing another query there. Its not a loop. I just need to execute that query there and some means (which I am asking) to display that output there.

Inside the loop
:
:
$query="Select col1 as column from example";

echo ???
:
:
loop ending

I have an alias 'column' for what I am just asking the syntax with which I could display column in my page.

Thanks.

0

Are your databases on the same server? If so, should be OK. You may be able to combine both queries into one using the JOIN clause. Post your code so that we can see what you're trying to do.

You can access different fields/tables by catenating the db name:

For example:

"SELECT db1.table2.field1 AS videotitle, db2.table1.field7 AS videoyear FROM db1.table2 INNER JOIN db2.table1 ON db1.table2.videoid = db2.table1.vid"

You will now have 2 fields returned by the recordset: 'videotitle' and 'videoyear'.

Edited by diafol: n/a

0

yes.. thats correct.. but how can I display videotitle and videoyear in PHP after this SELECT statement?

If I try

echo $videotitle ===> getting error.

Somehow there should be a way to display it I think.. but that I don't know..

Thanks.

0

Aw, krip - you serious?

$r = mysql_query("SELECT...");
while($d = mysql_fetch_array($r)){
  echo $d['videotitle'] . " " . $d['videoyear'];
}
0

Hi..

I was getting errors on this..

Let me try.. I will get back to you..

Thanks.

0

Hey Ardav,

The errors that I was getting was because of the two DB connections that I was trying to work with. I got my required output now. Thanks!

I have a simple doubt please.. Suppose I have a SQL query that returns me a value. Its not fetching a row just a single value and I want to print it. Is there any other way by which I could print it without using a loop?

Thanks.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.