Blog Home PHP

mysqli_fetch_fields Nome:

$link = mysqli_connect('localhost', 'my_user', 'my_password', 'world');

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

$query = "SELECT firstname, lastname, email, pass from user ORDER BY ID LIMIT 5";

if ($result = mysqli_query($link, $query)) {

	/* Get field information for all columns */
	$finfo = mysqli_fetch_fields($result);

	foreach ($finfo as $val) {
		printf("Name:     %s\n", $val->name);
		printf("Table:    %s\n", $val->table);
		printf("max. Len: %d\n", $val->max_length);
		printf("Flags:    %d\n", $val->flags);
		printf("Type:     %d\n\n", $val->type);
	}    
	mysqli_free_result($result);
}

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

Teste agora →

mysqli_stmt_bind_param INSERT sem ID

$link = mysqli_connect('localhost', 'my_user', 'my_password', 'world');

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

$stmt = mysqli_prepare($link, "INSERT INTO user (firstname, lastname, email, pass) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($stmt, "ssss", $firstname, $lastname, $email, $pass);

//$id = 4;
$firstname = 'Luis';
$lastname = 'Medeiros';
$email = "joelma.medeiros@bitpixel.com.br";
$pass = "123456";

/* execute prepared statement */
mysqli_stmt_execute($stmt);

$rows = mysqli_stmt_affected_rows($stmt);

/* fetch values */
/*
while (mysqli_stmt_fetch($stmt)) {
	printf ("%s %s\n", $first, $last);
}
*/
printf("Linhas afetadas pelo INSERT %s\n", $rows);

/* close statement and connection */
mysqli_stmt_close($stmt);

/* Clean up table CountryLanguage */
mysqli_query($link, "DELETE FROM user WHERE firstname='Luis' AND lastname='Medeiros'");
printf("%d Linha apagada.\n", mysqli_affected_rows($link));

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

Teste agora →

mysqli_stmt_bind_param INSERT

$link = mysqli_connect('localhost', 'my_user', 'my_password', 'world');

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

$stmt = mysqli_prepare($link, "INSERT INTO user VALUES (?, ?, ?, ?, ?)");
mysqli_stmt_bind_param($stmt, "issss", $id, $firstname, $lastname, $email, $pass);

$id = 4;
$firstname = 'Luis';
$lastname = 'Medeiros';
$email = "joelma.medeiros@bitpixel.com.br";
$pass = "123456";

/* execute prepared statement */
mysqli_stmt_execute($stmt);

//$rows = mysqli_stmt_affected_rows($stmt);

/* fetch values */
/*
while (mysqli_stmt_fetch($stmt)) {
	printf ("%s %s\n", $first, $last);
}
*/
//printf ("Linhas afetadas %s\n", $rows);

/* close statement and connection */
mysqli_stmt_close($stmt);

/* Clean up table CountryLanguage */
mysqli_query($link, "DELETE FROM user WHERE ID=4");
printf("%d Row deleted.\n", mysqli_affected_rows($link));

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

Teste agora →

mysqli_stmt_bind_param SELECT

$link = mysqli_connect('localhost', 'my_user', 'my_password', 'world');

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

$stmt = mysqli_prepare($link, "SELECT firstname, lastname FROM user WHERE firstname=?");
mysqli_stmt_bind_param($stmt, "s", $firstname);

$firstname = "Dime";

/* execute prepared statement */
mysqli_stmt_execute($stmt);

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

$rows = mysqli_stmt_affected_rows($stmt);

/* fetch values */
while (mysqli_stmt_fetch($stmt)) {
	printf ("%s %s\n", $first, $last);
}

/* close statement and connection */
mysqli_stmt_close($stmt);

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

Teste agora →

mysqli_stmt_bind_param

$link = mysqli_connect('localhost', 'my_user', 'my_password', 'world');

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

$stmt = mysqli_prepare($link, "INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($stmt, 'sssd', $code, $language, $official, $percent);

$code = 'DEU';
$language = 'Bavarian';
$official = "F";
$percent = 11.2;

/* execute prepared statement */
mysqli_stmt_execute($stmt);

printf("%d Row inserted.\n", mysqli_stmt_affected_rows($stmt));

/* close statement and connection */
mysqli_stmt_close($stmt);

/* Clean up table CountryLanguage */
mysqli_query($link, "DELETE FROM CountryLanguage WHERE Language='Bavarian'");
printf("%d Row deleted.\n", mysqli_affected_rows($link));

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

Teste agora →

mysqli_stmt_fetch 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();
}

$query = "SELECT firstname, lastname FROM user ORDER by ID DESC LIMIT 0,5";

if ($stmt = mysqli_prepare($link, $query)) {

    /* execute statement */
    mysqli_stmt_execute($stmt);
	
    /* bind result variables */
    mysqli_stmt_bind_result($stmt, $first, $last);
	
    /* fetch values */
    while (mysqli_stmt_fetch($stmt)) {
        printf ("%s (%s)\n", $first, $last);
    }
	
    /* close statement */
    mysqli_stmt_close($stmt);
}

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

Teste agora →

mysqli_prepare 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();
}

$firstname = "Davi";

/* create a prepared statement */
if ($stmt = mysqli_prepare($link, "SELECT firstname, lastname FROM user WHERE firstname=?")) {
	
    /* bind parameters for markers */
    mysqli_stmt_bind_param($stmt, "s", $firstname);
	
    /* execute query */
    mysqli_stmt_execute($stmt);
	
    /* bind result variables */
    mysqli_stmt_bind_result($stmt, $firstname, $lastname);
	
    /* fetch value */
    mysqli_stmt_fetch($stmt);
	
    printf("%s is the firstname of %s\n", $firstname, $lastname);
	
    /* close statement */
    mysqli_stmt_close($stmt);
}

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

				

Teste agora →
Busca
Destaques
Conheça as novidades da nova versão Bootstrap 4.1.3 lançada em abril de 2018