|
   |
|
|
Tut: Basic MySQL querying with PHP
Tutorial name: Basic MySQL querying You're experience: Advanced Difficulty: Novice Time Taken: 10-120 seconds Plug-ins needed: none; it is pretty simple. Code: <?php //you start off by putting the php tags in. $query="select * from `users`; // you add your query into the query variable. $result=mysql_query($query, $db_connect); // depending on your php version, you may or may not need the connection info while($row=mysql_fetch_array($result)) //this fetches the information row by row. { $id=$row['id']; // the $row variable becomes an array, making it so that you can call the information that you want. echo $id; //durring this loop, you may do what ever you want. This loop terminates as soon as it hits the last row. } //lets say you want to do a single query, not where you are selecting data from the server. $query="insert into `history`"; if(mysql_query) { echo "success"; } else { echo "Error : " . mysql_error(); //this is the error function, it contains the error information of the last query sent to the server. } ?> For more information of MySQL querying with php, check out : http://ca.php.net/manual/en/ref.mysql.php For more information on MySQL querys, check out : http://dev.mysql.com/doc/refman/6.0/en/index.html Be sure to select the correct php version on the dev.mysql.com site. For more advanced users, you can also get more creative by using functions much like these: addColumIfNotExist Code: function addColumIfNotExist($table, $field, $type) { $query="ALTER TABLE $table ADD `$field` $type NOT NULL ;"; if(mysql_query($query)) { echo"</BR> $field field has been added."; } else { if(mysql_error() !== "Duplicate column name '$field'") { echo"Error: " . mysql_error(); return 2; } else { echo"</BR>$table $field field exists.\n"; } } } checkIfTableExists Code: function checkIfTableExists($name) { $check = 0; $query = "SELECT * FROM $name"; $result = mysql_query($query); while ($row = mysql_fetch_array($result)) { $check = 1; } return $check; } deleteTable Code: function deleteTable($tablename) { $query= "DROP TABLE $tablename ;"; if(mysql_query($query)) { echo"Table $tablename has been successfully deleted\n"; } else { echo"Error: Table $tablename has failed to delete\n" . mysql_error(); exit; } } update Code: function update($table, $set, $to, $where, $equals) { if($where !== NULL) { $query="UPDATE $table SET $set='$to' WHERE $where='$equals'"; } else { $query="UPDATE $table SET $set='$to'"; } if(mysql_query($query)) { return 1; } else { return mysql_error(); } } A Noobs World © 2007-2008 |
|
 |
|
No reactions yet.
Please login or sign up to rate this intel.
Please login or sign up to add a comment.
The copyright for this content entitled "Tut: Basic MySQL querying with PHP" has been specified by the contributor as:
All Rights Reserved
This content may not be copied, distributed or adapted by anyone under any circumstances.
|
 |
|
This intel was contributed by hotnoob
|
May, 2012
2008
January, February, March, April, May, June, July, August, September, October, November, December
2009
January, February, March, April, May, June, July, August, September, October, November, December
2010
January, February, March, April, May, June, July, August, September, October, November, December
2011
January, February, March, April, May, June, July, August, September, October, November, December
2012
January, February, March, April, May
|
|
Not a member yet?
Qondio is a powerful network for making it online. If you have a website to
promote, we can help.
Sign up and get in on the action.
|
|
Welcome to Qondio! Discover the awesome power this network can deliver by going to our About page. Or you could skip straight to the Sign Up form.
|
|