Re: [PHP-DB] new pair of eyes
Hey there, I was just looking at your code, and I also look at other's code, and I think people are making life difficult for themselves. Sure a lot of people on this list are beginners, but thats more te reason to simplify code... One thing you should always do, is write functions... I have written a few functions to connect to a database, execute queries and return results, and insert and update queries. Using functions like these make it a lot easier to write working code, and because it's less typing, its a lesser chance of an error occurring... for example... here is a portion of my lib.php which I include() on each page I create (some comments are removed to save space) putting the below code into an external file and including it on every page makes everything a lot cleaner, and if you need to connect to a database, just call dbConnect(); in your php script... you must make sure that in the external file, the constants are set with your server details... or you can do it in the actual page with the function, like so: dbConnect("dbname","localhost","username","password"); and if you need the connection ID, it is in the variable $DB_CONN. If you want some more functions like this (i have a few more called getData(), dbInsert(), dbUpdate() which all make it a LOT easier to transport data to and from a mysql database), email me and I will send them to you directly. If you don't run mySQL, then you can just modify the functions to suit your DB, or you can add and delete things inside the functions for your own setup. Adam / DEFINE CONSTANTS / // Connecting to a database define( "DB_SERVER" , "localhost" ); // used for dbConnect() define( "DB_USERNAME" , "username" ); // used for dbConnect() define( "DB_PASSWORD" , "password" ); // used for dbConnect() define( "DB_DATABASE" , "dbname" ); // used for dbConnect() / DATABASE FUNCTIONS / function dbConnect($dbDatabase=DB_DATABASE, $dbServer=DB_SERVER, $dbUsername=DB_USERNAME, $dbPassword=DB_PASSWORD){ global $DB_CONN; $DB_CONN = @mysql_connect($dbServer, $dbUsername, $dbPassword) or die("Couldn't connect to database server."); $db = @mysql_select_db($dbDatabase) or die("Couldn't select database."); return $DB_CONN; } -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] new pair of eyes
If I want to delete a record, I simply put this (the rest is not necessary): $sql="DELETE from $tableName where ROWID=$rowid"; OK, now the complete routine would be: $deleteStmt="DELETE from $tableName where ROWID=$rowid" ; //Connect to the DB if (!($link=mysql_pconnect($hostName, $userName, $password))) { DisplayErrMsg(spintf("error conectando al host %s por el usuario %s", $hostName, $userName)); exit(); } //Select DB if (!mysql_select_db($databaseName, $link)) { DisplayErrMsg(sprintf("Error seleccionando %s database", $databaseName)); DisplayErrMsg(sprintf("error: %d %s", mysql_errno($link), mysql_error($link))) ; exit(); } //Execute the Statement if (!mysql_query($deleteStmt, $link)) { DisplayErrMsg(sprintf("Error in ejecutando %s mandato", $deleteStmt)) ; DisplayErrMsg(sptintf("error: %d %s", mysql_erro($link), mysql_error($link))) ; exit(); } - Jp At 12:30 31/05/02, Jas wrote: >I think I am missing something: >$db_name = "database"; > $table_name = "auth_users"; > $sql = "DELETE user_id, f_name, l_name, email_addy, un, pw FROM $table_name >WHERE user_id = \"$user_id\", $dbh"; > $result = @mysql_query($sql, $dbh) or die ("Could not execute query. >Please try again later."); >Its not deleting the records based on a hidden field in a form named >$user_id >Any help would be great! >Jas > > > >-- >PHP Database Mailing List (http://www.php.net/) >To unsubscribe, visit: http://www.php.net/unsub.php Juan Pablo Aqueveque <[EMAIL PROTECTED]> Ingeniero de Sistemas Departamento de Redes y Comunicaciones http://www.drc.uct.cl Universidad Católica de Temuco. Tel:(5645) 205 630 Fax:(5645) 205 628 -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] new pair of eyes
On Saturday 01 June 2002 00:30, Jas wrote: > I think I am missing something: > $db_name = "database"; > $table_name = "auth_users"; > $sql = "DELETE user_id, f_name, l_name, email_addy, un, pw FROM > $table_name WHERE user_id = \"$user_id\", $dbh"; The MySQL syntax for DELETE is: DELETE FROM table [WHERE ...] thus there is no need for user_id, f_name etc. -- Jason Wong -> Gremlins Associates -> www.gremlins.com.hk Open Source Software Systems Integrators * Web Design & Hosting * Internet & Intranet Applications Development * /* The number of licorice gumballs you get out of a gumball machine increases in direct proportion to how much you hate licorice. */ -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php