Jump to content

Recommended Posts

Hello everyone,

 

I am having a problem getting my "prepared statements" working. Here is my setup...

 

index.php -> authenticate.php -> admin.php

 

1)index.php has a login form on it so when someone enters their username the form redirects to another page I call authenticate.php.

 

2)In the authenticate.php file I want to use prepared statements to interact with the MySQL database. I want to compare the username submitted from the form with the username in the database.

 

3)If the login username was legitimate then you are forwarded to admin.php

 

Here is what I have but I don't think it makes any sense and it doesn't work.

 

$link = mysqli_connect($hostname, $dbusername, $password, $database);
$stmt = mysqli_prepare($link, "SELECT * FROM administrators WHERE adminusers=?");
mysqli_stmt_bind_param($stmt, 'ss', $username);
$result = mysqli_stmt_execute($stmt);

$count=mysqli_num_rows($result);

if($count==1){
header("location:admin.php");
} else {
echo "Failure";
}

 

Any help is appreciated.

Link to comment
https://forums.phpfreaks.com/topic/165656-solved-select-and-prepared-statements/
Share on other sites

ok, I have the following code using mysqli but it is not using prepared statements. Anyone know how I can modify it to use prepared statements?

 

 

<?php
$connect = mysqli_connect($hostname, $username, $password, $database);
$sql="SELECT * FROM administrators WHERE adminusers='$adminuser'";
$result=mysqli_query($connect, $sql);

$count=mysqli_num_rows($result);

if($count==1){
header("location:admin.php");
} else {
echo "Failure";
}
?>

It's been a while, and this is untested.... but:

$query = "SELECT * FROM administrators WHERE adminusers=?";
if($dbh = $connect->prepare($query)) {
// bind the parameters:
$dbh->bind_param('s', $adminuser);

// execute
$dbh->execute();

if(!$connect->error) {
	while($row = $dbh->fetch_assoc()) {
		echo $row['column']; // echo a row, or whatever you want to do.
	}
} else {
	echo $connect->error;
}
}

Well, what errors did it give?

 

The way I did it is the way I'm used to, OOP style.  Do the following comments help:

// Create the query string, notice the ? as a placeholder
$query = "SELECT * FROM administrators WHERE adminusers=?";

// Prepare the statement, but only run it if it doesn't fail
if($dbh = $connect->prepare($query)) {

// If we are here, then the prep didn't fail...

// bind the parameters (I'm guessing $adminuser is a string, thus the 's')
$dbh->bind_param('s', $adminuser);

// execute your query
$dbh->execute();

// As long as there wasn't an error,
if(!$connect->error) {

	// We should loop through the results
	while($row = $dbh->fetch_assoc()) {

		// and echo the row, or whatever.
		echo $row['column']; 
	}

} else {

	// but if we got here, there was an error.
	echo $connect->error;
}

} else {

// and if we got here, the prepare call didn't work. so lets see why:
echo $connect->error;
}

ok, I have it working now. For anyone reading this post and needing help here is what I have...

 

$link = mysqli_connect($hostname, $username, $password, $database);
$stmt = mysqli_prepare($link, "SELECT * FROM administrators WHERE adminusers=?");
mysqli_stmt_bind_param($stmt, "s", $adminuser);
$result = mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
$count = mysqli_stmt_num_rows($stmt);

 

KingPhillip: Thanks for commenting your code, but I am new to programming and PHP and have not yet learned OOP style :-)

 

 

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.