Home › Forums › Back End › Insert data in to the textbox in PHP › Reply To: Insert data in to the textbox in PHP
i have started MYSQLi . but the problem my all colleagues are using mysql and as i am new. i may ask you some fullish questions while learning.
No problem. And if your job requires you to learn the mysql_
functions, then, of course, you should learn them. Even if that is the case, however, you’ll definitely be better off learning mysqli/pdo also. : )
i use mysqli . Do i need to config any file or else i can use it directly as per the syntax given in the manual
You may put your database credentials (host, username, etc.) in your php.ini file, but no: you are not required to. You may provide the credentials in your code like normal.
MySQLi has a very similar process to the old mysql_ functions. In addition to having individual functions, it also supports objects. Personally, I find objects much easier to work with, though they are a change from functional programming. For example, instead of something like this:
// connect
$db = mysqli_connect( 'host','username','password','db_name' );
if( ! $db ){ /* error: unable to connect */ }
// write your sql with _placeholders_
$sql = "select something from my_table where this=?";
// make a _prepared statement_ from your sql
$stmt = mysqli_prepare( $db,$sql );
// this attaches the data that goes with the query
// (in place of the "?"),
// but mysql will know it is _data_, so there is no security risk
mysqli_stmt_bind_param( $stmt,'s',$_GET['this'] );
// execute (send) the statement
mysql_stmt_execute( $stmt );
// get the results
mysqli_stmt_bind_results( $stmt,$something );
mysqli_stmt_fetch( $stmt );
// $something now holds the value from the DB
echo $something;
You could use the object oriented way:
// connect
$db = new mysqli( 'host','username','password','db_name );
if( $db->connect_errno ){ /* error: unable to connect */ }
// prepare your statement
$sql = "select something from my_table where this=?";
$stmt = $db->prepare( $sql );
// bind your data to the parameters ("?")
$stmt->bind_param( 's',$_GET['this'] );
// execute (send) the statement
$stmt->execute();
// get the results
$stmt->bind_result( $something );
$stmt->fetch();
echo $something;
I prefer using PDO to MySQLi (it streamlines parts of this process, and you can use named parameters (like this=:this
instead of this=?
)), but PDO has no functions (it’s all objects, like my second example above). If you are willing to work with objects, I’d recommend PDO instead of MySQLi; but I can help you with either.