PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 2nd, 2009, 12:12 PM
NewWEBdesigner NewWEBdesigner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2007
Posts: 48 NewWEBdesigner User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 58 m 41 sec
Reputation Power: 3
PHP5 - Trouble with getting data from a MySQL database

Hello all,

Here is what I am trying to do. A user enters the first name of somebody and that persons first name, last name, and age is displayed. I am using PHP and MySQL. I know the database is set up and working because I can see it in PHP My Admin and I can insert data to it. What is wrong? I think it has something to do with the this part, because this where i edited the tutorial i got the code from:
PHP Code:
 $result mysql_query("SELECT * FROM Persons WHERE FirstName='$_POST[firstname]'"); 


When I try to run this code, I get this error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\select.php on line 17

Below is the code I am trying to get to work:

Here is the html code for the form:
Code:
<html> <body>  <form action="select.php" method="post"> Firstname: <input type="text" name="firstname" /> <input type="submit" /> </form>  </body> </html> 


Here is the PHP code:

PHP Code:
<?php   echo "$_POST[firstname]";  $con mysql_connect("localhost","root",""); if (!$con)   {   die('Could not connect: ' mysql_error());   }  mysql_select_db("my_db"$con);  $result mysql_query("SELECT * FROM Persons WHERE FirstName='$_POST[firstname]'");  while($row mysql_fetch_array($result))   {   echo $row['FirstName'] . " " $row['LastName'];   echo "<br />";   } ?>


Thanks for your help

Reply With Quote
  #2  
Old July 2nd, 2009, 12:14 PM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
User, Contributing
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Oct 2006
Location: Texas, USA
Posts: 3,776 ManiacDan User rank is General 11st Grade (Above 100000 Reputation Level)ManiacDan User rank is General 11st Grade (Above 100000 Reputation Level)ManiacDan User rank is General 11st Grade (Above 100000 Reputation Level)ManiacDan User rank is General 11st Grade (Above 100000 Reputation Level)ManiacDan User rank is General 11st Grade (Above 100000 Reputation Level)ManiacDan User rank is General 11st Grade (Above 100000 Reputation Level)ManiacDan User rank is General 11st Grade (Above 100000 Reputation Level)ManiacDan User rank is General 11st Grade (Above 100000 Reputation Level)ManiacDan User rank is General 11st Grade (Above 100000 Reputation Level)ManiacDan User rank is General 11st Grade (Above 100000 Reputation Level)ManiacDan User rank is General 11st Grade (Above 100000 Reputation Level)ManiacDan User rank is General 11st Grade (Above 100000 Reputation Level)ManiacDan User rank is General 11st Grade (Above 100000 Reputation Level)ManiacDan User rank is General 11st Grade (Above 100000 Reputation Level)ManiacDan User rank is General 11st Grade (Above 100000 Reputation Level)ManiacDan User rank is General 11st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 19 h 31 m 26 sec
Reputation Power: 1677
Do this:
PHP Code:
 $result mysql_query("SELECT * FROM Persons WHERE FirstName='" mysql_real_escape_string($_POST['firstname']) , "'");
if ( !
$result ) die(mysql_error()); 
-Dan
__________________
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

Think we're being rude? Please read this. Trying to argue intelligently? Please read this.

Reply With Quote
  #3  
Old July 3rd, 2009, 01:03 AM
NewWEBdesigner NewWEBdesigner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2007
Posts: 48 NewWEBdesigner User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 58 m 41 sec
Reputation Power: 3
Dan,

Thanks for responding. When I substituted your code for mine I got this error:

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\select.php on line 14

here is the code again
PHP Code:
<?php   echo "$_POST[firstname]";  $con mysql_connect("localhost","root",""); if (!$con)   {   die('Could not connect: ' mysql_error());   }  mysql_select_db("test"$con);  $result mysql_query("SELECT * FROM Persons WHERE FirstName='" mysql_real_escape_string($_POST['firstname']) , "'"); if ( !$result ) die(mysql_error());    while($row mysql_fetch_array($result))   {   echo $row['FirstName'] . " " $row['LastName'];   echo "<br />";   } ?>

Reply With Quote
  #4  
Old July 3rd, 2009, 02:08 AM
DaWizman DaWizman is offline
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 31 DaWizman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 22 m
Reputation Power: 8
PHP Code:
<?php
$con 
mysql_connect("localhost","root",""); 
if (!
$con)   {   
die(
'Could not connect: ' mysql_error());   
}  
mysql_select_db("test"$con);
$query "SELECT `FirstName`, `LastName` FROM `Persons` WHERE `FirstName` = '" mysql_real_escape_string($_POST['firstname']) . "' ";  
$result mysql_query($query);
if ( !
$result ) {
die(
mysql_error());
}    
while(
$row mysql_fetch_assoc($result))   {   
echo 
$row['FirstName'] . " " $row['LastName'];   
echo 
"<br />";   

?>


ManiacDan had a comma after the mysql_real_escape_string instead of a period. I also did some housecleaning to make the code a little nicer.

Cheers
__________________
Your man, DaWizman
Visit DaWizman Dot Com(Under Construction)
Check out Central Intelligence Forums

Reply With Quote
  #5  
Old July 10th, 2009, 12:06 AM
NewWEBdesigner NewWEBdesigner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2007
Posts: 48 NewWEBdesigner User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 58 m 41 sec
Reputation Power: 3
Thanks guys.

I changed my code around a bit. now I have a database of States and the the colleges in them. I want a user to select the state via a drop down menu. Then when the user goes to select a college, they are presented with only the colleges that correspond to that state.

My problem is that I can't figure out the syntax to get the colleges to appear in a drop down menu without getting errors.

Here is the error message I get:
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\wamp\www\Drop.php on line 49
(The error refers to the PHP code)

This is my HTML code:
Code:
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head>  <body> select your state:   <form action="Drop.php" method="post"> <select name="State"> <option value="NY">NY</option> <option value="ND">ND</option> </select> <input type="submit" /> </form>  <form action="" method=""> <select name="College"> <option value="Select College">Select College</option>  </select> <input type="submit" /> </form> </body> </html>


Here is my php code, drop.php:

PHP Code:
 Welcome <?php echo $_POST["State"]; ?>!<br />   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head>  <body> select your state:   <form action="Drop.php" method="post"> <select name="State"> <option value="NY">NY</option> <option value="ND">ND</option> </select> <input type="submit" /> </form>  <?php if (isset($_POST['State'])){     $state $_POST['State'];  echo "Country Selected: "$state "<br />";  }?>   <?php $con mysql_connect("localhost","root",""); if (!$con)   {   die('Could not connect: ' mysql_error());   }  mysql_select_db("books"$con);   $result mysql_query("SELECT * FROM schools WHERE State='$state'"); echo "<form >  <select action=''>";  while($row mysql_fetch_array($result))   {         echo "<option value='$row['School']'>" $row['School'] . "</option";      } echo " </select>"; echo " </form>"?>  </body> </html> 


btw, DaWizman, how did you get my code to appear vertically as opposed to horizontally?

PS: line 49 refers to this line of my code
PHP Code:
echo "<option value='$row['School']'>" $row['School'] . "</option"


What's a good online resource for php code syntax? that seems to be what I am having the most trouble with. Thanks

Reply With Quote
  #6  
Old July 10th, 2009, 06:09 AM
IkoTikashi's Avatar
IkoTikashi IkoTikashi is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Sep 2003
Location: Germany
Posts: 1,252 IkoTikashi User rank is Captain (20000 - 30000 Reputation Level)IkoTikashi User rank is Captain (20000 - 30000 Reputation Level)IkoTikashi User rank is Captain (20000 - 30000 Reputation Level)IkoTikashi User rank is Captain (20000 - 30000 Reputation Level)IkoTikashi User rank is Captain (20000 - 30000 Reputation Level)IkoTikashi User rank is Captain (20000 - 30000 Reputation Level)IkoTikashi User rank is Captain (20000 - 30000 Reputation Level)IkoTikashi User rank is Captain (20000 - 30000 Reputation Level)IkoTikashi User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 7 h 20 m 5 sec
Reputation Power: 275
PHP Code:
echo "<option value='$row['School']'>" $row['School'] . "</option"
should be
PHP Code:
echo "<option value=\"{$row['School']}\">{$row['School']}</option>"
__________________
IkoTikashi - ikotikashi.de

Reply With Quote
  #7  
Old July 10th, 2009, 02:38 PM
NewWEBdesigner NewWEBdesigner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2007
Posts: 48 NewWEBdesigner User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 58 m 41 sec
Reputation Power: 3
Thank you, it my code works now!!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP5 - Trouble with getting data from a MySQL database


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate 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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
Stay green...Green IT