Jump to content

Recommended Posts

While visiting the PHP.net site after following a link from one of the other posts here, I found something that highlights something that confuses me regarding the language.  The function in question isn't of importance (mysqli_stmt_prepare), but the examples are:

OO style of using the function:
[code]
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
  printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
}

$city = "Amersfoort";

/* create a prepared statement */
$stmt =  $mysqli->stmt_init();
if ($stmt->prepare("SELECT District FROM City WHERE Name=?")) {

  /* bind parameters for markers */
  $stmt->bind_param("s", $city);

  /* execute query */
  $stmt->execute();

  /* bind result variables */
  $stmt->bind_result($district);

  /* fetch value */
  $stmt->fetch();

  printf("%s is in district %s\n", $city, $district);

  /* close statement */
  $stmt->close();
}

/* close connection */
$mysqli->close();
?>
[/code]

Procedural style of using the function:
[code]
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
  printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
}

$city = "Amersfoort";

/* create a prepared statement */
$stmt = mysqli_stmt_init($link);
if ($stmt = mysqli_stmt_prepare($stmt, "SELECT District FROM City WHERE Name=?")) {

  /* bind parameters for markers */
  mysqli_stmt_bind_param($stmt, "s", $city);

  /* execute query */
  mysqli_stmt_execute($stmt);

  /* bind result variables */
  mysqli_stmt_bind_result($stmt, $district);

  /* fetch value */
  mysqli_stmt_fetch($stmt);

  printf("%s is in district %s\n", $city, $district);

  /* close statement */
  mysqli_stmt_close($stmt);
}

/* close connection */
mysqli_close($link);
?>
[/code]

Is the 'arrow' notation used in the first example always just representative of a truncated function name?  Could I do something like the following?
[code]
<?php

$dbc = $mysql -> connect($username, $host, $password);
$db = $mysql -> select_db('dbname', $dbc);

?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/26328-syntax-question-oo-vs-procedural/
Share on other sites

[quote author=thorpe link=topic=114011.msg463682#msg463682 date=1162829305]
[quote]Is the 'arrow' notation used in the first example always just representative of a truncated function name?[/quote]

No. In the example mysqli is an object, not a function.
[/quote]

There doesn't seem to be much difference, though.  I mean, there are two ways to initialize it:
[code]
<?php

$stmt =  $mysqli->stmt_init(); //the OO way

$stmt = mysqli_stmt_init($link); //the procedural way

?>
[/code]

The former uses an object...is there a mysql object where it could be used in the way I theorized in my last post (code repeated below)?

[code]
<?php

$dbc = $mysql -> connect($username, $host, $password);
$db = $mysql -> select_db('dbname', $dbc);

?>
[/code]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.