Page 1 of 1

Entering SQL query in PHP

Posted: Mon May 14, 2007 10:19 am
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?

Posted: Mon May 14, 2007 10:58 am
by Oren

Code: Select all

$sql = 'SELECT * FROM `database` WHERE `table1` AND lt = NOW()';

Re: Entering SQL query in PHP

Posted: Mon May 14, 2007 1:24 pm
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()';

Posted: Mon May 14, 2007 1:51 pm
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

Re: Entering SQL query in PHP

Posted: Mon May 14, 2007 6:48 pm
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?

Re: Entering SQL query in PHP

Posted: Mon May 14, 2007 7:57 pm
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.

Posted: Tue May 15, 2007 6:48 am
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

Posted: Wed May 16, 2007 5:31 pm
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

Posted: Thu May 17, 2007 12:42 pm
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!

Posted: Thu May 17, 2007 6:39 pm
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.