Jump to content
xisto Community
Sign in to follow this  
demolaynyc

PHP & MySQL: Displaying Content From A Given ID

Recommended Posts

Okay so I got this sample link (not working):

 

http://forums.xisto.com/no_longer_exists/

 

 

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:

 

$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?

Share this post


Link to post
Share on other sites

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:

 

$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.

Share this post


Link to post
Share on other sites

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. :P

Share this post


Link to post
Share on other sites

$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"]).

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.