Entering SQL query in PHP

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
influenceuk
Forum Commoner
Posts: 42
Joined: Tue May 08, 2007 7:48 am
Location: London, UK

Entering SQL query in PHP

Post by influenceuk »

I have been trying to enter this mySQL query into my PHP page but keep getting errors

Code: Select all

$sql = 'SELECT * FROM `database` WHERE `table1` & lt ; = current_date
How would i go about putting it in?
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Code: Select all

$sql = 'SELECT * FROM `database` WHERE `table1` AND lt = NOW()';
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Entering SQL query in PHP

Post by califdon »

influenceuk wrote:I have been trying to enter this mySQL query into my PHP page but keep getting errors

Code: Select all

$sql = 'SELECT * FROM `database` WHERE `table1` & lt ; = current_date
How would i go about putting it in?
First of all, you select FROM a table, not a database (you first specify the database with:

Code: Select all

mysql_select_db(`databasename`);
then you specify the selection criteria in the WHERE clause. I don't know what you're actually trying to do, but I suspect you want something like:

Code: Select all

$sql = 'SELECT * FROM `table1` WHERE `date-in-record` < DATE()';
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Oren wrote:

Code: Select all

$sql = 'SELECT * FROM `database` WHERE `table1` AND lt = NOW()';
Hmm... what the heck? I guess I was confused by your query + read/replied too fast. Forgive me, and ignore my previous post :P :lol:

P.S No, I wasn't drunk :P
influenceuk
Forum Commoner
Posts: 42
Joined: Tue May 08, 2007 7:48 am
Location: London, UK

Re: Entering SQL query in PHP

Post by influenceuk »

califdon wrote:
influenceuk wrote:I have been trying to enter this mySQL query into my PHP page but keep getting errors

Code: Select all

$sql = 'SELECT * FROM `database` WHERE `table1` & lt ; = current_date
How would i go about putting it in?
First of all, you select FROM a table, not a database (you first specify the database with:

Code: Select all

mysql_select_db(`databasename`);
then you specify the selection criteria in the WHERE clause. I don't know what you're actually trying to do, but I suspect you want something like:

Code: Select all

$sql = 'SELECT * FROM `table1` WHERE `date-in-record` < DATE()';
Yeah sorry i posted database instead of table by mistake.

so if i state the database connection atthe top, and or to the connections.php file (where the database info is held) can i then just have multiple DB conections?

example

Code: Select all

mysql_select_db(`databasename`);
$sql = 'SELECT * FROM `cdsus` WHERE `release` > DATE()';

$sql = 'SELECT * FROM `cdsuk` WHERE `release` > DATE()';
so in theory the above would allow me to display data from the database, but have 2 different tables showing info?
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Re: Entering SQL query in PHP

Post by bdlang »

influenceuk wrote:I have been trying to enter this mySQL query into my PHP page but keep getting errors

Code: Select all

$sql = 'SELECT * FROM `database` WHERE `table1` & lt ; = current_date
How would i go about putting it in?
Aside from the other comments; you have no closing quote in that statement, and what errors are you getting? Please be specific when posting. Not only is that invalid SQL, it's not even valid PHP.
influenceuk wrote: so if i state the database connection atthe top, and or to the connections.php file (where the database info is held) can i then just have multiple DB conections?

example

Code: Select all

mysql_select_db(`databasename`);
$sql = 'SELECT * FROM `cdsus` WHERE `release` > DATE()';

$sql = 'SELECT * FROM `cdsuk` WHERE `release` > DATE()';
so in theory the above would allow me to display data from the database, but have 2 different tables showing info?
Why do you need multiple connections? Is your data located on two different servers? Two seperate databases? Or just two (or more) tables in the same database on the same server?

The two queries you show could be consolidated into one with a JOIN statement, they're both using the same WHERE clause. If you want more help with a JOIN statement (which I recommend to save resources) then please post your table schema(s) and exactly what you hope to achieve from the queries.
influenceuk
Forum Commoner
Posts: 42
Joined: Tue May 08, 2007 7:48 am
Location: London, UK

Post by influenceuk »

Hi there,
basically i am trying to display upcoming releases. On the main home page.

for example...

US - May 21
CD1
CD2
CD3

UK - May 18
CD1
CD1
CD3

I have 2 separate tables in one database.
database: CDs
Tables: CDs_US and CD_UK
Within this i have a list of all titles i need, and from these i want to only display a selection of the next 7 days worth of releases.

I have heard about this JOIN statement, but have not read up on it yet.
Thing is i will also want to have 2 other pages displaying the FULL release list for both countries, and display the results in order of release date.

I have used Navicat, and have been able to process the queries i want, and it works fine on there, now i just need to get them onto the actual site.

I am sure once i get the gist of it, with one of the codes, i will then manage to work out the rest lol :)

Cheers for all the help so far
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

Oops. Yes, if you have not heard of the JOIN clause in SQL, you have a lot of studying to do. I suggest you spend some time learning the real b-a-s-i-c-s of database operation (I don't want to be sarcastic, it's just so extremely fundamental that you really need to learn it!).

A good place to start would be: http://www.w3schools.com/sql/default.asp or
http://www.1keydata.com/sql/sql.html or
http://www.sql-tutorial.net/SQL-JOIN.asp
influenceuk
Forum Commoner
Posts: 42
Joined: Tue May 08, 2007 7:48 am
Location: London, UK

Post by influenceuk »

califdon, i'm in no way offended! Infact i find it extremly useful i know i need alot of studying with MySQL.
And i do have alot to learn, i will check out those sites and see how i go :)

Cheers for the help!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I agree with califdon. Doing what you want to do is covered in just about every starter PHP tutorial on the web nowadays. All except how to write effective queries.

What it appears you want to do is select data from two tables that are related. That is simple enough. But before you get your hands on that, get your feet wet with connecting to a database server, selecting a database to use and executing simple queries (including simple joins). Then move into what you want to do.
Post Reply