Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> PHP & MySQL: Displaying Content From A Given ID
demolaynyc
post Dec 11 2006, 06:38 PM
Post #1


Premium Member
Group Icon

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?
Go to the top of the page
 
+Quote Post
pyost
post Dec 11 2006, 06:50 PM
Post #2


Nenad Bozidarevic
Group Icon

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.
Go to the top of the page
 
+Quote Post
Hercco
post Dec 11 2006, 07:50 PM
Post #3


Super Member
Group Icon

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. tongue.gif

Go to the top of the page
 
+Quote Post
demolaynyc
post Dec 11 2006, 08:39 PM
Post #4


Premium Member
Group Icon

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?
Go to the top of the page
 
+Quote Post
pyost
post Dec 11 2006, 09:53 PM
Post #5


Nenad Bozidarevic
Group Icon

Group: [MODERATOR]
Posts: 1,043
Joined: 7-November 05
From: Belgrade, Serbia
Member No.: 9,500
myCENTs:38.31



QUOTE(demolaynyc @ Dec 11 2006, 09:39 PM) *

$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"]).
Go to the top of the page
 
+Quote Post
iGuest
post Aug 7 2008, 01:51 PM
Post #6


Newbie [ Level 1 ]
Group Icon

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
Go to the top of the page
 
+Quote Post
iGuest
post Aug 7 2008, 01:57 PM
Post #7


Newbie [ Level 1 ]
Group Icon

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
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Small Issues About Mysql Connector/j 5.1(2)
  2. Ajax + Php + Sql = Simply Superb! ( With Visitor Tracking )(11)
  3. MySQL For EasyPHP Users(5)
  4. Login System(6)
  5. CuteNews: PHP-based Blog System - No MySQL(11)
  6. Which Language Is Easy And Secure Today For Web Development(2)
  7. Php Tutorial: Making A Shoutbox(12)
  8. Mysql Overhead(3)
  9. Navcat For MySQL(9)
  10. What You Need Before You Can Create A Text-based Game..(7)
  11. Important: Basics Of Using PHP And MySQL(10)
  12. [PHP + MySQL] Encrypting Data(11)
  13. Qupis : Free Cpanel Web Hosting (one Line Text Ad At Bottom)(10)
  14. MySQL Realtime Replication(4)
  15. How To: Display A Members/user List.(3)
  1. MySQL Output Database Question(18)
  2. Any Website Provide Free Host Mysql Host?(4)
  3. Recover Tables From A MySQL .frm File(8)
  4. Mysql Multiple Tables(1)
  5. Login System Using A Mysql Db(5)
  6. MySQL, Multiple Tables(24)
  7. Mysql Database Entry By Excel Sheets(2)
  8. Mysql On Computer(9)
  9. Need Help With A PHP - MySQL Registration Script(13)
  10. MySQL - Trouble With Bulk Insert Statements(3)
  11. Can You Link Game Maker With Mysql/php(0)
  12. Using Blogger To Add Content(9)
  13. Mysql With My Own Server(4)
  14. Mysql Database Management(1)
  15. Mysql And User File_priv(0)


 



- Lo-Fi Version Time is now: 22nd November 2008 - 04:19 PM