Change PHP PEAR for mysql connection -
i'm working along tutorial on how import excel data mysql. problem have used pear: database connection , don't know how works. convert code commonly used mysql connection string. i'm sure i've never seen pear or db::connect used before.
here's code below.
<?php require_once( "db.php" ); $data = array(); $db =& db::connect("mysql://root@localhost/names", array()); if (pear::iserror($db)) { die($db->getmessage()); } function add_person( $first, $middle, $last, $email ) { global $data, $db; $sth = $db->prepare( "insert names values( 0, ?, ?, ?, ? )" ); $db->execute( $sth, array( $first, $middle, $last, $email ) ); $data []= array( 'first' => $first, 'middle' => $middle, 'last' => $last, 'email' => $email ); } if ( $_files['file']['tmp_name'] ) { $dom = domdocument::load( $_files['file']['tmp_name'] ); $rows = $dom->getelementsbytagname( 'row' ); $first_row = true; foreach ($rows $row) { if ( !$first_row ) { $first = ""; $middle = ""; $last = ""; $email = ""; $index = 1; $cells = $row->getelementsbytagname( 'cell' ); foreach( $cells $cell ) { $ind = $cell->getattribute( 'index' ); if ( $ind != null ) $index = $ind; if ( $index == 1 ) $first = $cell->nodevalue; if ( $index == 2 ) $middle = $cell->nodevalue; if ( $index == 3 ) $last = $cell->nodevalue; if ( $index == 4 ) $email = $cell->nodevalue; $index += 1; } add_person( $first, $middle, $last, $email ); } $first_row = false; } } ?> <html> <body> these records have been added database: <table> <tr> <th>first</th> <th>middle</th> <th>last</th> <th>email</th> </tr> <?php foreach( $data $row ) { ?> <tr> <td><?php echo( $row['first'] ); ?></td>< <td><?php echo( $row['middle'] ); ?></td>< <td><?php echo( $row['last'] ); ?></td>< <td><?php echo( $row['email'] ); ?></td>< </tr> <?php } ?> </table> click <a href="list.php">here</a> entire table. </body> </html>
i assume post want mysql_*
functions, not newer mysqli
class. (personally, prefer latter oo-interface.)
then change
$db =& db::connect("mysql://root@localhost/names", array()); if (pear::iserror($db)) { die($db->getmessage()); }
to
$db = @mysql_connect('localhost', 'user', 'pass') or die ( mysql_error() ); mysql_select_db('db_name', $db);
and change
$sth = $db->prepare( "insert names values( 0, ?, ?, ?, ? )" ); $db->execute( $sth, array( $first, $middle, $last, $email ) );
to
$query = sprintf( 'insert names values( 0, "%s", "%s", "%s", "%s" )' , mysql_real_escape_string($first), mysql_real_escape_string($middle), mysql_real_escape_string($last), mysql_real_escape_string($email) ); mysql_query($query, $db);
Comments
Post a Comment