User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 401,422 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,908 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 728 | Replies: 31 | Solved
Reply
Join Date: Mar 2008
Posts: 85
Reputation: justinmyoung is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
justinmyoung justinmyoung is offline Offline
Junior Poster in Training

please help with pulling data from database...

  #1  
Jul 24th, 2008
I'm trying pull up data from database, and display it on a table. I used this code before but for some reason it won't work here. Any suggestions? Thanks!

It says the error is this line: <?php while ($list = mysql_fetch_assoc($result)) {?>
The error message is: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /Users/laurenyoung/Sites/runners.php on line 60

<?php

// Include connection to your database
	$con = mysql_connect("","","");
	mysql_select_db("RUNNERS", $con);
	
	$name = $_POST['name']; 
	$query = "SELECT * FROM runners WHERE id = '$name'";
	$result = mysql_query($query);?>

	<table class="sortable">
	<!-- Table Header -->
	<thead>
	<tr>
	<th>First Name</th>
	<th>Last Name</th>
	<th>1 Mile</th>
	<th>2 Mile</th>
	<th>5k</th>
	<th>8k</th>
	<th>10k</th>
	<th>15k</th>
	<th>20k</th>
	<th>13.1</th>
	<th>25k</th>
	<th>26.2</th>
	</tr>
	</thead>

<!-- Table body-->
	<tbody>
	<?php while ($list = mysql_fetch_assoc($result)) {?>
	<tr>
	<th><?php echo $list['first_name'] ?></th>
	<th><?php echo $list['last_name'] ?></th>
	<th><?php echo $list['1_mile'] ?></th>
	<th><?php echo $list['2_mile'] ?></th>
	<th><?php echo $list['5k'] ?></th>
	<th><?php echo $list['8k'] ?></th>
	<th><?php echo $list['10k'] ?></th>
	<th><?php echo $list['15k'] ?></th>
	<th><?php echo $list['20k'] ?></th>
	<th><?php echo $list['half'] ?></th>
	<th><?php echo $list['25k'] ?></th>
	<th><?php echo $list['full'] ?></th>
	</tr>
	<?php } ?>
	</tbody>


</table>
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2008
Location: Lahore, Pakistan
Posts: 36
Reputation: mustafaneguib is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
mustafaneguib mustafaneguib is offline Offline
Light Poster

Re: please help with pulling data from database...

  #2  
Jul 24th, 2008
try to use
  1. <?php while ($list = mysql_fetch_array($result)) {?>

instead of,

  1. <?php while ($list = mysql_fetch_assoc($result)) {?>
Last edited by mustafaneguib : Jul 24th, 2008 at 6:50 pm.
Reply With Quote  
Join Date: Mar 2008
Posts: 85
Reputation: justinmyoung is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
justinmyoung justinmyoung is offline Offline
Junior Poster in Training

Re: please help with pulling data from database...

  #3  
Jul 24th, 2008
Still got the same error, but thanks for trying. Perhaps it's something wrong with the code I use on the initial page, which looks like this:

<form action="runners.php" method="post">

		   
		
	
	<label for="first_name" style="font-size: 14px; margin-top: 10px; font-weight: bold; font-variant: small-caps;">First Name:</label>
	<input type="text" name="name" value="" maxlength="30" style="width:49.6%; font-size: 30px;"<br /><?php if    (isset($_POST['first_name'])) echo $_POST['first_name']; ?><br /></p>
	<label for="last_name" style="font-size: 14px; margin-top: 10px; font-weight: bold; font-variant: small-caps;">Last Name:</label>
	<input type="text" name="storeid" value="" maxlength="30" style="width:49.6%; font-size: 30px;"<br /><?php if    (isset($_POST['last_name'])) echo $_POST['last_name']; ?><br /></p>
	
	<form method="post" action="runners.php">
	
	<input type="submit" style="width:90px; height: 35px; margin-left: 260px; -moz-border-radius: 10px;" name="submit" value="get info">
</form>

Thanks again!
Reply With Quote  
Join Date: Mar 2008
Posts: 85
Reputation: justinmyoung is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
justinmyoung justinmyoung is offline Offline
Junior Poster in Training

Re: please help with pulling data from database...

  #4  
Jul 24th, 2008
Well I got it to come up with no errors, but it's not pulling the data:


<form action="runners.php" method="post">

		   
		
	
	<label for="first_name" style="font-size: 14px; margin-top: 10px; font-weight: bold; font-variant: small-caps;">First Name:</label>
	<input type="text" name="first_name" value="" maxlength="30" style="width:49.6%; font-size: 30px;"<br /><?php if    (isset($_POST['first_name'])) echo $_POST['first_name']; ?><br /></p>
	<label for="last_name" style="font-size: 14px; margin-top: 10px; font-weight: bold; font-variant: small-caps;">Last Name:</label>
	<input type="text" name="storeid" value="" maxlength="30" style="width:49.6%; font-size: 30px;"<br /><?php if    (isset($_POST['last_name'])) echo $_POST['last_name']; ?><br /></p>
	
	<form method="post" action="runners.php">


<?php
	// Include connection to your database
	$con = mysql_connect("localhost","root","");
	mysql_select_db("RUNNERS", $con);
	
	$name = $_POST['first_name']; 
	$query = "SELECT * FROM runners WHERE id = '$name'";
	$result = mysql_query($query);?>

	<table class="sortable">
	<!-- Table Header -->
	<thead>
	<tr>
	<th>ID #</th>
	<th>First Name</th>
	<th>Last Name</th>
	<th>1 Mile</th>
	<th>2 Mile</th>
	<th>5k</th>
	<th>8k</th>
	<th>10k</th>
	<th>20k</th>
	<th>13.1</th>
	<th>26.2</th>
	</tr>
	</thead>




	<!-- Tabel body-->
	<tbody>
	<?php while ($list = mysql_fetch_assoc($result)) {?>
	<tr>
	<th><?php echo $list['first_name'] ?></th>
	<th><?php echo $list['last_name'] ?></th>
	<th><?php echo $list['one'] ?></th>
	<th><?php echo $list['two'] ?></th>
	<th><?php echo $list['five'] ?></th>
	<th><?php echo $list['ten'] ?></th>
	<th><?php echo $list['fifteen'] ?></th>
	<th><?php echo $list['twenty'] ?></th>
	<th><?php echo $list['half'] ?></th>
	<th><?php echo $list['full'] ?></th>
	</tr>
	<?php } ?>
	</tbody>


</table>

Thanks for any help!
Reply With Quote  
Join Date: Jun 2008
Location: hyderabad,india
Posts: 95
Reputation: praveen_dusari is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 11
praveen_dusari's Avatar
praveen_dusari praveen_dusari is offline Offline
Junior Poster in Training

Re: please help with pulling data from database...

  #5  
Jul 24th, 2008
hi,
echo your sql query. you didn`t put die() it will display error if database is selected or not
Failure is success if we learn from it
Reply With Quote  
Join Date: Jul 2008
Location: Hyderabad,India.
Posts: 534
Reputation: Shanti Chepuru is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 53
Shanti Chepuru's Avatar
Shanti Chepuru Shanti Chepuru is offline Offline
Posting Pro

Re: please help with pulling data from database...

  #6  
Jul 24th, 2008
change this :
  1. $query = "SELECT * FROM runners WHERE id = '$name'";

To:
  1. $query = "SELECT * FROM runners WHERE id =".$name;
Champions Are Not SuperNatural,They Just Fought For One More Second When Every One Else Quit...Some times That One Second Of Effort Gives You The VICTORY...
Reply With Quote  
Join Date: Mar 2008
Posts: 85
Reputation: justinmyoung is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
justinmyoung justinmyoung is offline Offline
Junior Poster in Training

Re: please help with pulling data from database...

  #7  
Jul 25th, 2008
1. Looks like I'm connecting fine. I added the die function.

2. I changed $query = "SELECT * FROM runners WHERE id = '$name'"; to $query = "SELECT * FROM runners WHERE id =".$name; , but I'm receiving this error: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /Users/laurenyoung/Sites/runners.php on line 67. Line 67 = <?php while ($list = mysql_fetch_assoc($result)) ?>
Here's how I have the code as it stands now:

<?php
	// Include connection to your database
	$con = mysql_connect("localhost","root","");
	mysql_select_db("RUNNERS", $con);
	
	if (!$con)
	  {
	  die('Could not connect: ' . mysql_error());
	  }
	
	$name = $_POST['first']; 
	$query = "SELECT * FROM runners WHERE id =".$name;
	$result = mysql_query($query);?>

<table class="sortable">
	<!-- Table Header -->
	<thead>
	<tr>
	<th>ID #</th>
	<th>First Name</th>
	<th>Last Name</th>
	<th>1 Mile</th>
	<th>2 Mile</th>
	<th>5k</th>
	<th>8k</th>
	<th>10k</th>
	<th>20k</th>
	<th>13.1</th>
	<th>26.2</th>
	</tr>
	</thead>

<!-- Table body-->
	<tbody>
	<?php while ($list = mysql_fetch_assoc($result)) ?>
	<tr>
	<th><?php echo $list['first_name'] ?></th>
	<th><?php echo $list['last_name'] ?></th>
	<th><?php echo $list['one'] ?></th>
	<th><?php echo $list['two'] ?></th>
	<th><?php echo $list['five'] ?></th>
	<th><?php echo $list['ten'] ?></th>
	<th><?php echo $list['fifteen'] ?></th>
	<th><?php echo $list['twenty'] ?></th>
	<th><?php echo $list['half'] ?></th>
	<th><?php echo $list['full'] ?></th>
	</tr>
	</tbody>


</table>


That's the only error I'm receiving, however. I would like to have the user enter his/her first and last name to pull the data. I thought that was how I had it written, but it is still, obviously, given me trouble.

Thanks!
Reply With Quote  
Join Date: Jul 2008
Location: Hyderabad,India.
Posts: 534
Reputation: Shanti Chepuru is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 53
Shanti Chepuru's Avatar
Shanti Chepuru Shanti Chepuru is offline Offline
Posting Pro

Re: please help with pulling data from database...

  #8  
Jul 25th, 2008
so you dont have any colums with that name $_POST['first'] in you table...

first execute your query in mysql with the same name,and put in your php page...
Champions Are Not SuperNatural,They Just Fought For One More Second When Every One Else Quit...Some times That One Second Of Effort Gives You The VICTORY...
Reply With Quote  
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 599
Reputation: R0bb0b is on a distinguished road 
Rep Power: 2
Solved Threads: 53
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Posting Pro

Re: please help with pulling data from database...

  #9  
Jul 25th, 2008
put in more die functions.

  1. <?php
  2. // Include connection to your database
  3. $con = mysql_connect("localhost","root","") or die(mysql_error());;
  4. mysql_select_db("RUNNERS", $con) or die(mysql_error());
  5.  
  6. $name = $_POST['first'];
  7. $query = "SELECT * FROM runners WHERE id =".$name;
  8. $result = mysql_query($query) or die(mysql_error());?>
  9.  
  10. <table class="sortable">
  11. <!-- Table Header -->
  12. <thead>
  13. <tr>
  14. <th>ID #</th>
  15. <th>First Name</th>
  16. <th>Last Name</th>
  17. <th>1 Mile</th>
  18. <th>2 Mile</th>
  19. <th>5k</th>
  20. <th>8k</th>
  21. <th>10k</th>
  22. <th>20k</th>
  23. <th>13.1</th>
  24. <th>26.2</th>
  25. </tr>
  26. </thead>
  27.  
  28. <!-- Table body-->
  29. <tbody>
  30. <?php while ($list = mysql_fetch_assoc($result)) ?>
  31. <tr>
  32. <th><?php echo $list['first_name'] ?></th>
  33. <th><?php echo $list['last_name'] ?></th>
  34. <th><?php echo $list['one'] ?></th>
  35. <th><?php echo $list['two'] ?></th>
  36. <th><?php echo $list['five'] ?></th>
  37. <th><?php echo $list['ten'] ?></th>
  38. <th><?php echo $list['fifteen'] ?></th>
  39. <th><?php echo $list['twenty'] ?></th>
  40. <th><?php echo $list['half'] ?></th>
  41. <th><?php echo $list['full'] ?></th>
  42. </tr>
  43. </tbody>
  44.  
  45.  
  46. </table>

I agree with Shanti, there is something wrong with your query and this should tell you what it is.
Last edited by R0bb0b : Jul 25th, 2008 at 12:49 am.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
Reply With Quote  
Join Date: Apr 2008
Location: Lahore, Pakistan
Posts: 36
Reputation: mustafaneguib is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
mustafaneguib mustafaneguib is offline Offline
Light Poster

Re: please help with pulling data from database...

  #10  
Jul 25th, 2008
i think ive found the problem. ur using while loop when your only looking for one (as far as i can see) record. so remove the while statement, and use the following,

  1. $list=mysql_fetch_array($result);
instead of it because your problem lies in this line.


  1.  
  2. <!-- Table body-->
  3. <tbody>
  4. <?php $list=mysql_fetch_array($result); ?> //change this to this
  5. <tr>
  6. <th><?php echo $list['first_name'] ?></th>
  7. <th><?php echo $list['last_name'] ?></th>
  8. <th><?php echo $list['one'] ?></th>
  9. <th><?php echo $list['two'] ?></th>
  10. <th><?php echo $list['five'] ?></th>
  11. <th><?php echo $list['ten'] ?></th>
  12. <th><?php echo $list['fifteen'] ?></th>
  13. <th><?php echo $list['twenty'] ?></th>
  14. <th><?php echo $list['half'] ?></th>
  15. <th><?php echo $list['full'] ?></th>
  16. </tr>
  17. </tbody>
  18.  
  19.  
  20. </table>
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 12:24 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC