Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
Hi i am trying to use mysqli_affected_rows function but it always return 0 can someone help. Trying to get it done theu MSQLI OOP.
<?php
$servername ="localhost";
$username ="root";
$password ="testdb";
$database ="mydb";
$conn = new mysqli($servername, $username, $password, $database);
if($conn->connect_error)
{
die ("The Database connection is not established : " .connect_error);
}
echo "connection established";
//editing record
$sql_update = "update mytbl SET fname='Nitin Sharma' where sr=2";
echo "The affected Rows :" .mysqli_affected_rows($conn);
$conn->close();
?>
Table Values:
You missed to execute your SQL query.
$conn->query($sql_update);
You need to first execute your query by mysqli_query
$sql_update = "update mytbl SET fname='Nitin Sharma' where sr=2";
$conn->mysqli_query($sql_update);
echo "The affected Rows :" .$conn->affected_rows;
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am facing a problem when i am try to check user and password from database while login it keep
reply an error message :
Notice: Trying to get property 'num_row' of non-object in /Applications/XAMPP/xamppfiles/htdocs/studyact/login.php on line 27
User name or Password is incorrect, please check and try again.
i type user and password correct! enter image description here
php file :
<?php
//html
$user_staff = $_POST["user_staff"];
$pass_staff = $_POST["pass_staff"];
// Create connection
$servername = "localhost";
$username = "root";
$password = "";
$db ="studyact";
$con = new mysqli($servername, $username, $password,$db);
// Check connection
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}else{
$stmt =$con-> prepare("select * from loginstaff where user_staff = ?");
$stmt->bind_param("s",$user_staff);
$stmt->execute();
$stmtresult = $stmt->get_result();
if($stmtresult-> num_row > 0){
$data = $stmtresult-> fetch_assoc();
if($data["pass_staff"] === $pass_staff){
echo "<h2>Login Successfully</h2>";
}
else{
echo "<h2> Sorry User name or Password is incorrect.</h2>";
}
}else{
echo "<h2> User name or Password is incorrect, please check and try again.</h2>";
}
}
?>
You've got a typo on line 27
if($stmtresult->num_rows > 0)
mysqli_stmt::$num_rows — Returns the number of rows fetched from the server
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
The following code is part of the main page that connect database with the main page. mainpage.php
<?php
error_reporting(E_ALL);
include_once dirname(__FILE__).'\dbh.php';
session_start()
?>
this part of the code is the connection between database with the website. dhb.php
<?php
$host = "localhost";
$user = "root";
$password = "root";
$dp="ia";
$port=8889;
$link=mysqli_init();
$conn = mysqli_real_connect($link, $host, $user , $password , $dp, $port);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
$sql = "SELECT * FROM table player";
$result = $conn-> query($sql);
?>
This image is how the website tells me:
mysqli_real_connect() returns a boolean. It does not return a valid mysqli object. You create the mysqli object with mysqli_init().
You are overcomplicating the database connection. You only need three lines to connect properly.
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'user', 'pass', 'db');
$mysqli->set_charset('utf8mb4'); // always set the charset
If you are only starting to learn PHP then you should learn PDO instead of mysqli. PDO is much easier and more suitable for beginners. Start here https://phpdelusions.net/pdo & https://websitebeaver.com/php-pdo-prepared-statements-to-prevent-sql-injection
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Currently, I have one table in my database called 'factory'. In this table, there are two columns, 'Fac_ID' and 'Fac_Name'. Now, I want to create a function to add some new factory to the table 'factory'.
The value of 'Fac_ID' and 'Fac_Name' must be same, which mean when I want to add factory 'F09', the value of Fac_ID and Fac_Name must be same which is 'F09'.
When I used to connect with MYSQL database (PDO), the addition is successful. BUt when i change to MSSQL (PDO),
" Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in C:\xampp\htdocs\ebooking\add_factory.php:24 Stack trace: #0 C:\xampp\htdocs\ebooking\add_factory.php(24): PDOStatement->bindParam(':Fac_ID', 'F11')"
Here is my code for add_factory.php
<?php
require_once "configPDO.php";
if(isset($_POST['Submit'])) {
$Fac_ID = $_POST['Fac_ID'];
// checking empty fields
if(empty($Fac_ID)) {
if(empty($Fac_ID)) {
echo "<font color='red'>Name field is empty.</font><br/>";
}
//link to the previous page
echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
} else {
// if all the fields are filled (not empty)
//insert data to database
$sql = "INSERT INTO factory(Fac_Name, Fac_ID) VALUES(:Fac_Name, :Fac_Name)";
$query = $conn->prepare($sql);
$query->bindParam(':Fac_Name', $Fac_ID,);
$query->bindParam(':Fac_ID', $Fac_ID,);
$query->execute();
//display success message
header("Location:factory.php");
}
}
?>
and here is my configPDO.php
<?php
$servername = 'xxx.xx.xx.xxx';
$username = 'xx';
$password = 'xxxxxx';
$dbname = 'xxxx';
try {
$conn = new PDO("sqlsrv:Server=$servername;Database=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $error) {
$error->getMessage();
}
?>
Can I know what the problem? the input at HTML to add the factory is 'Fac_ID'
in the following query
$sql = "INSERT INTO factory(Fac_Name, Fac_ID) VALUES(:Fac_Name, :Fac_Name)";
you are using :Fac_Name twice instead you should use the following
$sql = "INSERT INTO factory(Fac_Name, Fac_ID) VALUES(:Fac_Name, :Fac_ID)";
and if you need to set the same value for the name and id you should ommit the following line
$query->bindParam(':Fac_ID', $Fac_ID,);
since you are trying to bind data to a parameter that doesnt exist in your query
the following statement is sufficent in your case
$query->bindParam(':Fac_Name', $Fac_ID,);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am trying to print out one column called Language1 from my Table that is called Mull, in a database called v6e.
At the moment i am getting a blank white screen.
<?php
session_start();
$servername = "localhost";
$user = "xxxx";
$password = "xxxx";
$dbname = "v6e";
// Create connection
$conn = mysqli_connect($servername, $user, $password, $dbname);
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
else
{
$query = "SELECT Language1 FROM Mull WHERE username = 'Mull'";
$result = mysqli_query($query);
$row = mysqli_fetch_arrary($result);
echo $row['Language1'];
}
mysqli_close($conn);
?>
You have a typo issue. Change the line:
$row = mysqli_fetch_arrary($result);
With:
$row = mysqli_fetch_array($result);
Plus, you're also not connecting to DB with your query
$result = mysqli_query($conn, $query);
Reference:
http://php.net/manual/en/mysqli.query.php
You should also check for errors:
http://php.net/manual/en/mysqli.error.php
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
When I do a query to my database this is the result:
mysqli_result Object
(
[current_field] => 0
[field_count] => 3
[lengths] =>
[num_rows] => 3
[type] => 0
)
This is the code I'm using:
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password, 'melona');
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM votacion";
$result = $conn->query($sql);
echo "<pre>";
print_r($result);
echo "</pre>";
die();
Of course I don't want to obtain that, I want to get the rows contained in the table, what's wrong with my code?
Well, you need to fetch the results out first, and to do that, you need to use either ->fetch_assoc() or ->fetch_array():
// you need to loop it if you're expecting multiple rows
while($row = $result->fetch_assoc()) {
echo $row['column_name'];
}
Ref:
http://php.net/manual/en/mysqli-result.fetch-array.php
http://php.net/manual/en/mysqli-result.fetch-assoc.php
You have to do
$row = $result->fetch_assoc();