I'm trying to execute a simple mySQL query in a php page, and I keep getting this error :
"Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in..."
even though the query returns a result in mysql workbench.
This is my code:
<?php
$con=mysqli_connect("localhost","root","","eshkol");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql1="SET Names utf8";
$sql = mysql_query("SELECT * FROM student WHERE idStudent=2");
$r = mysql_fetch_array($sql);
echo $r["idStudent"];
if (!mysqli_query($con,$sql1))
{
die('Error hebrew: ' . mysqli_error($con));
}
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "success";
mysqli_close($con);
?>
What am I doing wrong here?
You're mixing mysql_* and mysqli_* functions.
$sql = mysql_query($con, "SELECT * FROM student WHERE idStudent=2");
$r = mysql_fetch_array($sql);
should be
$sql = mysqli_query($con, "SELECT * FROM student WHERE idStudent=2");
$r = mysqli_fetch_array($sql);
What's interesting is you're using them just below that code:
if (!mysqli_query($con,$sql1))
{
die('Error hebrew: ' . mysqli_error($con));
}
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
You probably want to combine the two to clean up your code:
$sql = mysql_query($con, "SELECT * FROM student WHERE idStudent=2");
if (!$sql) {
die('Error: ' . mysqli_error($con));
}
$r = mysql_fetch_array($sql);
Related
I have a list of items that is being output via PHP / MySQL. I also have an Edit button and a Delete button in one column. I am trying to figure out how to delete a list item on a specific row by clicking the Delete button. I have tried the following:
$id = $_GET['id'];
if(isset($_POST["deletelist"])) {
$query = "SELECT * FROM lists";
$result = mysqli_query($db, $query);
if(mysqli_num_rows($result) == 1) {
$query = "DELETE FROM lists WHERE id = '$id'";
} else {
echo "Cannot delete";
}
}
This of course does not work. Can anyone help me out with this?
UPDATE
This is the code for the entire page:
https://pastebin.com/raw/qjnZkUU2
UPDATED CODE
$id = $_GET['id'];
if(isset($_POST["deletelist"])) {
$query = "SELECT * FROM lists";
$result = mysqli_query($db, $query);
if(mysqli_num_rows($result) == 1) {
$query = "DELETE FROM lists WHERE id = '$id'";
mysqli_query($db, $query);
} else {
echo "Cannot delete";
}
}
What I am confused about is how does the query know which item to delete? Should I be appending the ID to the URL to pass the ID?
UPDATE
Ok I think I get it....In the delete button, I need to echo the ID of that row so when the query runs from clicking the delete button, it knows which ID to delete correct?
RESOLVED
Alright. I got it figured out!
I have a button that references the ID of the list item:
echo "
I then pass that ID to deletelist.php
if (!isset($_GET['id'])) {
echo 'No ID was given...';
exit;
}
if ($db->connect_error) {
die('Connect Error (' . $con->connect_errno . ') ' . $con->connect_error);
}
$sql = "DELETE FROM lists WHERE id = ?";
if (!$result = $db->prepare($sql)) {
die('Query failed: (' . $db->errno . ') ' . $db->error);
}
Item gets deleted.
you have to execute query for any action in database
mysqli_query($db, $query);
so execute a delete query and then try it again
You have to execute the query. There is no query execution code. Try this
$id = $_GET['id'];
if(isset($_POST["deletelist"])) {
$query = "SELECT * FROM lists";
$result = mysqli_query($db, $query);
if(mysqli_num_rows($result) == 1) {
$query = "DELETE FROM lists WHERE id = '$id'";
mysqli_query($db, $query);
} else {
echo "Cannot delete";
}
}
In order to delete a specific row what I needed to do was echo the ID within a link/button. This would then pass the ID to the needed PHP to delete the row from the database.
Echoing the row ID in the button
echo "
The PHP to delete the action row
<?php
include("db.php");
if (!isset($_GET['id'])) {
echo 'No ID was given...';
exit;
}
if ($db->connect_error) {
die('Connect Error (' . $con->connect_errno . ') ' . $con->connect_error);
}
$sql = "DELETE FROM lists WHERE id = ?";
if (!$result = $db->prepare($sql)) {
die('Query failed: (' . $db->errno . ') ' . $db->error);
}
if (!$result->bind_param('i', $_GET['id'])) {
die('Binding parameters failed: (' . $result->errno . ') ' . $result->error);
}
if (!$result->execute()) {
die('Execute failed: (' . $result->errno . ') ' . $result->error);
}
if ($result->affected_rows > 0) {
echo "The ID was deleted with success.";
} else {
echo "Couldn't delete the ID."; }
$result->close();
$db->close();
header('Location: ../account.php');
?>
This deletes the item row and then returns the user to account.php. Which in this case, the page never really changes.
Try like below
if(isset($_POST["deletelist"]) && isset($_GET['id']) ) {
$id = $_GET['id'];
$query = "SELECT * FROM lists";
$result = mysqli_query($db, $query);
if(mysqli_num_rows($result) == 1) {
$query = "DELETE FROM lists WHERE id = '".mysqli_real_escape_string($db,$id)."'";
mysqli_query($db, $query);
} else {
echo "Cannot delete";
}
}
I do not understand what is going wrong with code. The result is get is "connected successfully success Query failed". I tried few combinations and I get the same result. Please help me in solving this. Thanks in advance.
<?php
$link = mysql_connect('localhost', 'root1', '')
or die('Could not connect: ' . mysql_error());
if ($link) {
echo 'connected successfully';
}
$l = mysql_select_db('vtflix', $link) or die ('Could not select the database');
if ($l) {
echo ' success';
}
/*$varCNAME = 'John';
$varCONTENT = '4';
$varVID = '1';*/
$sql = "INSERT INTO mpaa(C_Name, ContentRating, V_ID) VALUES ('Jon', 4, 3)";
mysql_query($sql, $link) or die("Query failed");
$que = "SELECT * FROM mpaa";
$query = mysql_query($que, $link);
if (!$query) {
echo 'query failed';
}
while ($sqlrow = mysql_fetch_array($query, MYSQL_ASSOC)) {
$row = $sqlrow['C_Name'];
$nrow = $sqlrow['Content Rating'];
$mrow = $sqlrow['V_ID'];
echo "<br>" . $row . " " . $nrow . " " . $mrow . "<br>";
}
mysql_close($link);
?>
1.Don't use mysql_* library (deprecated from php5 onward + removed from php7) .Use mysqli_* OR PDO.
2.An example of mysqli_*(with your code)is given below:-
<?php
error_reporting(E_ALL); // check all type of error
ini_set('display_errors',1); // display those errors
$link = mysqli_connect('localhost', 'root1', '','vtflix');
if($link){
echo 'connected successfully';
$sql= "INSERT INTO mpaa(C_Name,ContentRating,V_ID) VALUES ('Jon', 4, 3)";
if(mysqli_query($link,$sql)){
$query = "SELECT * FROM mpaa";
$res = mysqli_query($link,$query);
if($res){
while($sqlrow=mysqli_fetch_assoc($query))
{
$row= $sqlrow['C_Name'];
$nrow= $sqlrow['Content Rating'];
$mrow= $sqlrow['V_ID'];
echo "<br>".$row." ".$nrow." ".$mrow."<br>";
}
mysqli_close($link);
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Could not connect: ' . mysqli_connect_error());
}
?>
Note:- To check php version (either on localhost or on live server) create a file with name phpInfo.php, and just write one line code in that file:-
<?php
phpinfo();
?>
Now run this file and you will get the current php version.
Like this:- https://eval.in/684551
Here it seems that you are using deprecated API of mysql_* .
1) Check your PHP version
<?php phpinfo();exit;//check version ?>
2) avoid the usage of mysql use mysqli or PDO
3) change your db connection string with this :
new Mysqlidb($hostname, $username, $pwd, $dbname);
example with you code
<?php
$link = mysqli_connect('localhost', 'root1', '','vtflix');
if($link){
echo 'connected successfully';
$sql= "INSERT INTO mpaa(C_Name,ContentRating,V_ID) VALUES ('Jon', 4, 3)";
if(mysqli_query($link,$sql)){
$query = "SELECT * FROM mpaa";
$res = mysqli_query($link,$query);
if($res){
while($sqlrow=mysqli_fetch_assoc($query))
{
$row= $sqlrow['C_Name'];
$nrow= $sqlrow['Content Rating'];
$mrow= $sqlrow['V_ID'];
echo "<br>".$row." ".$nrow." ".$mrow."<br>";
}
mysqli_close($link);
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Could not connect: ' . mysqli_connect_error());
}
?>
I am trying to update my SQL database using a form through php, but i keep getting the error "Error: Query was empty".
<?php
$sql = "";
$con = mysql_connect("*******","*******");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("*******", $con);
mysql_query($sql, $con);
if (isset($_POST['STUDENT_FNAME'], $_POST['STUDENT_SNAME'],
$_POST['STUDENTNO'] ))
{
$sql="UPDATE STUDENT SET STUDENT_FNAME=('$_POST[STUDENT_FNAME]'),
STUDENT_SNAME=('$_POST[STUDENT_SNAME]')
WHERE STUDENTNO=
('$_POST[STUDENTNO]')";
}
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record updated";
mysql_close($con);
?>
It also won't update my table and I don't know what I've done wrong. All help will be much appreciated. I am new to this as you can probably tell!
There is an error in your code first you are calling mysql_query($sql, $con); without any query in your $sql variable your $sql is blank ""
<?php
$sql = "";
$con = mysql_connect("*******","*******");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("*******", $con);
if (isset($_POST['STUDENT_FNAME'], $_POST['STUDENT_SNAME'],
$_POST['STUDENTNO'] ))
{
$sql="UPDATE STUDENT SET STUDENT_FNAME=('$_POST[STUDENT_FNAME]'),
STUDENT_SNAME=('$_POST[STUDENT_SNAME]')
WHERE STUDENTNO=
('$_POST[STUDENTNO]')";
}
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record updated";
mysql_close($con);
?>
Remove mysql_query($sql, $con) query execution after db selection because $sql is empty,
Also put your update sql execution in IF conditions, because if its not true than again $sql will be empty and you will get same error again,...
<?php
$sql = "";
$con = mysql_connect("*******","*******");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("*******", $con);
// mysql_query($sql, $con); // <-- remove this
if (isset($_POST['STUDENT_FNAME'], $_POST['STUDENT_SNAME'],
$_POST['STUDENTNO'] ))
{
$sql="UPDATE STUDENT SET STUDENT_FNAME=('$_POST[STUDENT_FNAME]'),
STUDENT_SNAME=('$_POST[STUDENT_SNAME]')
WHERE STUDENTNO=
('$_POST[STUDENTNO]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record updated";
}
mysql_close($con);
?>
I am working on recieveing an item from another application with the use of $_POST and I am trying to see if that item already exists in the database. If it does, then $count increases by one. If it does not exist in the database, then it will added in with the use of INSERT INTO.
Here is my code:
<?php
date_default_timezone_set('Asia/Manila');
$today = date('m-d-Y');
echo $today;
$con= mysqli_connect("******","******","******")
or die ('Error: ' . mysql_error());
mysqli_select_db($con,"a3656574_opacmin");
$sql= "SELECT keyWord FROM searchedWords";
$result= mysqli_query($con,$sql);
if($result==$_POST[keyWord])
{
$upD="UPDATE searchedWords SET countr = countr + 1";
while (!mysqli_query($con,$upD))
{
die('Error: ' . mysqli_error($con));
}
}
else
{
$insertIn="INSERT INTO `searchedWords`( `keyWord`, `countr`) values ('$_POST[keyWord]',1)";
while (!mysqli_query($con,$insertIn))
{
die('Error: ' . mysqli_error($con));
}
}
?>
I don't know what's wrong. No items are sent to the database at all. Does anyone know how to fix it?
Change your code like this...
$result= mysqli_query($con,"SELECT keyWord FROM searchedWords");
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
if($row['keyWord']==$_POST[keyWord])
{
$upD="UPDATE searchedWords SET countr = countr + 1";
while (!mysqli_query($con,$upD))
{
die('Error: ' . mysqli_error($con));
}
}
$result==$_POST['keyWord'] won't work becase $result is object there so...
After this line
$result= mysqli_query($con,$sql);
You have to fetch the data
$keyword = '';
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
$keyword = $row["keyWord"];
}
I am very frustrated with the function below. It is not updated my flag in the database. I have tried putting the ones and zeros inbetween quotes and not. I have the field set to small integer in database. Do you see what I am doing wrong? The entry is there but the 1 is not updating to 0.
function postValue(){
global $customerID;
$query="SELECT flag FROM welcomecall WHERE customerID= '$customerID'";
echo $query;
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo "The customer flag is ". $row["flag"];
}
if ($row['flag']=='0'){
$query="UPDATE welcomecall SET flag='1' WHERE customerID='$customerID'";
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
}
else if($row['flag']=='1'){
$query="UPDATE welcomecall SET flag='0' WHERE customerID='$customerID'";
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
}
}
When the while expression exits, there'll be no $row left:
while ($row = mysql_fetch_assoc($result)) {
So this will not be true:
if ($row['flag']=='0'){
Consider moving the if statements inside the while loop.
Looks like you're using logic outside of your loop that belongs inside of your loop:
function postValue(){
global $customerID;
$query="SELECT flag FROM welcomecall WHERE customerID= '$customerID'";
echo $query;
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo "The customer flag is ". $row["flag"];
// Moved
if ($row['flag']=='0'){
$query="UPDATE welcomecall SET flag='1' WHERE customerID='$customerID'";
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
}
else if($row['flag']=='1'){
$query="UPDATE welcomecall SET flag='0' WHERE customerID='$customerID'";
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
}
}
}