Forgot Password / Register
Site Statistics
Total Members: 510
Total Tutorials: 242
Newsest User: Hp
Todays Unique Hits: 130
0 Users 2 Guests Online

MySQL and PHP commands


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

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

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

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

$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

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 || 0 | abcabc |
| Namco | None | 0 | qwerty |
Okay now lets get the info.

$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
There are currently no comments for this tutorial.