Forgot Password / Register
Site Statistics
Total Members: 520
Total Tutorials: 242
Newsest User: 8884244477
Todays Unique Hits: 624
0 Users 11 Guests Online

MySQL and PHP commands

x-----------x
| MYSQL/PHP |
| COMMANDS |
x-----------x

Okay, this is my very first tutorial... ever. I will be explaining lots of mysql functions in php. Here we go...

MYSQL_FETCH_ARRAY
This is the code lots of people like to use to get information from the database. The code basically goes like this...
Code

<?
$select_info= mysql_query("SELECT * FROM users WHERE userlevel='0' ORDER BY `id` ASC");
// Look for users with a userlevel of 0 and order the results by the users ID ascending

$the_users= mysql_fetch_array($select_info); // Get the user so you can select various results.

//Okay this is where im a bit rusty since i havnt used fetch arrays before xD

echo "$the_users['username']"; //I have no idea if thats correct i think it might be this or...
echo "$the_users[1]";
?>


The above code will hopefully get a result from the database and put the username of a user on the page.

MYSQL_FETCH_OBJECT
Ahh, my favourite function. This good function is the same as a fetch array but works differently. Take a look at the code below...
Code

<?
$select_info= mysql_query("SELECT * FROM users WHERE userlevel='0' ORDER BY `id` ASC"); //Okay im using the same mysql query from before so you can easily contrast the both.

$the_users= mysql_fetch_object($select_info); // The same thing but i changed array to object

echo "Hello $the_users->username"; // Okay basically this one is saying Hello (the users Username from the mysql result).
?>

Okay as you can see, instead of using $the_users['username'] i used $the_users->username. The function, when echoed, works like this: $variable_here->table_row e.g. username

MYSQL_NUM_ROWS
Okay mysql num rows is a descent and useful command used for recording the number of results found from a query. Okay first lets see a table.
Table name: users
------------------------------------------
| Username | Email | Userlevel | Password |
|_________________________________________
| Ilyas | None | 3 | 123123 |
| Killer | k@h.com| 0 | abcabc |
| Namco | None | 0 | qwerty |
|_________________________________________|
Okay now lets get the info.
Code

<?
$select= mysql_query("SELECT * FROM users WHERE userlevel='0'"); // Im sure we dont have to go thru this again :)

$number_of_results= mysql_num_rows($select); //Using the mysql num rows function to get the number of results.

echo "$number_of_results"; // This will echo the number 2 since there is only 2 users in the table with a userlevel of '0'.
?>

Notice that the mysql_num_rows function does not need an arrow ( -> ) or a [ blabla ] to work. U just use the variable only.

Okay thats all for now. Until next time.
I may have made mistakes since i rushed it so plz correct me. Thanks
ilyas-shezad
Views:
2210
Rating:
There are currently no comments for this tutorial.