w3resource

PHP mysqli: warning_count() function

mysqli_warning_count() function / mysqli::$warning_count

The mysqli_warning_count() function / mysqli::$warning_count returns the number of warnings from the last query in the connection.

Syntax:

Object oriented style

int $mysqli->warning_count;

Procedural style

int mysqli_warning_count ( mysqli $link )

Parameter:

Name Description Required/Optional
link A link identifier returned by mysqli_connect() or mysqli_init() Required for procedural style only and Optional for Object oriented style

Usage: Procedural style

mysqli_warning_count(connection);

Parameter:

Name Description Required/Optional
connection Specifies the MySQL connection to use. Required

Return value:

Number of warnings or zero if there are no warnings.

Version: PHP 5, PHP 7

Example of object oriented style:

<?php
$mysqli = new mysqli("localhost", "user1", "datasoft123", "hr");

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

$mysqli->query("CREATE TABLE mycity LIKE city");

/* a remarkable city in Wales */
$query = "INSERT INTO mycity (CountryCode, Name) VALUES('GBR',
        'Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch')";

$mysqli->query($query);

if ($mysqli->warning_count) {
    if ($result = $mysqli->query("SHOW WARNINGS")) {
        $row = $result->fetch_row();
        printf("%s (%d): %s\n", $row[0], $row[1], $row[2]);
        $result->close();
    }
}

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

Example of the Procedural style

<?php
$link = mysqli_connect("localhost", "user1", "datasoft123", "hr");

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

mysqli_query($link, "CREATE TABLE myCity LIKE city");

/* a remarkable long city name in Wales */
$query = "INSERT INTO myCity (CountryCode, Name) VALUES('GBR',
        'Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch')";

mysqli_query($link, $query);

if (mysqli_warning_count($link)) {
    if ($result = mysqli_query($link, "SHOW WARNINGS")) {
        $row = mysqli_fetch_row($result);
        printf("%s (%d): %s\n", $row[0], $row[1], $row[2]);
        mysqli_free_result($result);
    }
}

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

Output:

Warning (1264): Data truncated for column 'Name' at row 1

Example:

<?php
$mysqli = new mysqli("localhost", "user1", "datasoft123", "hr");

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

$con->query("CREATE TABLE my_city LIKE city");

/* a remarkable city in Wales */
$query = "INSERT INTO my_city (CountryCode, Name) VALUES('GBR',
        'thequickbrownfoxjumpsoverthelazydog')";

$con->query($query);

if ($con->warning_count) {
    if ($result = $con->query("SHOW WARNINGS")) {
        $row = $result->fetch_row();
        printf("%s (%d): %s\n", $row[0], $row[1], $row[2]);
        $result->close();
    }
}

/* close connection */
$con->close();
?>

See also

PHP Function Reference

Previous: use_result
Next: PHP mail function



Share this Tutorial / Exercise on : Facebook and Twitter

PHP: Tips of the Day

echo and print: echo and print are language constructs, not functions. This means that they don't require parentheses around the argument like a function does (although one can always add parentheses around almost any PHP expression and thus echo("test") won't do any harm either). They output the string representation of a variable, constant, or expression. They can't be used to print arrays or objects.

Assign the string Joel to the variable $name

Example:

$name = "Peter";

Output the value of $name using echo & print

<?php
$name = "Peter";
echo $name;
echo "\n";
print $name;
?>

Output:

Peter
Peter

Parentheses are not required, but can be used

<?php
$name = "Owen";
echo($name); 
echo "\n";
print($name);
?>

Output:

Owen
Owen

Using multiple parameters (only echo)

<?php
$name = "Jhon";
echo $name, "Edward";
?> 

Output:

JhonEdward
<?php
$name = "Jhon";
echo $name, " ", "Edward" ;
?>

Output:

Jhon Edward

print, unlike echo, is an expression (it returns 1), and thus can be used in more places:

<?php
print("Hello") && print(" ") && print("World");
?>

Output:

World11

The above is equivalent to:

<?php
print ("Hello" && (print (" " && print "World")));
?>

Output:

World11