how to solve this php mysql warning [duplicate] - php

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 6 years ago.
My code is as following:
$br_code=$_SESSION["br_code"];
echo $sqlstr="select DISTINCT branch_assets.s_id,s_name
from branch_assets,assets
where assets.s_id=branch_assets.s_id and
br_code='$br_code'";
echo $result=mysql_query($sqlstr);
while($row1=mysql_fetch_array($result))
{
}
But on the line of while loop it show me warning as
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\System_management\send_in_maintanance1.php
where is my mistake please help to find this.

$sqlstr="select DISTINCT branch_assets.s_id,s_name from branch_assets,assets where assets.s_id = branch_assets.s_id and br_code='".$br_code."'";
It's best to switch to either mysqli or PDO with a prepared statement instead of mysql_, since it is deprecated and deleted from PHP 7.0.
Reference:
https://en.wikipedia.org/wiki/Prepared_statement

Related

what is the error [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 6 years ago.
what is error in this code output is
Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\xamp\htdocs\LMS\LMS\all_books.php on line 60
<?php
$sql1=mysql_query("SELECT COUNT(*) FROM book_list where book_id=$id AND status=0 AND item_type=0 AND condition='new'");
$count1 = mysql_result($sql1, 0, 0);
echo $count1;
?>
The function mysql_result() expects a resource (a mysql result-set) as its first parameter but has instead been supplied with a boolean (true/false) value.
This is likely because your SQL query is malformed (condition is a reserved keyword); You ought to enclose your table and column names in backticks, for example:
SELECT COUNT(*) FROM `book_list` WHERE `book_id`=$id AND `status`=0 AND `item_type`=0 AND `condition`='new'

mysql_query returns the notice "Trying to get property of non-object" [duplicate]

This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
(31 answers)
Closed 5 months ago.
I am trying to get an old PHP script to work but I keep getting this notice:
Notice: Trying to get property of non-object in ...
The notice is based on the last line of this code:
$result_id_check = mysql_query("select ses_userid from fe_sessions where ses_id = '".$_COOKIE['fe_typo_user']."';");
$row_check = mysql_fetch_object($result_id_check);
if ($row_check->ses_userid) {
I also tried using mysqli_query and mysqli_fetch_object but that won't take any changes.
Any ideas how I can resolve this?
This error normally means that the query failed.
You should be checking for errors as you go like this, also the string concatenation can be made simpler if you use either the {$_COOKIE['fe_typo_user']} form of variable expansion inside a double quoted string, or alternatively this will also work $_COOKIE[fe_typo_user]
$sql = "select ses_userid
from fe_sessions
where ses_id = '{$_COOKIE['fe_typo_user']}'"
$result_id_check = mysql_query($sql);
if ( $result_id_check === false ) {
echo mysql_error();
echo 'Bad SQL : ' . $sql;
exit;
}
$row_check = mysql_fetch_object($result_id_check);
This way when you make small mistakes with your SQL, you get told about them directly and you dont have to wonder what nay have gone wrong.
Please dont use the mysql_ database extension, it
is deprecated (gone for ever in PHP7)
Specially if you are just learning PHP, spend your energies learning the PDO database extensions.
Start here
You should also be using parameterized queries ( available in the mysqli_ and PDO database extensions, but not the old deprecated mysql_ extension) to avoid SQL Injection Attack Specially if you are using data got from $_POST or $_GET or $_COOKIE
If you are considering moving to mysqli_ or PDO you should read also read this Can I mix MySQL APIs in PHP?
Before using $row_check->ses_userid just check whether $row_check is true or false.
if ($row_check) {
if ($row_check->ses_userid) {
}
}

Mistake deal with mysql query: "Warning: mysql_fetch_array() expects parameter 1 to be resource" [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
When I restart my page I received following mistake:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in
on this string:
while($row=mysql_fetch_array($emp_query)){ $id=$row['history_id'];"
What I did wrong in this code? Without following words in my MySQL query:
"WHERE user Like"
my code is work.
My code
<?php
$emp_query=mysql_query("SELECT * FROM `history` WHERE user LIKE $login_session");
while($row=mysql_fetch_array($emp_query)){ $id=$row['history_id'];
?>
<td>........</td>
<?php }?>
There is a problem with your SELECT query that's why it through error on mysql_fetch_array , try this way & use % if you want to match like '$login_session%' or '%$login_session' or '%$login_session%'
$emp_query=mysql_query("SELECT * FROM `history` WHERE user LIKE '".$login_session."'");
NB: move on to mysqli or PDO as adeneo advised

Deprecated: mysql_query() [duplicate]

This question already has an answer here:
The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead [duplicate]
(1 answer)
Closed 8 years ago.
i just update my server. it showing an error today
Deprecated: mysql_query(): The mysql extension is deprecated and will be removed in the >future: use mysqli or PDO instead in C:\wamp\www\work\db\dbfields - Copy.php on line 33
my dbfields - Copy.php page is
mysql_query("insert into user(name,address) values('$name','$address')");
i create 2 columns (name&address), need to insert the value of var($name& $address).
mysqli_query is now used instead of mysql_query. You can also use PDO::query or MySQLi::query. You can see the documentation here
Read : The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
It basically means mysql_query() can no longer be used. You will have to switch to using PDO.
For PDO, read: http://php.net/manual/en/book.pdo.php

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource 22 [duplicate]

This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
(31 answers)
Closed 8 years ago.
I don't understand why I am getting this error:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL
result resource
My code is:
$alreadyMember = mysql_fetch_assoc(mysql_query("SELECT id FROM members WHERE emailAddress='{$_SESSION['register']['email']}'"));
I am also getting the error here:
$alreadyRegistered = mysql_fetch_assoc(mysql_query("SELECT confirm_code FROM memberstemp WHERE emailAddress='{$_SESSION['register']['email']}'"));
If mysql_query() fails, it returns FALSE instead of a MySQL result resource.
So basically mysql_query() is failing and you are passing FALSE to mysql_fetch_assoc() instead of the resource you are supposed to.
You have to run mysql_query() separately and check if it returns FALSE before proceeding, in which case you print mysql_error() to learn what went wrong.
It is best to separate query and the fetching of result for easier debugging and prevent unnecessary error, like:
$query = "SELECT id FROM members WHERE emailAddress='{$_SESSION['register']['email']}'";
$result = mysql_query($query) or die("Error : ".mysql_error.__LINE__);
if ($result) // Test if result has no error
{
$alreadyMember = mysql_fetch_assoc($result);
}
However, it is highly recommended to use prepared statements using PDO to prevent sql injection instead of simple mysql() extensions.

Categories