Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
05-29-2003, 08:03 AM
|
#1
|
|
Member
Registered: Jun 2002
Location: USA
Distribution: Suse 8.0
Posts: 247
Rep:
|
pulling data from mysql database
This is somewhat of a double post, so let me go ahead and apologize. I have the following php script to pull data from a mysql database:
<?
$db_name = "php";
$db_user = "testphp";
$db_pass = "test";
$connection = mysql_connect("localhost", "$db_user", "$db_pass") or die("Couldn't");
$db = mysql_select_db($db_name, $connection) or die("Couldn't Select Database");
$sql = "SELECT * from phptest";
$result = mysql_query($sql, $connection)or die ("Couln't execute query.");
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$name = $row['name'];
$text = "$id $name <br>";
}
echo $text;
?>
the problem is, it only displays the very last row from the table. I have five rows and would like for it to show the id and name from all five. Any suggestions? Thanks
|
|
|
|
05-29-2003, 08:08 AM
|
#2
|
|
Senior Member
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243
Rep:
|
Its not working becuase you echo $text line is outside of your while loop! So all you're printing is the final value written to the $text variable.
What you want is
PHP Code:
<?
$db_name = "php";
$db_user = "testphp";
$db_pass = "test";
$connection = mysql_connect("localhost", "$db_user", "$db_pass") or die("Couldn't");
$db = mysql_select_db($db_name, $connection) or die("Couldn't Select Database");
$sql = "SELECT * from phptest";
$result = mysql_query($sql, $connection)or die ("Couln't execute query.");
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$name = $row['name'];
$text = "$id $name <br>";
echo $text;
}
?>
cheers
Jamie...
|
|
|
|
05-29-2003, 08:28 AM
|
#3
|
|
Member
Registered: Jun 2002
Location: USA
Distribution: Suse 8.0
Posts: 247
Original Poster
Rep:
|
Thanks, that did it. To take it a step further for a  if i want to only pull one row, why can i not change
$sql = "SELECT * from phptest";
to
$sql = "SELECT test from phptest";
and it only pull back the row with the id and name of test? When i do so, it comes back 'cant execute query'
Thanks
Last edited by zuessh; 05-29-2003 at 08:30 AM.
|
|
|
|
05-29-2003, 08:32 AM
|
#4
|
|
Senior Member
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243
Rep:
|
Sounds to me like you want something like
Code:
select * from phptest where name = "test";
on the assumption that you want all rows where name = "test". If you just want the first occurance then you'll want
Code:
select * from phptest where name = "test" limit 1;
Sounds to me like you need to readup on SQL!
cheers
Jamie...
|
|
|
|
05-29-2003, 09:08 AM
|
#5
|
|
Member
Registered: Jun 2002
Location: USA
Distribution: Suse 8.0
Posts: 247
Original Poster
Rep:
|
You are correct, I need to read on sql and php. Any good suggestions, other than webmonkey? Couple more questions
When i add
$sql = "select * from phptest where id = 1";
it returns the correct information, however when i add
$sql = "select * from phptest where name = Test";
it comes back 'could not execute query'.
Thanks for all the help
|
|
|
|
05-29-2003, 09:13 AM
|
#6
|
|
Senior Member
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243
Rep:
|
Quote:
Originally posted by zuessh
$sql = "select * from phptest where name = Test";
|
Should be
Code:
$sql = "select * from phptest where name = "Test";
You need to quote strings. Remember that if you doing this via PHP and you are holding your query in a variable first then you'll need to either delimit the speachmarks or user single quotes, so
PHP Code:
$query = "select * from phptest where name = \"test\"";
or
PHP Code:
$query = "select * from phptest where name = 'test'";
As for SQL resources, I'm not sure really, what do you get if you just google for "SQL tutorial"? I'd guess that you might hit some good material at universities as basic SQL seems a favourite on many computing related degrees.
cheers
Jamie...
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:11 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|