473,386 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

begginer's simple PHP select statment

I need help on my basic table connection syntaxt using PHP. I call
the php from a web browser url_name_goes_here/test.php. The database
name is tennis and the user name is tennis.

If I type the commands directly from the MySQL> mode, my access works
fine (see example 1 below). But when I try to access the table from
PHP, I have a syntaxt problem (see example 2 below). Can anyone
please type up the correct PHP syntaxt for me? The 3rd example works
but I don't know how to translate that into a select statement for the
table.

Finally, after I get it working, how do I keep my password out of
sight? if I forgot to put an index file in the directory, wouldln't all
of the files be visible from a surfer's browser?

Thanks!

Alex Glaros

EXAMPLE 1 (from within MySQL - this works correctly)
--------------------------------------------------------------------------------------------------------------------------------------------------------------

/usr/local/mysql3/bin/mysql -h db1.pacific.net -u tennis -p
Enter password: ( get prompted for my password here, then sucessfully
get to mysql> prompt)
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 123456 to server version: 3.23.57
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use tennis
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from tourneys;
| tourney_id | tourney_name | minutes_per_game | td_id |
multi_venued_y_or_n | date_tourney_begins | date_tourney_ends

Then I get all the rows for table tourneys correctly displayed
--------------------------------------------------------------------------------------------------------------------------------------------------------------

EXAMPLE 2 (Web browser calls PHP - this doesn't work)

<?
$db_username = 'tennis';
$db_password = 'mypassword';
$db_hostname = 'db1.pacific.net';
$db_connect = mysql_connect($db_hostname, $db_username, $db_password)or
die("Unable to connect to MySQL");
$db_select = mysql_select_db('tourneys', $db_connect)or die("Unable to
select tourneys");
while ($row = mysql_fetch_array($result)) {
printf "$row[tourney_id] $row[tourney_name] $row[td_id]";
}
mysql_close($db_connect);
?>

HERE'S THE ERROR MESSAGE I GET:
Parse error: parse error, unexpected '\"' in
/server_path_name/public_html/test.php on line 8
--------------------------------------------------------------------------------------------------------------------------------------------------------------

EXAMPLE 3 (Web browser calls PHP - this does work but I don't know how
to get the select statement above to work)

<?php
mysql_connect("db1.pacific.net", "tennis", "mypassword");
$result = mysql_list_tables("tennis");
$num_rows = mysql_num_rows($result);
for ($i = 0; $i < $num_rows; $i++) {
echo "Table: ", mysql_tablename($result, $i), "\n";
}
mysql_free_result($result);
?>

Jul 17 '05 #1
5 2283
Alex Glaros wrote:
I need help on my basic table connection syntaxt using PHP. I call
the php from a web browser url_name_goes_here/test.php. The database
name is tennis and the user name is tennis.

If I type the commands directly from the MySQL> mode, my access works
fine (see example 1 below). But when I try to access the table from
PHP, I have a syntaxt problem (see example 2 below). Can anyone
please type up the correct PHP syntaxt for me? The 3rd example works
but I don't know how to translate that into a select statement for the
table.

Finally, after I get it working, how do I keep my password out of
sight? if I forgot to put an index file in the directory, wouldln't all
of the files be visible from a surfer's browser?

Thanks!

Alex Glaros

EXAMPLE 1 (from within MySQL - this works correctly)
--------------------------------------------------------------------------------------------------------------------------------------------------------------

/usr/local/mysql3/bin/mysql -h db1.pacific.net -u tennis -p
Enter password: ( get prompted for my password here, then sucessfully
get to mysql> prompt)
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 123456 to server version: 3.23.57
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use tennis
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from tourneys;
| tourney_id | tourney_name | minutes_per_game | td_id |
multi_venued_y_or_n | date_tourney_begins | date_tourney_ends

Then I get all the rows for table tourneys correctly displayed
--------------------------------------------------------------------------------------------------------------------------------------------------------------

EXAMPLE 2 (Web browser calls PHP - this doesn't work)

<?
$db_username = 'tennis';
$db_password = 'mypassword';
$db_hostname = 'db1.pacific.net';
$db_connect = mysql_connect($db_hostname, $db_username, $db_password)or
die("Unable to connect to MySQL");
$db_select = mysql_select_db('tourneys', $db_connect)or die("Unable to
select tourneys");
while ($row = mysql_fetch_array($result)) {
printf "$row[tourney_id] $row[tourney_name] $row[td_id]";
}
mysql_close($db_connect);
?>

HERE'S THE ERROR MESSAGE I GET:
Parse error: parse error, unexpected '\"' in
/server_path_name/public_html/test.php on line 8


<snip>

mysql_select_db() is used to select the database, not the table. It is
equivalent to the "USE tennis" you did from the mysql client, so you
need to change "tourneys" to "tennis" in the mysql_select_db call. Also,
you aren't executing a query. You need this between the mysql_select_db
and the while():

$result = mysql_query("SELECT * FROM tourneys", $db_connect);

NM
Jul 17 '05 #2
Alex Glaros wrote:
Finally, after I get it working, how do I keep my password out of
sight? if I forgot to put an index file in the directory, wouldln't all
of the files be visible from a surfer's browser?


As long as your web server is properly configured to do PHP, users won't
be able to see your PHP code. If PHP is broken, that's another story.

Check the PHP manual re MySQL. I seem to recall there being environment
variables where you can put the MySQL user/pass into. That way if you
break PHP, you aren't exposing your code. You should also check the
MySQL manual, as you can put a file called ".mysql_rc" (or something
like) that in the homedir of the user you are accessing MySQL as. You
can put the password in that file as well.

NM
Jul 17 '05 #3
> Alex Glaros wrote:
EXAMPLE 2 (Web browser calls PHP - this doesn't work)

<?
$db_username = 'tennis';
$db_password = 'mypassword';
$db_hostname = 'db1.pacific.net';
$db_connect = mysql_connect($db_hostname, $db_username, $db_password)or
die("Unable to connect to MySQL");
$db_select = mysql_select_db('tourneys', $db_connect)or die("Unable to
select tourneys");
while ($row = mysql_fetch_array($result)) {
printf "$row[tourney_id] $row[tourney_name] $row[td_id]";
}
mysql_close($db_connect);
?>

HERE'S THE ERROR MESSAGE I GET:
Parse error: parse error, unexpected '\"' in
/server_path_name/public_html/test.php on line 8

News Me wrote:
mysql_select_db() is used to select the database, not the table. It is
equivalent to the "USE tennis" you did from the mysql client, so you
need to change "tourneys" to "tennis" in the mysql_select_db call. Also,
you aren't executing a query. You need this between the mysql_select_db
and the while():

$result = mysql_query("SELECT * FROM tourneys", $db_connect);

NM


Too much Holiday cheer....

Your printf line won't work either. Assuming "tourney_id",
"tourney_name", and "td_id" are columns in TENNIS.TOURNEYS, then you
should change your while() and printf() to this:

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
print "$row{'tourney_id'} $row{'tourney_name'} $row{'td_id'}\n";

The MYSQL_ASSOC causes the row to be returned as an associative array
rather than an ordinal array, which is required if you want to refer to
columns by names. Otherwise you must use $row[0] for the first column,
$row[1] for the second, etc.

You should read up on print and printf, as you are using printf in your
example as if you were using print. The print statement I show above is
considered bad form (at least in perl it is), but it works.

NM

Jul 17 '05 #4
It works!

I changed the print row so that the single quotes were removed and I'm
all set.

print "$row[tourney_id] $row[tourney_name] $row[td_id]\n";
Thanks for showing me the basics.

Alex Glaros

News Me wrote:
Alex Glaros wrote:
EXAMPLE 2 (Web browser calls PHP - this doesn't work)

<?
$db_username = 'tennis';
$db_password = 'mypassword';
$db_hostname = 'db1.pacific.net';
$db_connect = mysql_connect($db_hostname, $db_username, $db_password)or die("Unable to connect to MySQL");
$db_select = mysql_select_db('tourneys', $db_connect)or die("Unable to select tourneys");
while ($row = mysql_fetch_array($result)) {
printf "$row[tourney_id] $row[tourney_name] $row[td_id]";
}
mysql_close($db_connect);
?>

HERE'S THE ERROR MESSAGE I GET:
Parse error: parse error, unexpected '\"' in
/server_path_name/public_html/test.php on line 8
News Me wrote:
mysql_select_db() is used to select the database, not the table.
It is equivalent to the "USE tennis" you did from the mysql client, so you need to change "tourneys" to "tennis" in the mysql_select_db call. Also, you aren't executing a query. You need this between the mysql_select_db and the while():

$result = mysql_query("SELECT * FROM tourneys", $db_connect);

NM


Too much Holiday cheer....

Your printf line won't work either. Assuming "tourney_id",
"tourney_name", and "td_id" are columns in TENNIS.TOURNEYS, then you
should change your while() and printf() to this:

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
print "$row{'tourney_id'} $row{'tourney_name'} $row{'td_id'}\n";

The MYSQL_ASSOC causes the row to be returned as an associative array

rather than an ordinal array, which is required if you want to refer to columns by names. Otherwise you must use $row[0] for the first column, $row[1] for the second, etc.

You should read up on print and printf, as you are using printf in your example as if you were using print. The print statement I show above is considered bad form (at least in perl it is), but it works.

NM


Jul 17 '05 #5
News Me wrote:
Alex Glaros wrote:
Finally, after I get it working, how do I keep my password out of
sight? if I forgot to put an index file in the directory, wouldln't all
of the files be visible from a surfer's browser?

As long as your web server is properly configured to do PHP, users won't
be able to see your PHP code. If PHP is broken, that's another story.

Check the PHP manual re MySQL. I seem to recall there being environment
variables where you can put the MySQL user/pass into. That way if you
break PHP, you aren't exposing your code. You should also check the
MySQL manual, as you can put a file called ".mysql_rc" (or something
like) that in the homedir of the user you are accessing MySQL as. You
can put the password in that file as well.

NM


(Correct me if I'm wrong, but from what I understand...)

Additionally, you can define a user specifically for your host
(user@localhost) with the access permissions that you need with no
password. Once again, from what I understand, that user _must_ be
accessing from localhost to work, which seems pretty secure to me.

If there are any hacks on this that should be addressed, please let me
know.. just a beginner here myself ;)

Thanks,
Glenn
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Alex Glaros | last post by:
I need help on my basic table connection syntaxt using PHP. I call the php from a web browser url_name_goes_here/test.php. The database name is tennis and the user name is tennis. If I type...
1
by: e_AK_05 | last post by:
I have a question and I can't figure out how to do it. I have a select statment : SELECT table1.* AS table1.*, table2.* AS table2.* FROM table1, table2 WHERE 1 this does not work...for me at...
1
by: Joe Saliba | last post by:
Hi, would like to know how to write a crosstable select statment in sql server2000 where having: - ItemNumber, ItemDescription, ItemColor, ItemSize as rows - Stores as columns - Qty * Netttc...
5
by: orencs | last post by:
Hello, I am using Microsoft.Practices.EnterpriseLibrary.Data. I am running the following sqlCommand = "SELECT var1 FROM table1 WHERE var2 IN (4,5,6) ; SELECT var3 FROM table2 WHERE var2 IN...
1
by: Anton Nikiforov | last post by:
Dear All, could you please help me with writing select statment for the following: I have two tables dictionary_text label| Text -----+------------------------ 23 | General 24 | Internet...
6
by: mabond | last post by:
Hi Is it possible to compare a vaule for a select case statement using a wildcard. e.g. somthing like Select case myValue case like "*ing" end select
2
by: gimme_this_gimme_that | last post by:
I use the following SQL statment to bring z_emp_id values to a employee table: update employee set z_emp_id = (select z.emp_id from z.employee z where z.login=employee.login) Upon executing...
8
by: Trevor2007 | last post by:
I am trying to hard code the following select query into a select case statement ie (case1 <statment>, case 2 <statment>) but I getteing Compiler error: expected line number or label, or...
13
by: deshix | last post by:
Hi everyone, can you recommend me the best way to learn the PHP? I'm really begginer...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.