|
|
|
|
![]() ![]() |
Dec 11 2006, 06:38 PM
Post
#1
|
|
|
Premium Member Group: Members Posts: 330 Joined: 2-February 06 Member No.: 11,040 |
Okay so I got this sample link (not working):
http://www.acosta.com/joo.asp?id=654 Now suppose I have a PHP file that would use MySql in order to get all values in the row where id 654 is found. Here's a sample DB: Table: demnyc ______________________________________ | id | Name | Age | Email | *----------------------------------------------------* | 1 | Albert | 17 | no email | *----------------------------------------------------* | 2 | YaPow | 888 | no email | |______________________________________| The result should be "Albert" Here's a portion of the current code to display the desired value: CODE $query = "SELECT Name FROM demnyc WHERE id=1"; $result = mysql_query($query); print "$result"; The current result is: Resource id # 4 Can someone tell me please what I'm missing? |
|
|
|
Dec 11 2006, 06:50 PM
Post
#2
|
|
|
Nenad Bozidarevic Group: [MODERATOR] Posts: 1,043 Joined: 7-November 05 From: Belgrade, Serbia Member No.: 9,500 myCENTs:38.31 |
When you use mysql_query, it doesn't return a string/number, but rather a resource. For example, if you select more rows, how would you display them? That's why mysql_result is used.
After selecting a row from the table (or rows), you will get $result. In order to use it, you must do this: CODE $someVariable = mysql_result($result,0); By doing this, $someVariable will be assigned the value from the first row of $result (0 is for the first row, 1 for the second etc). You can also add another parameter after the "0" which will indicate what column you want to use, if more have been selected. |
|
|
|
Dec 11 2006, 07:50 PM
Post
#3
|
|
|
Super Member Group: Members Posts: 595 Joined: 4-September 04 Member No.: 228 |
Yes indeed, you get the resource from which you can pull the actual data.
Mysql_result() is fine function to use if you need to fetch only one row and value from it. However these cases are quite rare and for instance I can't remember ever using the basic function. Instead use mysql_fetch_assoc() or mysql_fetch _row() which fetch one entire row from which you have access to all the values your query returned. The difference between these two is that mysql_fetch_assoc() gives you an associative array, for exmple $result['id'] would point to the id value which with mysql_fetch_row() it'd be $result[0] (presuming 'id' is mentioned first in the query). There's also a third alternative mysql_fetch_object() which doesn't return an array but an object. Nice if you like to work with objects. (duh) Under no circumstance use mysql_result() in a loop to get entire row. I've seen code like that and I nearly had a stroke. |
|
|
|
Dec 11 2006, 08:39 PM
Post
#4
|
|
|
Premium Member Group: Members Posts: 330 Joined: 2-February 06 Member No.: 11,040 |
Oh so would the syntax be like:
$result = mysql_query($query); $name = mysql_fetch_row($result[0]); Would this syntax be right? |
|
|
|
Dec 11 2006, 09:53 PM
Post
#5
|
|
|
Nenad Bozidarevic Group: [MODERATOR] Posts: 1,043 Joined: 7-November 05 From: Belgrade, Serbia Member No.: 9,500 myCENTs:38.31 |
$name = mysql_fetch_row($result[0]); No, because $result is not an array, it is a resource. $name = mysql_fetch_row($result); This, on the other hand, will work. After doing this, you will get $name - and that's an array. But, as Hercco said, you are better off with mysql_fetch_assoc(), because you will be able to use column names instead of numbers. (so that's $name["column"]). |
|
|
|
Aug 7 2008, 01:51 PM
Post
#6
|
|
|
Newbie [ Level 1 ] Group: Members Posts: 0 Joined: 1-November 07 Member No.: 25,869 |
How do I retrieve related data using Mysql/php on id query? If this make sense
PHP & MySQL: Displaying Content From A Given ID I Have set up a video website whereby a user selects a video to play, but I have other videos which I want to display that are related but I am only getting the output for the selected video. I am new to this and the frustration is killing me trying to get this to work. Can someone tell me the best way to approach this. Regds Wilburwt -question by Wilburwt |
|
|
|
Aug 7 2008, 01:57 PM
Post
#7
|
|
|
Newbie [ Level 1 ] Group: Members Posts: 0 Joined: 1-November 07 Member No.: 25,869 |
How do I retrieve related data using Mysql/php on id query? If this make sense
PHP & MySQL: Displaying Content From A Given ID I Have set up a video website whereby a user selects a video to play, but I have other videos which I want to display that are related but I am only getting the output for the selected video. I am new to this and the frustration is killing me trying to get this to work. Can someone tell me the best way to approach this. Regds Wilburwt -question by Wilburwt |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 22nd November 2008 - 04:19 PM |