Jump to content
xisto Community
Sign in to follow this  
hellonull

How To Use MySQL, PHP And Recordsets ? Using info retrieved from MySQL via PHP

Recommended Posts

I am just beginning to use PHP and MySQL together. I was wondering, in PHP, how do you access information you have retrieved from a MySQL database? I've used ASP with an MS Access database before, and in that scenario the information returned through an SQL query can be stored in a recordset. Basically, I am wanting to know how to do this in PHP.

Edited by microscopic^earthling (see edit history)

Share this post


Link to post
Share on other sites

You'll need to use queries for that.

$q = mysql_query("SELECT $columns FROM $table")
$q will be a MySQL query result. To get the data from it use something like this:
$qRow = mysql_fetch_array($q);
$qRow will contain one row of the MySQL query. It will be in the form of an associative array. For example, if you have a table like this:
NAME | ADDRESS

John | MyStreetRox 89
Pete | MyStreetRoxMore 12

then your $qRow will contain the array with as key: [Name] and as value "John". And as key [Address] and as value "MyStreetRox89"

Good Luck

( echo $qRow["Name"]; will give you John )

Share this post


Link to post
Share on other sites

Thanks lhunath. Your reply is just what I was looking for, getting results from the query and then extracting data from a specific "cell" (as it would appear in Access). I've implemented what you've said into my code and I am able to get the desired results now. Thanks again.

Share this post


Link to post
Share on other sites

There are lot's of handy functions in PHP to be used with MySQL. I strongly recommend glancing through this page http://php.net/manual/en/ref.mysql.php

 

There are lots of stuff that can make your life easier. I remember when I started using PHP & MySQL combo, I stubbornly programmed lots of stuff myself, when I didn't know of all things that were just function call away.

 

So it is a good idea to check the manual to know roughly what kind of features are available. When you program something, instead of implementing it yourself, it might come to your mind that there could be a ready function for this.

Share this post


Link to post
Share on other sites

I went to the manual but I am not able to find the function that moves the value of a variable to the next item in an array of MySQL query results. What would be the function (I'm pretty sure that there is one for something like this) and how would I use it? Or, simply put, what is the PHP equivalent of ASP's RS.MoveNext function?

Share this post


Link to post
Share on other sites

I'm not exactly sure what you want to do. But what I know is, that PHP does not really have that many functions to use MySQL directly. They like you better to use mysql_query() and then the MySQL code in it. Makes it easier for you since you already know mysql. If you don't know that command, try finding out what the ASP-function actually does! Look on the bright side you don't have to learn it new completely.. :-)

Share this post


Link to post
Share on other sites

I'm not exactly sure what you want to do. But what I know is, that PHP does not really have that many functions to use MySQL directly. They like you better to use mysql_query() and then the MySQL code in it. Makes it easier for you since you already know mysql. If you don't know that command, try finding out what the ASP-function actually does! Look on the bright side you don't have to learn it new completely.. :-)

1064327654[/snapback]

Actually PHP has about 31 functions for just the MySQL:

 

mysql_affected_rows (get the number of affected rows in the pervious MySQL operation),

mysql_close (close a MySQL connection),

mysql_connect (open a connection to MySQL server),

mysql_create_db ( create a MySQL database),

mysql_data_seek (mover the internal result pointer),

mysql_db_query (Send an SQ: query to MySQL),

mysql_drop_db (Drop a MySQL database),

mysql_return_errno (Returns the error number of the pervious MySQL operation),

mysql_error (Returns the text of the error message from the previous MySql operation),

mysql_fetch_array (Fetches a result row as an associative array),

mysql_fetch_field (Get column information from a result and return it as an object),

mysql_fetch_length (Gets the maximum data size of each column in a result),

mysql_fetch_object (Fetches a result row ans an object),

mysql_fetch_row (Get a result row as an enumerated array),

mysql_field_flags (Gets the flags associate with the specified field in a result),

mysql_field_len (Returns the length of a  specified field),

mysql_field_name (Get the name of the specified field in a result),

mysql_field_seek (Set the pointer to a specific field offset),

mysql_field_table (Get the name of the table the specified field is in),

mysql_field_type (Get the type of the specified field in a result),

mysql_free_result (free result memory),

mysql_insert_id (Get the ID generated from the previous INSERT operation),

mysql_list_dbs (Lists the databases available on a MySQL server),

mysql_list_fields (List MySQL result fields),

mysql_list_tables (Lists the tables in a MySQL database),

mysql_num_fields (Get the number of fields in a result),

mysql_num_rows (Get the number of rows in a result),

mysql_pconnect (Open a persistent connection to a MySQL database server),

mysql_query (Send anSQL query to MySQL),

mysql_result (Get result data)and of course

mysql_select_db (Select a MySQL database)

 

 

Plus for mSQL there are 28, for Oracle there are 40, forPostgreSQL there are 32, and there is Sybase, InterBase with 11, and several with Informix and Hyperwave... I could have made un unordered list but I used that format to save space in the post and not have a long list of PHP functions.

Edited by jipman (see edit history)

Share this post


Link to post
Share on other sites

There are a lot of good tutorials which can be easily found by using google how to start using mysql+php being a newbie which explains everything better and more clear than the php.net manual, so I would recommend to take 1 minute to find one and read it.

Share this post


Link to post
Share on other sites

Yes man, but if you would make the effort and read through the descriptions on php.net then you could easily find a lot of notes saying "pleae use mysql-code blahblahblah instead".If you are new to MySQL then you are better off using MySQL code in mysql_query, that's what I was trying to say.

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.