The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> MySQL PHP Select Concat from Database, Select all columns and concat from same database
Dante Monaldo
post Mar 18 2012, 11:07 PM
Post #1


Advanced Member
****

Group: Members
Posts: 124
Joined: 22-March 09
From: California, USA
Member No.: 8,132



I've been having some trouble with an SQL query and was wondering if someone could take a look.

I am trying to SELECT all of the columns in a database and CONCAT one column inside a image HTML tag. Ideally what I want is to retrieve all of the columns and put <img src=" and "/> around image_url column. Below is the working code that I have right now, but I can only get the image column with the HTML tags around it (my_image) to appear. None of the other columns write.

CODE

$sql = "SELECT CONCAT('<img src=\"', image_url, '\" width=\"50px\" />') AS my_image FROM products";


Thanks!

This post has been edited by Dante Monaldo: Mar 18 2012, 11:10 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Dante Monaldo
post Mar 19 2012, 07:44 PM
Post #2


Advanced Member
****

Group: Members
Posts: 124
Joined: 22-March 09
From: California, USA
Member No.: 8,132



I think I may have to do two SELECT statements, one for all the columns and one for the CONCAT image column. The problem is that I need to do this all in one line of SQL.

I've tried using multiple SELECT statements in several different ways, but I keep getting errors. For example...

CODE
$sql = "SELECT * (SELECT CONCAT('<img src=\"', image_url, '\" width=\"50px\" />') AS my_image FROM products) FROM products";


I would really appreciate any help, or at least a tip to get around this.

BTW, I'm using PHP with MySQL.

This post has been edited by Dante Monaldo: Mar 19 2012, 07:45 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Mar 21 2012, 08:30 PM
Post #3


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



QUOTE
The problem is that I need to do this all in one line of SQL.


Can you explain why? Your attempt above makes no sense at all to me...

What values do you need to get from the DB --- use SQL
What format do you need to write them out in -- use php

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Dante Monaldo
post Mar 23 2012, 12:40 AM
Post #4


Advanced Member
****

Group: Members
Posts: 124
Joined: 22-March 09
From: California, USA
Member No.: 8,132



Sorry, I should have been more descriptive. I actually just figured it out. See below:

CODE
SELECT *, concat('<img src=\"', image_url, '\" width=\"50px\" />') AS image FROM products


Basically, I was trying to retrieve all the columns of a database and concat one column inside an image tag. I knew I was missing something small; turns out it was simply just a comma.

Thanks anyways though!
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 28th March 2024 - 01:27 PM