diff --git a/reference/mysqli/functions/mysqli-affected-rows.xml b/reference/mysqli/functions/mysqli-affected-rows.xml index 817889ac08..bf8e96a0c2 100644 --- a/reference/mysqli/functions/mysqli-affected-rows.xml +++ b/reference/mysqli/functions/mysqli-affected-rows.xml @@ -1,5 +1,5 @@ - + mysqli_affected_rows @@ -59,74 +59,100 @@ Example - - - Object oriented style - - + Object oriented style + +query("DROP TABLE IF EXISTS affected_rows"); -$mysqli->query("CREATE TABLE affected_rows (a int)"); -$mysqli->query("INSERT INTO affected_rows VALUES (1),(2),(3),(4)"); +/* Insert rows */ +$mysqli->query("CREATE TABLE Language SELECT * from CountryLanguage"); +printf("Affected rows (INSERT): %d\n", $mysqli->affected_rows); -/* update values and retrieve number of affected rows */ -$mysqli->query("UPDATE affected_rows SET a=5 WHERE a=1"); -printf("Affected rows (update): %d\n", $mysqli->affected_rows); +$mysqli->query("ALTER TABLE Language ADD Status int default 0"); -/* delete rows and retrieve number of affected_rows */ -$mysqli->query("DELETE FROM affected_rows WHERE a < 4"); -printf("Affected rows (delete): %d\n", $mysqli->affected_rows); +/* update rows */ +$mysqli->query("UPDATE Language SET Status=1 WHERE Percentage > 50"); +printf("Affected rows (UPDATE): %d\n", $mysqli->affected_rows); -/* select all rows and retrieve number of affected_rows */ -$mysqli->query("SELECT a FROM affected_rows"); -printf("Affected rows (select): %d\n", $mysqli->affected_rows); +/* delete rows */ +$mysqli->query("DELETE FROM Language WHERE Percentage < 50"); +printf("Affected rows (DELETE): %d\n", $mysqli->affected_rows); + +/* select all rows */ +$result = $mysqli->query("SELECT CountryCode FROM Language"); +printf("Affected rows (SELECT): %d\n", $mysqli->affected_rows); + +$result->close(); + +/* Delete table Language */ +$mysqli->query("DROP TABLE Language"); /* close connection */ $mysqli->close(); ?> - ]]> - - - - Procedural style - - + + + + Procedural style + + 50"); +printf("Affected rows (UPDATE): %d\n", mysqli_affected_rows($link)); + +/* delete rows */ +mysqli_query($link, "DELETE FROM Language WHERE Percentage < 50"); +printf("Affected rows (DELETE): %d\n", mysqli_affected_rows($link)); + +/* select all rows */ +$result = mysqli_query($link, "SELECT CountryCode FROM Language"); +printf("Affected rows (SELECT): %d\n", mysqli_affected_rows($link)); + +mysqli_free_result($result); + +/* Delete table Language */ +mysqli_query($link, "DROP TABLE Language"); /* close connection */ mysqli_close($link); ?> - ]]> - - +]]> + + + + The above examples would produce the following output: + + + diff --git a/reference/mysqli/functions/mysqli-autocommit.xml b/reference/mysqli/functions/mysqli-autocommit.xml index b961bff9f7..2c7176f994 100644 --- a/reference/mysqli/functions/mysqli-autocommit.xml +++ b/reference/mysqli/functions/mysqli-autocommit.xml @@ -1,5 +1,5 @@ - + mysqli_autocommit @@ -54,17 +54,16 @@ Example - - - Object oriented style - + + Object oriented style + query("SELECT @@autocommit")) { $mysqli->close(); ?> ]]> - - - - Procedural style - + + + + Procedural style + ]]> - - + + + + The above examples would produce the following output: + + + diff --git a/reference/mysqli/functions/mysqli-bind-param.xml b/reference/mysqli/functions/mysqli-bind-param.xml index 27f8141174..09a00c9f52 100644 --- a/reference/mysqli/functions/mysqli-bind-param.xml +++ b/reference/mysqli/functions/mysqli-bind-param.xml @@ -1,5 +1,5 @@ - + mysqli_bind_param @@ -88,7 +88,6 @@ mysqli_bind_result, mysqli_execute, mysqli_fetch, - mysqli_prepare, mysqli_send_long_data, mysqli_stmt_errno, @@ -97,66 +96,95 @@ Example - - Object oriented style - + + Object oriented style + query("DROP TABLE IF EXISTS mytable"); -$mysqli->query("CREATE TABLE mytable (a int, b int, c varchar(30))"); - -/* prepare statement and bind parameters */ -$stmt = $mysqli->prepare("INSERT INTO mytable VALUES (?, ?, ?)"); -$stmt->bind_param("iis", $a, $b, $c); +/* check connection */ +if (mysqli_connect_errno()) { + printf("Connect failed: %s\n", mysqli_connect_error()); + exit(); +} -$a = 1; -$b = 2; -$c = "I like PHP"; +$stmt = $mysqli->prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)"); +$stmt->bind_param('sssd', $code, $language, $official, $percent); + +$code = 'DEU'; +$language = 'Bavarian'; +$official = "F"; +$percent = 11.2; /* execute prepared statement */ $stmt->execute(); +printf("%d Row inserted.\n", $stmt->affected_rows); + /* close statement and connection */ $stmt->close(); + +/* Clean up table CountryLanguage */ +$mysqli->query("DELETE FROM CountryLanguage WHERE Language='Bavarian'"); +printf("%d Row deleted.\n", $mysqli->affected_rows); + +/* close connection */ $mysqli->close(); +?> ]]> - + Procedural style ]]> - - + + The above examples would produce the following output: + + + + + + + mysqli_bind_result @@ -62,28 +62,20 @@ Example - - + Object oriented style - query("DROP TABLE IF EXISTS bind_result"); -$mysqli->query("CREATE TABLE bind_result (id int, extension varchar(20))"); -$mysqli->query("INSERT INTO bind_result VALUES (1, 'ldap')"); -$mysqli->query("INSERT INTO bind_result VALUES (2, 'mysql')"); -$mysqli->query("INSERT INTO bind_result VALUES (3, 'pcre')"); - /* prepare statement */ -if ($stmt = $mysqli->prepare("SELECT id, extension FROM bind_result")) { +if ($stmt = $mysqli->prepare("SELECT Code, Name FROM Country ORDER BY Name LIMIT 5")) { $stmt->execute(); /* bind variables to prepared statement */ @@ -91,8 +83,9 @@ if ($stmt = $mysqli->prepare("SELECT id, extension FROM bind_result")) { /* fetch values */ while ($stmt->fetch()) { - printf("Id: %d Extension: %s\n", $col1, $col2); + printf("%s %s\n", $col1, $col2); } + /* close statement */ $stmt->close(); } @@ -100,26 +93,24 @@ if ($stmt = $mysqli->prepare("SELECT id, extension FROM bind_result")) { $mysqli->close(); ?> - ]]> +]]> - - - Procedural style - - + + Procedural style + + - ]]> - - - - - +]]> + + + + The above examples would produce the following output: + + + + + + + mysqli_change_user @@ -67,19 +67,26 @@ Example - Object oriented style query("CREATE TEMPORARY TABLE mytemp (a int)"); +/* check connection */ +if (mysqli_connect_errno()) { + printf("Connect failed: %s\n", mysqli_connect_error()); + exit(); +} + +/* Set Variable a */ +$mysqli->query("SET @a:=1"); -/* reset all */ -$mysqli->change_user("my_user", "my_password", NULL); +/* reset all and select a new database */ +$mysqli->change_user("my_user", "my_password", "world"); if ($result = $mysqli->query("SELECT DATABASE()")) { $row = $result->fetch_row(); @@ -87,8 +94,12 @@ if ($result = $mysqli->query("SELECT DATABASE()")) { $result->close(); } -if (!($result = $mysqli->query("SHOW TABLES LIKE mytemp"))) { - printf ("temporary table mytemp doesn't exist\n"); +if ($result = $mysqli->query("SELECT @a")) { + $row = $result->fetch_row(); + if ($row[0] === NULL) { + printf("Value of variable a is NULL\n"); + } + $result->close(); } /* close connection */ @@ -102,12 +113,20 @@ $mysqli->close(); ]]> - - + + + + The above examples would produce the following output: + + + diff --git a/reference/mysqli/functions/mysqli-character-set-name.xml b/reference/mysqli/functions/mysqli-character-set-name.xml index f91c90d24b..e0008f7b52 100644 --- a/reference/mysqli/functions/mysqli-character-set-name.xml +++ b/reference/mysqli/functions/mysqli-character-set-name.xml @@ -1,5 +1,5 @@ - + mysqli_character_set_name @@ -40,41 +40,61 @@ Example - - Object oriented style - + + Object oriented style + character_set_name(); - printf ("Current character set is %s\n", $charset); +/* check connection */ +if (mysqli_connect_errno()) { + printf("Connect failed: %s\n", mysqli_connect_error()); + exit(); +} - $mysqli->close(); +/* Print current character set */ +$charset = $mysqli->character_set_name(); +printf ("Current character set is %s\n", $charset); + +$mysqli->close(); ?> ]]> - - - - Procedural style - + + + + Procedural style + ]]> + + The above examples would be produce the following output: + + + + diff --git a/reference/mysqli/functions/mysqli-commit.xml b/reference/mysqli/functions/mysqli-commit.xml index c86f88d1ec..df4e439181 100644 --- a/reference/mysqli/functions/mysqli-commit.xml +++ b/reference/mysqli/functions/mysqli-commit.xml @@ -1,5 +1,5 @@ - + mysqli_commit @@ -48,22 +48,29 @@ query("DROP TABLE IF EXISTS ta_sample"); -$mysqli->query("CREATE TABLE ta_sample (a int) TYPE=InnoDB"); +$mysqli->query("CREATE TABLE Language LIKE CountryLanguage Type=InnoDB"); /* set autocommit to off */ $mysqli->autocommit(FALSE); /* Insert some values */ -$mysqli->query("INSERT INTO ta_sample VALUES (1)"); -$mysqli->query("INSERT INTO ta_sample VALUES (1)"); +$mysqli->query("INSERT INTO Language VALUES ('DEU', 'Bavarian', 'F', 11.2)"); +$mysqli->query("INSERT INTO Language VALUES ('DEU', 'Swabian', 'F', 9.4)"); /* commit transaction */ $mysqli->commit(); +/* drop table */ +$mysqli->query("DROP TABLE Language"); + /* close connection */ $mysqli->close(); ?> @@ -75,18 +82,22 @@ $mysqli->close(); - + mysqli_connect @@ -73,15 +73,14 @@ Example - - - Object oriented style - + + Object oriented style + host_info); /* close connection */ $mysqli->close(); -?> +?> ]]> - - - - Procedural style - + + + + Procedural style + +?> ]]> - - - - - - See also + + - mysqli_close, - mysqli_init, - mysqli_options - mysqli_real_connect. + The above examples would produce the following output: + + + diff --git a/reference/mysqli/functions/mysqli-data-seek.xml b/reference/mysqli/functions/mysqli-data-seek.xml index 99ddd64ed0..ddf5508bb1 100644 --- a/reference/mysqli/functions/mysqli-data-seek.xml +++ b/reference/mysqli/functions/mysqli-data-seek.xml @@ -1,5 +1,5 @@ - + mysqli_data_seek @@ -52,74 +52,85 @@ Example - - - Object oriented style - + + Object oriented style + query( "DROP TABLE IF EXISTS friends"); -$mysqli->query( "CREATE TABLE friends (id int, name varchar(30))"); - -$mysqli->query( "INSERT INTO friends VALUES (1, 'Greant'), - (2, 'Stocker'), (3, 'Rethans'), (4, 'Wendel')"); - -/* Get some rows */ -$query = "SELECT name FROM friends ORDER BY name"; -$result = $mysqli->query( $query) or die(mysqli_error($link)); - -$total = $result->field_count; - -if ($total > 0) { // there is at least one row - /* Get the last employee */ - $result->data_seek($result->num_rows -1); - $friend = $result->fetch_row(); - printf ("Friends name : %s\n", $friend[0]); +/* check connection */ +if (mysqli_connect_errno()) { + printf("Connect failed: %s\n", mysqli_connect_error()); + exit(); } + +$query = "SELECT Name, CountryCode FROM City ORDER BY Name"; +if ($result = $mysqli->query( $query)) { + + /* seek to row no. 400 */ + $result->data_seek(399); + + /* fetch row */ + $row = $result->fetch_row(); + + printf ("City: %s Countrycode: %s\n", $row[0], $row[1]); -$result->close(); + /* free result set*/ + $result->close(); +} + +/* close connection */ $mysqli->close(); ?> ]]> - - - - Object oriented style - + + + + Procedural style + 0) { /* there is at least one row */ - /* Get the last employee */ - mysqli_data_seek($result, mysqli_num_rows($result) -1); - $friend = mysqli_fetch_row($result); - printf ("Friends name : %s\n", $friend[0]); +/* check connection */ +if (!$link) { + printf("Connect failed: %s\n", mysqli_connect_error()); + exit(); } + +$query = "SELECT Name, CountryCode FROM City ORDER BY Name"; + +if ($result = mysqli_query($link, $query)) { + + /* seek to row no. 400 */ + mysqli_data_seek($result, 399); + + /* fetch row */ + $row = mysqli_fetch_row($result); + + printf ("City: %s Countrycode: %s\n", $row[0], $row[1]); -mysqli_free_result($result); + /* free result set*/ + mysqli_free_result($result); +} + +/* close connection */ mysqli_close($link); ?> ]]> - - + + + + The above examples would produce the following output: + + + diff --git a/reference/mysqli/functions/mysqli-errno.xml b/reference/mysqli/functions/mysqli-errno.xml index e106bbc2b4..8d72fb3cc8 100644 --- a/reference/mysqli/functions/mysqli-errno.xml +++ b/reference/mysqli/functions/mysqli-errno.xml @@ -1,5 +1,5 @@ - + mysqli_errno @@ -50,39 +50,61 @@ Example - - - Object oriented style - + + Object oriented style + query("SET a=1")) { printf("Errorcode: %d\n", $mysqli->errno); } + +/* close connection */ $mysqli->close(); ?> ]]> - - - - Procedural style - + + + + Procedural style + ]]> - - + + + + The above examples would produce the following output: - + + + + + mysqli_error @@ -43,39 +43,61 @@ Example - - - Object oriented style - + + Object oriented style + query("SET a=1")) { - printf("Errorcode: %d\n", $mysqli->error); + printf("Errormessage: %s\n", $mysqli->error); } + +/* close connection */ $mysqli->close(); ?> ]]> - - - - Procedural style - + + + + Procedural style + ]]> - - + + + + The above examples would produce the following output: - + + + + + mysqli_execute @@ -53,94 +53,129 @@ Example - - - Object oriented style - + + Object oriented style + query("DROP TABLE IF EXISTS friends"); -$mysqli->query("CREATE TABLE friends (id int, name varchar(20))"); - +/* check connection */ +if (mysqli_connect_errno()) { + printf("Connect failed: %s\n", mysqli_connect_error()); + exit(); +} + +$mysqli->query("CREATE TABLE myCity LIKE City"); + /* Prepare an insert statement */ -$query = "INSERT INTO friends VALUES(?, ?)"; +$query = "INSERT INTO myCity (Name, CountryCode, District) VALUES (?,?,?)"; $stmt = $mysqli->prepare($query); -$stmt->bind_param("is", $val1, $val2); +$stmt->bind_param("sss", $val1, $val2, $val3); -$val1 = 1; -$val2 = 'Miller'; +$val1 = 'Stuttgart'; +$val2 = 'DEU'; +$val3 = 'Baden-Wuerttemberg'; /* Execute the statement */ $stmt->execute(); -$val1 = 2; -$val2 = 'Wagner'; +$val1 = 'Bordeaux'; +$val2 = 'FRA'; +$val3 = 'Aquitaine'; /* Execute the statement */ $stmt->execute(); - + +/* close statement */ $stmt->close(); -/* Return the affected rows for the statement */ -if ($result = $mysqli->query("SELECT id,name FROM friends")) { +/* retrieve all rows from myCity */ +$query = "SELECT Name, CountryCode, District FROM myCity"; +if ($result = $mysqli->query($query)) { while ($row = $result->fetch_row()) { - printf("ID: %s Name: %s\n", $row[0], $row[1]); + printf("%s (%s,%s)\n", $row[0], $row[1], $row[2]); } + /* free result set */ $result->close(); } - + +/* remove table */ +$mysqli->query("DROP TABLE myCity"); + +/* close connection */ $mysqli->close(); ?> ]]> - - - - Procedural style - + + + + Procedural style + ]]> - - + + + + The above examples would produce the following output: + + + diff --git a/reference/mysqli/functions/mysqli-fetch-array.xml b/reference/mysqli/functions/mysqli-fetch-array.xml index a3c3a30566..d465d92ad9 100644 --- a/reference/mysqli/functions/mysqli-fetch-array.xml +++ b/reference/mysqli/functions/mysqli-fetch-array.xml @@ -1,5 +1,5 @@ - + mysqli_fetch_array @@ -68,89 +68,90 @@ Example - - - Object oriented style - + + Object oriented style + query("DROP TABLE IF EXISTS friends"); -$mysqli->query("CREATE TABLE friends (id int, name varchar(20))"); +/* check connection */ +if (mysqli_connect_errno()) { + printf("Connect failed: %s\n", mysqli_connect_error()); + exit(); +} -$mysqli->query("INSERT INTO friends VALUES (1,'Hartmut'), (2, 'Ulf')"); - -$result = $mysqli->query( "SELECT id, name FROM friends"); +$query = "SELECT Name, CountryCode FROM City ORDER by ID LIMIT 3"; +$result = $mysqli->query($query); /* numeric array */ -while ($row = $result->fetch_array(MYSQLI_NUM)) { - printf ("ID: %s Name: %s\n", $row[0], $row[1]); -} -$result->close(); - -$result = $mysqli->query("SELECT id, name FROM friends"); +$row = $result->fetch_array(MYSQLI_NUM); +printf ("%s (%s)\n", $row[0], $row[1]); /* associative array */ -while ($row = $result->fetch_array(MYSQLI_ASSOC)) { - printf ("ID: %s Name: %s\n", $row["id"], $row["name"]); -} -$result->close(); - -$result = $mysqli->query("SELECT id, name FROM friends"); +$row = $result->fetch_array(MYSQLI_ASSOC); +printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]); /* associative and numeric array */ -while ($row = $result->fetch_array(MYSQLI_BOTH)) { - printf ("ID: %s Name: %s\n", $row[0], $row["name"]); -} +$row = $result->fetch_array(MYSQLI_BOTH); +printf ("%s (%s)\n", $row[0], $row["CountryCode"]); + +/* free result set */ $result->close(); +/* close connection */ $mysqli->close(); ?> ]]> - - - - Procedural style - + + + + Procedural style + ]]> - - + + + + The above examples would produce the following output: + + + diff --git a/reference/mysqli/functions/mysqli-fetch-assoc.xml b/reference/mysqli/functions/mysqli-fetch-assoc.xml index 59b9ccb630..bf21dfe450 100644 --- a/reference/mysqli/functions/mysqli-fetch-assoc.xml +++ b/reference/mysqli/functions/mysqli-fetch-assoc.xml @@ -1,5 +1,5 @@ - + mysqli_fetch_assoc @@ -62,49 +62,77 @@ query( "DROP TABLE IF EXISTS friends"); -$mysqli->query( "CREATE TABLE friends (id int, name varchar(20))"); - -$mysqli->query( "INSERT INTO friends VALUES (1,'Hartmut'), (2, 'Ulf')"); - -$result = $mysqli->query( "SELECT id, name FROM friends"); - -while ($row = $result->fetch_assoc()) { - printf ("ID: %s Name: %s\n", $row["id"], $row["name"]); +/* check connection */ +if (mysqli_connect_errno()) { + printf("Connect failed: %s\n", mysqli_connect_error()); + exit(); } -$result->close(); + +$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5"; +if ($result = $mysqli->query($query)) { + + /* fetch associative array */ + while ($row = $result->fetch_assoc()) { + printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]); + } + + /* free result set */ + $result->close(); +} + +/* close connection */ $mysqli->close(); ?> ]]> - + Procedural style ]]> + + The above examples would produce the following output: + + + + diff --git a/reference/mysqli/functions/mysqli-fetch-field-direct.xml b/reference/mysqli/functions/mysqli-fetch-field-direct.xml index 1e708cde77..f97d1f8565 100644 --- a/reference/mysqli/functions/mysqli-fetch-field-direct.xml +++ b/reference/mysqli/functions/mysqli-fetch-field-direct.xml @@ -1,5 +1,5 @@ - + mysqli_fetch_field_direct @@ -104,63 +104,83 @@ query( "DROP TABLE IF EXISTS friends"); -$mysqli->query( "CREATE TABLE friends (id int, name varchar(20))"); +/* check connection */ +if (mysqli_connect_errno()) { + printf("Connect failed: %s\n", mysqli_connect_error()); + exit(); +} + +$query = "SELECT Name, SurfaceArea from Country ORDER BY Name LIMIT 5"; + +if ($result = $mysqli->query($query)) { + + /* Get field information for column 'SurfaceArea' */ + $finfo = $result->fetch_field_direct(1); -$mysqli->query( "INSERT INTO friends VALUES (1,'Hartmut'), (2, 'Ulf')"); - -$result = $mysqli->query( "SELECT id, name FROM friends"); - -/* Get field information for column 'name' */ -$finfo = $result->fetch_field_direct(1); - -printf("Name: %s\n", $finfo->name); -printf("Table: %s\n", $finfo->table); -printf("Default: %s\n", $finfo->def); -printf("max. Len: %d\n", $finfo->max_length); -printf("Flags: %d\n", $finfo->flags); -printf("Type: %d\n", $finfo->type); + printf("Name: %s\n", $finfo->name); + printf("Table: %s\n", $finfo->table); + printf("max. Len: %d\n", $finfo->max_length); + printf("Flags: %d\n", $finfo->flags); + printf("Type: %d\n", $finfo->type); -$result->close(); + $result->close(); +} +/* close connection */ $mysqli->close(); ?> ]]> - + Procedural style name); + printf("Table: %s\n", $finfo->table); + printf("max. Len: %d\n", $finfo->max_length); + printf("Flags: %d\n", $finfo->flags); + printf("Type: %d\n", $finfo->type); -$result = mysqli_query($link, "SELECT id, name FROM friends"); - -/* Get field information for column 'name' */ -$finfo = mysqli_fetch_field_direct($result, 1); - -printf("Name: %s\n", $finfo->name); -printf("Table: %s\n", $finfo->table); -printf("Default: %s\n", $finfo->def); -printf("max. Len: %d\n", $finfo->max_length); -printf("Flags: %d\n", $finfo->flags); -printf("Type: %d\n", $finfo->type); - -mysqli_free_result($result); + mysqli_free_result($result); +} +/* close connection */ mysqli_close($link); ?> ]]> + + The above examples would produce the following output: + + + + diff --git a/reference/mysqli/functions/mysqli-fetch-field.xml b/reference/mysqli/functions/mysqli-fetch-field.xml index 3c304b0407..616defaa50 100644 --- a/reference/mysqli/functions/mysqli-fetch-field.xml +++ b/reference/mysqli/functions/mysqli-fetch-field.xml @@ -1,5 +1,5 @@ - + mysqli_fetch_field @@ -99,70 +99,95 @@ Example - - - Object oriented style - + + Object oriented style + query( "DROP TABLE IF EXISTS friends"); -$mysqli->query( "CREATE TABLE friends (id int, name varchar(20))"); - -$mysqli->query( "INSERT INTO friends VALUES (1,'Hartmut'), (2, 'Ulf')"); - -$result = $mysqli->query( "SELECT id, name FROM friends"); - -/* Get field information for all columns */ -while ($finfo = $result->fetch_field()) { - printf("Name: %s\n", $finfo->name); - printf("Table: %s\n", $finfo->table); - printf("Default: %s\n", $finfo->def); - printf("max. Len: %d\n", $finfo->max_length); - printf("Flags: %d\n", $finfo->flags); - printf("Type: %d\n", $finfo->type); +/* check connection */ +if (mysqli_connect_errno()) { + printf("Connect failed: %s\n", mysqli_connect_error()); + exit(); } - -$result->close(); +$query = "SELECT Name, SurfaceArea from Country ORDER BY Code LIMIT 5"; + +if ($result = $mysqli->query($query)) { + + /* Get field information for all columns */ + while ($finfo = $result->fetch_field()) { + + printf("Name: %s\n", $finfo->name); + printf("Table: %s\n", $finfo->table); + printf("max. Len: %d\n", $finfo->max_length); + printf("Flags: %d\n", $finfo->flags); + printf("Type: %d\n\n", $finfo->type); + } + $result->close(); +} + +/* close connection */ $mysqli->close(); ?> ]]> - - - - Procedural style - + + + + Procedural style + name); - printf("Table: %s\n", $finfo->table); - printf("Default: %s\n", $finfo->def); - printf("max. Len: %d\n", $finfo->max_length); - printf("Flags: %d\n", $finfo->flags); - printf("Type: %d\n", $finfo->type); +/* check connection */ +if (mysqli_connect_errno()) { + printf("Connect failed: %s\n", mysqli_connect_error()); + exit(); } -mysqli_free_result($result); +$query = "SELECT Name, SurfaceArea from Country ORDER BY Code LIMIT 5"; +if ($result = mysqli_query($link, $query)) { + + /* Get field information for all fields */ + while ($finfo = mysqli_fetch_field($result)) { + + printf("Name: %s\n", $finfo->name); + printf("Table: %s\n", $finfo->table); + printf("max. Len: %d\n", $finfo->max_length); + printf("Flags: %d\n", $finfo->flags); + printf("Type: %d\n\n", $finfo->type); + } + mysqli_free_result($result); +} + +/* close connection */ mysqli_close($link); ?> ]]> - - + + + + The above examples would produce the following output: + + + diff --git a/reference/mysqli/functions/mysqli-fetch-fields.xml b/reference/mysqli/functions/mysqli-fetch-fields.xml index c0cee722c3..c339c09c77 100644 --- a/reference/mysqli/functions/mysqli-fetch-fields.xml +++ b/reference/mysqli/functions/mysqli-fetch-fields.xml @@ -1,5 +1,5 @@ - + mysqli_fetch_fields @@ -98,74 +98,97 @@ Example - - - Object oriented style - + + Object oriented style + name); - printf("Table: %s\n", $finfo[$i]->table); - printf("Default: %s\n", $finfo[$i]->def); - printf("max. Len: %d\n", $finfo[$i]->max_length); - printf("Flags: %d\n", $finfo[$i]->flags); - printf("Type: %d\n", $finfo[$i]->type); +/* check connection */ +if (mysqli_connect_errno()) { + printf("Connect failed: %s\n", mysqli_connect_error()); + exit(); } -mysqli_free_result($result); +$query = "SELECT Name, SurfaceArea from Country ORDER BY Code LIMIT 5"; -mysqli_close($link); -?> -]]> - - - - Procedural style - -query($query)) { -$mysqli->query( "DROP TABLE IF EXISTS friends"); -$mysqli->query( "CREATE TABLE friends (id int, name varchar(20))"); - -$mysqli->query( "INSERT INTO friends VALUES (1,'Hartmut'), (2, 'Ulf')"); + /* Get field information for all columns */ + $finfo = $result->fetch_fields(); -$result = $mysqli->query( "SELECT id, name FROM friends"); - -/* Get field information for column 'name' */ -$finfo = $result->fetch_fields(); - -for ($i=0; $i < count($finfo); $i++) { - printf("Name: %s\n", $finfo[$i]->name); - printf("Table: %s\n", $finfo[$i]->table); - printf("Default: %s\n", $finfo[$i]->def); - printf("max. Len: %d\n", $finfo[$i]->max_length); - printf("Flags: %d\n", $finfo[$i]->flags); - printf("Type: %d\n", $finfo[$i]->type); + for ($i=0; $i < count($finfo); $i++) { + printf("Name: %s\n", $finfo[$i]->name); + printf("Table: %s\n", $finfo[$i]->table); + printf("max. Len: %d\n", $finfo[$i]->max_length); + printf("Flags: %d\n", $finfo[$i]->flags); + printf("Type: %d\n\n", $finfo[$i]->type); + } + $result->close(); } - -$result->close(); +/* close connection */ $mysqli->close(); ?> ]]> - - + + + + Procedural style + +name); + printf("Table: %s\n", $finfo[$i]->table); + printf("max. Len: %d\n", $finfo[$i]->max_length); + printf("Flags: %d\n", $finfo[$i]->flags); + printf("Type: %d\n\n", $finfo[$i]->type); + } + mysqli_free_result($result); +} + +/* close connection */ +mysqli_close($link); +?> +]]> + + + + The above examples would produce the following output: + + + diff --git a/reference/mysqli/functions/mysqli-fetch-lengths.xml b/reference/mysqli/functions/mysqli-fetch-lengths.xml index 62b36db1bf..fe3f425e81 100644 --- a/reference/mysqli/functions/mysqli-fetch-lengths.xml +++ b/reference/mysqli/functions/mysqli-fetch-lengths.xml @@ -1,5 +1,5 @@ - + mysqli_fetch_lengths @@ -39,66 +39,93 @@ Example - - - Object oriented style - + + Object oriented style + query( "DROP TABLE IF EXISTS friends"); -$mysqli->query( "CREATE TABLE friends (id int, name varchar(20))"); - -$mysqli->query( "INSERT INTO friends VALUES (1,'Hartmut'), (2, 'Ulf')"); - -$result = $mysqli->query( "SELECT id, name FROM friends"); - -$row = $result->fetch_row(); - -/* Print field lengths */ -for ($i=0; $i < count($result->lengths); $i++) { - printf("Field %d has length %d\n", $i, $result->lengths[$i]); +/* check connection */ +if (mysqli_connect_errno()) { + printf("Connect failed: %s\n", mysqli_connect_error()); + exit(); } -$result->close(); +$query = "SELECT * from Country ORDER BY Code LIMIT 1"; +if ($result = $mysqli->query($query)) { + + $row = $result->fetch_row(); + + /* display column lengths */ + for ($i=0; $i < count($result->lengths); $i++) { + printf("Field %2d has Length %2d\n", $i+1, $result->lengths[$i]); + } + $result->close(); +} + +/* close connection */ $mysqli->close(); ?> ]]> - - - - Procedural style - + + + + Procedural style + ]]> - - + + + + The above examples would produce the following output: + + + diff --git a/reference/mysqli/functions/mysqli-fetch-object.xml b/reference/mysqli/functions/mysqli-fetch-object.xml index 444733d1cd..ac735c361f 100644 --- a/reference/mysqli/functions/mysqli-fetch-object.xml +++ b/reference/mysqli/functions/mysqli-fetch-object.xml @@ -1,5 +1,5 @@ - + mysqli_fetch_object @@ -45,58 +45,82 @@ Example - - - Object oriented style - + + Object oriented style + query($query)) { -/* fetch object */ -$obj = mysqli_fetch_object($result); + /* fetch object array */ + while ($obj = $result->fetch_object()) { + printf ("%s (%s)\n", $obj->Name, $obj->CountryCode); + } -printf("Id: %d Name: %s\n", $obj->id, $obj->name); + /* free result set */ + $result->close(); +} -mysqli_free_result($result); -mysqli_close($link); -?> -]]> - - - - Procedural style - -query( "DROP TABLE IF EXISTS friends"); -$mysqli->query( "CREATE TABLE friends (id int, name varchar(20))"); - -$mysqli->query( "INSERT INTO friends VALUES (1,'Hartmut'), (2, 'Ulf')"); - -$result = $mysqli->query( "SELECT id, name FROM friends"); - -/* fetch object */ -$obj = $result->fetch_object(); - -printf("Id: %d Name: %s\n", $obj->id, $obj->name); - -$result->close(); +/* close connection */ $mysqli->close(); ?> ]]> - - + + + + Procedural style + +Name, $obj->CountryCode); + } + + /* free result set */ + mysqli_free_result($result); +} + +/* close connection */ +mysqli_close($link); +?> +]]> + + + + The above examples would produce the following output: + + + diff --git a/reference/mysqli/functions/mysqli-fetch-row.xml b/reference/mysqli/functions/mysqli-fetch-row.xml index 058f57da81..3b6fcd99ac 100644 --- a/reference/mysqli/functions/mysqli-fetch-row.xml +++ b/reference/mysqli/functions/mysqli-fetch-row.xml @@ -1,5 +1,5 @@ - + mysqli_fetch_row @@ -50,58 +50,82 @@ Example - - - Object oriented style - + + Object oriented style + query( "DROP TABLE IF EXISTS friends"); -$mysqli->query( "CREATE TABLE friends (id int, name varchar(20))"); +/* check connection */ +if (mysqli_connect_errno()) { + printf("Connect failed: %s\n", mysqli_connect_error()); + exit(); +} -$mysqli->query( "INSERT INTO friends VALUES (1,'Hartmut'), (2, 'Ulf')"); +$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5"; -$result = $mysqli->query( "SELECT id, name FROM friends"); +if ($result = $mysqli->query($query)) { -/* fetch object */ -$row = $result->fetch_row(); + /* fetch object array */ + while ($row = $result->fetch_row()) { + printf ("%s (%s)\n", $row[0], $row[1]); + } -printf("Id: %d Name: %s\n", $row[0], $row[1]); + /* free result set */ + $result->close(); +} -$result->close(); +/* close connection */ $mysqli->close(); ?> ]]> - - - - Procedural style - + + + + Procedural style + ]]> - - + + + + The above examples would produce the following output: + + + diff --git a/reference/mysqli/functions/mysqli-fetch.xml b/reference/mysqli/functions/mysqli-fetch.xml index 259d2f4334..0f023e83e3 100644 --- a/reference/mysqli/functions/mysqli-fetch.xml +++ b/reference/mysqli/functions/mysqli-fetch.xml @@ -1,5 +1,5 @@ - + mysqli_fetch @@ -72,70 +72,94 @@ Example - - - Object oriented style - + + Object oriented style + query( "DROP TABLE IF EXISTS friends"); -$mysqli->query( "CREATE TABLE friends (id int, name varchar(20))"); +/* check connection */ +if (mysqli_connect_errno()) { + printf("Connect failed: %s\n", mysqli_connect_error()); + exit(); +} -$mysqli->query( "INSERT INTO friends VALUES (1,'Hartmut'), (2, 'Ulf')"); +$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 150,5"; -$stmt = $mysqli->prepare( "SELECT id, name FROM friends"); +if ($stmt = $mysqli->prepare($query)) { -/* execute statement */ -$stmt->execute(); + /* execute statement */ + $stmt->execute(); -/* bind result variables */ -$stmt->bind_result($column1, $column2); + /* bind result variables */ + $stmt->bind_result($name, $code); -/* fetch result */ -while ($stmt->fetch()) { - printf("Id: %d Name: %s\n", $column1, $column2); + /* fetch values */ + while ($stmt->fetch()) { + printf ("%s (%s)\n", $name, $code); + } + + /* close statement */ + $stmt->close(); } -$stmt->close(); +/* close connection */ $mysqli->close(); ?> ]]> - - - - Procedural style - + + + + Procedural style + ]]> - - + + + + The above examples would produce the following output: + + + diff --git a/reference/mysqli/functions/mysqli-field-seek.xml b/reference/mysqli/functions/mysqli-field-seek.xml index 3835853cd6..3add993c27 100644 --- a/reference/mysqli/functions/mysqli-field-seek.xml +++ b/reference/mysqli/functions/mysqli-field-seek.xml @@ -1,5 +1,5 @@ - + mysqli_field_seek @@ -47,75 +47,91 @@ Example - - - Object oriented style - + + Object oriented style + query( "DROP TABLE IF EXISTS friends"); -$mysqli->query( "CREATE TABLE friends (id int, name varchar(20))"); +/* check connection */ +if (mysqli_connect_errno()) { + printf("Connect failed: %s\n", mysqli_connect_error()); + exit(); +} + +$query = "SELECT Name, SurfaceArea from Country ORDER BY Code LIMIT 5"; + +if ($result = $mysqli->query($query)) { + + /* Get field information for 2nd column */ + $result->field_seek(1); + $finfo = $result->fetch_field(); -$mysqli->query( "INSERT INTO friends VALUES (1,'Hartmut'), (2, 'Ulf')"); - -$result = $mysqli->query( "SELECT id, name FROM friends"); - -/* set field pointer to column name */ -$result->field_seek(1); - -/* fetch field information */ -$finfo = $result->fetch_field(); -printf("Name: %s\n", $finfo->name); -printf("Table: %s\n", $finfo->table); -printf("Default: %s\n", $finfo->def); -printf("max. Len: %d\n", $finfo->max_length); -printf("Flags: %d\n", $finfo->flags); -printf("Type: %d\n", $finfo->type); - -$result->close(); + printf("Name: %s\n", $finfo->name); + printf("Table: %s\n", $finfo->table); + printf("max. Len: %d\n", $finfo->max_length); + printf("Flags: %d\n", $finfo->flags); + printf("Type: %d\n\n", $finfo->type); + + $result->close(); +} +/* close connection */ $mysqli->close(); ?> ]]> - - - - Procedural style - + + + + Procedural style + name); + printf("Table: %s\n", $finfo->table); + printf("max. Len: %d\n", $finfo->max_length); + printf("Flags: %d\n", $finfo->flags); + printf("Type: %d\n\n", $finfo->type); -$result = mysqli_query($link, "SELECT id, name FROM friends"); - -/* set field pointer to column name */ -mysqli_field_seek($result, 1); - -/* fetch field information */ -$finfo = mysqli_fetch_field($result); - -printf("Name: %s\n", $finfo->name); -printf("Table: %s\n", $finfo->table); -printf("Default: %s\n", $finfo->def); -printf("max. Len: %d\n", $finfo->max_length); -printf("Flags: %d\n", $finfo->flags); -printf("Type: %d\n", $finfo->type); - -mysqli_free_result($result); + mysqli_free_result($result); +} +/* close connection */ mysqli_close($link); ?> ]]> - - + + + + The above examples would produce the following output: + + + diff --git a/reference/mysqli/functions/mysqli-field-tell.xml b/reference/mysqli/functions/mysqli-field-tell.xml index d1d4af2f5c..225c8a811e 100644 --- a/reference/mysqli/functions/mysqli-field-tell.xml +++ b/reference/mysqli/functions/mysqli-field-tell.xml @@ -1,5 +1,5 @@ - + mysqli_field_tell @@ -44,75 +44,105 @@ Example - - - Object oriented style - + + Object oriented style + query( "DROP TABLE IF EXISTS friends"); -$mysqli->query( "CREATE TABLE friends (id int, name varchar(20))"); - -$mysqli->query( "INSERT INTO friends VALUES (1,'Hartmut'), (2, 'Ulf')"); - -$result = $mysqli->query( "SELECT id, name FROM friends"); - -while ($finfo = mysqli_fetch_field($result)) { - /* get field pointer position */ - $fieldnr = mysqli_field_tell($result); - printf("Fieldnr: %d\n", $fieldnr); - - printf("Name: %s\n", $finfo->name); - printf("Table: %s\n", $finfo->table); - printf("Default: %s\n", $finfo->def); - printf("max. Len: %d\n", $finfo->max_length); - printf("Flags: %d\n", $finfo->flags); - printf("Type: %d\n", $finfo->type); +/* check connection */ +if (mysqli_connect_errno()) { + printf("Connect failed: %s\n", mysqli_connect_error()); + exit(); } - -$result->close(); +$query = "SELECT Name, SurfaceArea from Country ORDER BY Code LIMIT 5"; + +if ($result = $mysqli->query($query)) { + + /* Get field information for all columns */ + while ($finfo = $result->fetch_field()) { + + /* get fieldpointer offset */ + $currentfield = $result->current_field; + + printf("Column %d:\n", $currentfield); + printf("Name: %s\n", $finfo->name); + printf("Table: %s\n", $finfo->table); + printf("max. Len: %d\n", $finfo->max_length); + printf("Flags: %d\n", $finfo->flags); + printf("Type: %d\n\n", $finfo->type); + } + $result->close(); +} + +/* close connection */ $mysqli->close(); ?> ]]> - - - - Procedural style - + + + + Procedural style + fetch_field()) { - /* get field pointer position */ - $fieldnr = $result->current_field; - printf("Fieldnr: %d\n", $fieldnr); - printf("Name: %s\n", $finfo->name); - printf("Table: %s\n", $finfo->table); - printf("Default: %s\n", $finfo->def); - printf("max. Len: %d\n", $finfo->max_length); - printf("Flags: %d\n", $finfo->flags); - printf("Type: %d\n", $finfo->type); +/* check connection */ +if (mysqli_connect_errno()) { + printf("Connect failed: %s\n", mysqli_connect_error()); + exit(); } -mysqli_free_result($result); +$query = "SELECT Name, SurfaceArea from Country ORDER BY Code LIMIT 5"; +if ($result = mysqli_query($link, $query)) { + + /* Get field information for all fields */ + while ($finfo = mysqli_fetch_field($result)) { + + /* get fieldpointer offset */ + $currentfield = mysqli_field_tell($result); + + printf("Column %d:\n", $currentfield); + printf("Name: %s\n", $finfo->name); + printf("Table: %s\n", $finfo->table); + printf("max. Len: %d\n", $finfo->max_length); + printf("Flags: %d\n", $finfo->flags); + printf("Type: %d\n\n", $finfo->type); + } + mysqli_free_result($result); +} + +/* close connection */ mysqli_close($link); ?> ]]> - - + + + + The above examples would produce the following output: + + + diff --git a/reference/mysqli/functions/mysqli-get-host-info.xml b/reference/mysqli/functions/mysqli-get-host-info.xml index b16a83e1be..c98fba8043 100644 --- a/reference/mysqli/functions/mysqli-get-host-info.xml +++ b/reference/mysqli/functions/mysqli-get-host-info.xml @@ -1,5 +1,5 @@ - + mysqli_get_host_info @@ -38,13 +38,18 @@ Example - - - Object oriented style - + + Object oriented style + host_info); @@ -53,14 +58,20 @@ printf("Host info: %s\n", $mysqli->host_info); $mysqli->close(); ?> ]]> - - - - Procedural style - + + + + Procedural style + ]]> - - + + + + The above examples would produce the following output: + + + diff --git a/reference/mysqli/functions/mysqli-get-proto-info.xml b/reference/mysqli/functions/mysqli-get-proto-info.xml index 87044ccfb9..8f35780951 100644 --- a/reference/mysqli/functions/mysqli-get-proto-info.xml +++ b/reference/mysqli/functions/mysqli-get-proto-info.xml @@ -1,5 +1,5 @@ - + mysqli_get_proto_info @@ -37,40 +37,58 @@ Example - - - Object oriented style - + + Object oriented style + protocol_version); /* close connection */ $mysqli->close(); ?> ]]> - - - - Procedural style - + + + + Procedural style + ]]> - - + + + + The above examples would produce the following output: + + + diff --git a/reference/mysqli/functions/mysqli-get-server-info.xml b/reference/mysqli/functions/mysqli-get-server-info.xml index 8c56c81406..816f424eaf 100644 --- a/reference/mysqli/functions/mysqli-get-server-info.xml +++ b/reference/mysqli/functions/mysqli-get-server-info.xml @@ -1,5 +1,5 @@ - + mysqli_get_server_info @@ -31,11 +31,68 @@ - &reftitle.seealso; + See also + mysqli_get_client_info, + mysqli_get_client_version, mysqli_get_server_version + + Example + + Object oriented style + +server_info); + +/* close connection */ +$mysqli->close(); +?> +]]> + + + + Procedural style + + +]]> + + + + The above examples would produce the following output: + + + + + + mysqli_get_server_version @@ -35,11 +35,68 @@ - &reftitle.seealso; + See also + mysqli_get_client_info, + mysqli_get_client_version, mysqli_get_server_info + + Example + + Object oriented style + +server_version); + +/* close connection */ +$mysqli->close(); +?> +]]> + + + + Procedural style + + +]]> + + + + The above examples would produce the following output: + + + + + + mysqli_info @@ -79,6 +79,89 @@ mysqli_num_rows + + Example + + Object oriented style + +query("CREATE TEMPORARY TABLE t1 LIKE City"); + +/* INSERT INTO .. SELECT */ +$mysqli->query("INSERT INTO t1 SELECT * FROM City ORDER BY ID LIMIT 150"); +printf("%s\n", $mysqli->info); + +/* close connection */ +$mysqli->close(); +?> +]]> + + + + Procedural style + + +]]> + + + + The above examples would produce the following output: + + + + + + + + + mysqli_insert_id @@ -48,7 +48,76 @@ - + + Example + + Object oriented style + +query("CREATE TABLE myCity LIKE City"); + +$query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)"; +$mysqli->query($query); + +printf ("New Record has id %d.\n", $mysqli->insert_id); + +/* drop table */ +$mysqli->query("DROP TABLE myCity"); + +/* close connection */ +$mysqli->close(); +?> +]]> + + + + Procedural style + + +]]> + + + + The above examples would produce the following output: + + + + + + + Improved MySQL Extension mysqli @@ -300,6 +300,9 @@ field_count - returns number of fields in resultset + + lengths - returns an an array of columnlengths + num_rows - returns number of rows in resultset @@ -309,7 +312,14 @@ &reference.mysqli.constants; - + +
+ Examples + + All Examples in the MySQLI documentation use the world database from MySQL AB. + The world database can be found at &url.mysql.example; + +
&reference.mysqli.functions;