Php Mysqli Undefined Function - php

When executing the
mysqli_bind_result();
I receive an error saying "Call to undefined function".
My code looks as follows:
$mysqli = new mysqli("localhost", "root", "", "*****");
if(isset($_POST['login'])){
if($_POST['username']){
if($_POST['password']){
$username = $_POST['username'];
$passwordtmp = $_POST['password'];
$password = md5(md5($passwordtmp));
//Connect to Database//
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
//Check if user exist in database//
$query = mysqli_query($mysqli,"SELECT * FROM users WHERE username = '$username'");
$numrows = mysqli_num_rows($query);
if($numrows == 1){
if($stmt = mysqli_prepare($mysqli, "SELECT password FROM users WHERE username = '$username'")){
mysqli_execute($stmt);
mysqli_bind_result($stmt,$passw);
while(mysqli_stmt_fetch($stmt)){
$pass = $passw;
}
Additional Information:
I transfered this code from a different computer to my laptop using the same connection to my mysql database.
$mysqli = new mysqli("localhost", "root", "", "*****");
I copied the exact database to my laptop and both my computer and laptop are using wamp.
I am wondering if this could be a connection to the database issue, or just a coding error. But it works perfectly on my original computer without any problems.
Thank you in advance. I will give credit for helpful answers.

You're probably using PHP > 5.4, as the function mysqli_bind_result was removed in PHP 5.4.
Change it to mysqli_stmt_bind_result and that should fix your problem.
mysqli_stmt_bind_result($stmt, $passw);

Related

Is my hosting company messing me up here: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)?

I use the hosting company aPlus.net, and I can't seem to get past a connection error I'm getting when trying to process some php to write database content to a webpage, and I am curious as to if this is because my database appears to not be on the same server as the entire rest of my hosting account, and if there is a way to resolve this in my code? This is my first attempt at writing PHP, and it would be good to know if my code is wrong, or if my hosting company is messing me up. (and either way, how to fix it)
Here's the code that's failing to pull from the database:
{
$con = mysql_connect("localhost","2p5dq9vxmy240651","MY_PASSWORD");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("felineasthma_2p5dq9vxmy240651", $con);
$users_name = $_POST['name'];
$users_comment = $_POST['requests'];
$users_name = mysql_real_escape_string($users_name);
$users_comment = mysql_real_escape_string($users_comment);
$inputid = $_GET['id'];
$query = "
INSERT INTO `felineasthma_2p5dq9vxmy240651`.`submissions` (`id`,
`name`, `requests`, `inputid`) VALUES (NULL,
'$users_name', '$users_comment', '$inputid');";
mysql_query($query);
echo "<h2>Your request has been processed, reload page.</h2>";
mysql_close($con);
}
and here's some screen captures from inside my hosting account (links because I don't have enough posts here yet to upload images, sorry):
felineasthma_2p5dq9vxmy240651 doesn't appear in my hosting account
yet it clearly exists in MySQL Manager, but on a different server
I was even more confused while making the user for this database, as the control panel didn't allow me to make a username, it just randomly assigned one. Help? Advice?
I found a more modern tutorial to learn PHP with, and now everything works, I just need to add security measures now. Here's the working code snippets, if anyone ever comes here asking the same questions.
here's the form action that places the entries into the database:
<?php
$servername = "sql5c40n.carrierzone.com";
$username = "my_username";
$password = "my_password";
$dbname = "my_database";
$users_name = $_POST['name'];
$users_request = $_POST['requests'];
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO submissions (name, requests)
VALUES ('$users_name', '$users_request')";
if (mysqli_query($conn, $sql)) {
header("Location: clv.php");
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
here's the include that puts the database entries onto the page:
<?php
$servername = "sql5c40n.carrierzone.com";
$username = "my_username";
$password = "my_password";
$dbname = "my_database";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, requests, name FROM submissions";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "" . $row["requests"]. " - by " . $row["name"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>

Establishing PHP connection with localhost server?

I am having an issue with connecting my PHP script to the database on my localhost server.
I have posted the code below, it is to enable user registration on the site.
The input boxes appear as they should when I run the code, but nothing updates to the database when I try and complete a sign up.
As a novice with PHP I don't know enough about it to spot any errors I might be making, or what they mean.
Any help on this subject would be appreciated as there is a lot of info about PHP online, but I would rather know what was causing this error in order to prevent it in the future.
Here are the errors appearing in the browser console:
Failed to load resource: the server responded with a status of 404 (Not Found)
ReferenceError: Can't find variable: $
And the UNIX socket code from MAMP (I don't know where this would fit in):
$user = 'root';
$password = 'root';
$db = 'inventory';
$socket = 'localhost:/Applications/MAMP/tmp/mysql/mysql.sock';
$link = mysql_connect(
$socket,
$user,
$password
);
$db_selected = mysql_select_db(
$db,
$link
);
And the PHP code:
//connect to database
$db = mysql_connect("localhost", "root", "", "authentication");
if (isset($_POST['register_btn'])) {
session_start();
$username =mysql_real_escape_string($_post['username']);
$email =mysql_real_escape_string($_post['email']);
$password =mysql_real_escape_string($_post['password']);
$password2 =mysql_real_escape_string($_post['password2']);
if ($password == $password2) {
//create user
$password = md5($password); //hash password before storing for security
$sql = "INSERT INTO users(username, email, password) VALUES('$username', '$email', '$password')";
mysql_query($db, $sql);
$_SESSION['message'] = "Find a Table";
$_SESSION['username'] = $username;
header("location: HomePage.html"); //redirect to homepage
}else{
$_SESSION['message'] = "Your passwords must match to proceed";
}
}
?>
Where to start? So many problems.
First off, you are using the OLD mysql functions which have been removed entirely in recent versions of PHP. Use the mysqli functions instead. The old functions like mysql_connect and mysql_query have been deprecated. You need to look for all occurrences of mysql_ in this code and think about replacing each command with its new counterpart.
You define this code to connect:
$user = 'root';
$password = 'root';
$db = 'inventory';
$socket = 'localhost:/Applications/MAMP/tmp/mysql/mysql.sock';
$link = mysql_connect(
$socket,
$user,
$password
);
$db_selected = mysql_select_db(
$db,
$link
);
and then you don't use the resulting connection -- even check if it worked. You should always check the value returned by mysqli_connect to see if it actually worked or if it returned FALSE. You reconnect again and don't bother checking to see if it worked:
//connect to database
$db = mysql_connect("localhost", "root", "", "authentication");
And in doing so, you redefine $db to something else.
Also, you run a query without checking whether it succeeded or not:
mysql_query($db, $sql);
$_SESSION['message'] = "Find a Table";
$_SESSION['username'] = $username;
header("location: HomePage.html"); //redirect to homepage
You should be checking the result of mysqli_query (not mysql_query as you have in your code) to see what it returned. It should be TRUE if the INSERT query worked.
And after you redirect, you fail to call exit, which means that all the code that follows your redirect attempt may end up actually executing anyway.

How to work with MySQLi?

I am new on this MySQLi connection. I have system codes here and working on WAMP server localhost. I changed the old mysql connection to new mysqli connection and got rid of these "deprecated errors". But But my problem is that I could not login or access query (sql related function). What I am saying is that I got rid of deprecated errors but could not login/access my database.
//This is my OLD connection:
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("towertec_master") or die(mysql_error());
//This is my NEW connection
$link = mysqli_connect ("localhost", "root", "", "towertec_master") or die(mysql_error());
Also here is my OLD fetch connection:
$sql=mysql_query("SELECT organization_name,address,phone,email FROM system_config");
$row=mysql_fetch_assoc($sql);
$organization_name=stripslashes(trim($row['organization_name']));
$address=stripslashes(trim($row['address']));
$phone=stripslashes(trim($row['phone']));
$email=stripslashes(trim($row['email']));
NEW fetch connection:
$query = "SELECT organization_name, address, phone, email FROM system_config" or die("Error in the consult.." . mysqli_error($link));
$result = mysqli_query($link, $query);
As pointed out, your are still using the deprecated error function. Also, for your connection, you need to create the connection object which you do with "new".
$link = new mysqli_connect ("localhost", "root", "", "towertec_master");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
For your query, you are putting the error function in the wrong place.
$query = "SELECT organization_name, address, phone, email FROM system_config";
$result = mysqli_query($link, $query);
if (!$result) {
printf("Error: %s\n", mysqli_error($link));
}

Login Script in php to my MYSQL shows Deprecated warning [duplicate]

This question already has answers here:
Why shouldn't I use mysql_* functions in PHP?
(14 answers)
Closed 8 years ago.
I'm writing a simple program in php that requires users to login. I have a working code but whenever a wrong username or password is entered, I got an exception that says the following:
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\wamp\www\Directory\login.php on line 67
The script I have is working well, but I just need to get rid of this warning message.
Below is my php code.
<?php
$username = $_POST['username'];
$password = $_POST['password'];
// Connect to the database
$con = mysql_connect('localhost', 'root', '');
// Make sure we connected succesfully
if (!$con) {
die('Connection Failed' . mysql_error());
}
// Select the database to use
mysql_select_db("Garden", $con);
$q = mysql_query("select * from register where username='" . $username . "' and password='" . $password . "' ") or die(mysql_error());
$res = mysql_fetch_row($q);
if ($res) {
header('location:home.php');
} else {
echo 'Error. The Username or Password that you entered is invalid.';
}
?>
I dont know if im using something. This is my first time of using php. Please I need your help. Thank you.
Here's the same code using MySQLi:
<?php
$mysqli = new mysqli('localhost', 'user', 'password', 'db_name');
if(mysqli_connect_errno()){
printf("DB Connect failed: %s\n", mysqli_connect_error());
exit();
}
// Add the UTF8 Support
$mysqli->query("SET NAMES 'utf8'");
$mysqli->query("SET CHARACTER SET utf8");
$username = isset($_POST['username']) ? $_POST['username'] : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';
$username = $mysqli->real_escape_string($username);
$password = $mysqli->real_escape_string($password);
$query = "SELECT * FROM `register` WHERE username='" . $username . "' and password='" . $password . "' LIMIT 1";
$result = $mysqli->query($query);
if(!empty($result)){
if($result->num_rows == 1) {
header("Location: home.php");
} else {
echo "Error. The Username or Password that you entered is invalid.";
}
}
mysqli_close($mysqli);
$mysqli = null;
?>
You shouldn't be avoiding the PHP Errors. You should always fix the code accordingly.
You can use PDO or MySQLi.
Rewrite your code (small effort) using mysqli even if it's working properly and check if the message still appears.

SQL cannot execute on live server

I am trying to exectute the following on the live server but it does not execute. On the local host everything is fine. I am able to execute other sql on the live server tho
$mysqli = new mysqli("localhost", "username", "password", "database");
if(isset($_POST['username'])&& isset($_POST['password']) ){
$username =$_POST['username'];
$password = md5($_POST['password']);
if ($mysqli) {
/* Prepared statement, stage 1: prepare */
if (!($stmt = $mysqli->prepare("select acc.accountId, acc.firstname, prof.imagename from accounts acc inner join profilepicture prof on prof.accountId=acc.accountId where acc.emailAddress=(?) and acc.password=(?)"))) {
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
/* Prepared statement, stage 2: bind and execute */
$stmt->bind_param("ss", $username,$password);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
if(!empty($row)){
`enter code here`
`enter code here`// STATEMENTS TO EXECUTE IF DETAILS EXISTS
}
This could be the reason: You are logging into mysql with no default database, no username and no password
$mysqli = new mysqli("localhost", "", "", "");
should be something like this:
$mysqli = new mysqli("localhost", "user", "password", "database");
Read this http://php.net/manual/en/mysqli.quickstart.connections.php
In the live server or cpanel.. go to mysql section create the database and user and password... and give that user the permission to edit update and delete.. then in the
$mysqli = new mysqli("localhost", "", "", "");
add all the three the username password and database name..then.. it will work fine..
Hope it works

Categories