Mssql fetch array show error in while loop - php

I'm write a query for take some result in mssql server ,that working but now they some errors ,the error is
Warning: mssql_fetch_array(): 3 is not a valid MS SQL-result resource
in
my connection query is working and there is no problem, my query like this
$qry="select cd_pcp,pcp_nm from tbl_m_pcp order by cd_pcp asc";
$result=mssql_query($qry);
while($row1=mssql_fetch_array($result))
{
echo $row1['cd_pcp'];
}
var_dump the $result show
resource(3) of type (mssql result)
the query is execute in sql-server whyshow this type error any way to solve this issue please help me?

One possible explanation is the return value from mssql_query():
Return Values
Returns a MS SQL result resource on success, TRUE if no rows were
returned, or FALSE on error.
It's important to check the result from mssql_query():
<?php
...
$qry = "select cd_pcp,pcp_nm from tbl_m_pcp order by cd_pcp asc";
$result = mssql_query($qry);
if ($result === true) {
echo "No rows are selected";
} else if ($result === false) {
echo "Error. ".mssql_get_last_message();
exit;
} else {
while ($row1 = mssql_fetch_array($result)) {
echo $row1['cd_pcp'];
}
}
...
?>
Notes:
MSSQL extension was removed in PHP 7.0. One good option is PHP Driver for SQL Server.

I believe that var_dump will always return "mssql result" independent if the return is successful or not. To do the debugging test if the value is returning FALSE
$result = mssql_query($qry,$conn);
if(!$result) {
die('MSSQL error: ' . mssql_get_last_message());
}

Related

PHP Error - Login Script

I am creating a new login script/members directory.
I am creating it from scratch without any frameworks (advice on this matter would also be appreciated).
The situation:
// Look up the username and password in the database
$query = "SELECT admin_id, username FROM admin WHERE adminname = '$admin_user' AND password = SHA1('$admin_pass')";
$data = mysqli_query($dbc, $query);
if (mysqli_num_rows($data) == 1) {
This bit of code keeps giving me an error (the last line in particular):
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /home8/craighoo/public_html/employees/security/dir_admin.php on line 20
When echoing the query I get:
SELECT admin_id, adminname FROM admin WHERE adminname = 'admin' AND password = SHA1('password')
EDIT:
Thanks to everyone. The problem was in my Database column names and the column names I was referencing.
Your query execution is failing. When that happens mysqli_query returns false (boolean value) and when is passed to mysqli_num_rows, you get this error.
Print the query just before executing and check for correctness.
Considering that mysqli_query returns false on failure, and that $data is a boolean, here, I suppose there is an error occuring during the execution of your SQL query.
You could try using mysqli_error to find out what this error is :
$data = mysqli_query($dbc, $query);
if ($data !== false) {
// Do whatever you want with $data
if (mysqli_num_rows($data) == 1) {
//
}
} else {
echo mysqli_error($dbc);
die;
}
Note : echoing the error message and dying, like I did here, is OK while developping your script ; but you should not do that in production.
Instead, in production, you should :
Log the error to a file
Display a nice message to the user
When you have a critical query, it's best to add a die to it like so:
mysqli_query($dbc, $query) or die('Critical error on line #'. __LINE__ .' when attempting to login ...<br>'. mysql_error());
Have you tried running that same query manually thru phpmyadmin or the console? What result do you get?

How can I avoid the error from mySQL on mysql_result?

I get the mysql result using
$back = mysql_result($query, 0);
but when nothing is found, I get this error:
Warning: mysql_result() [<a href='function.mysql-result'>function.mysql-result</a>]: Unable to jump to row 0 on MySQL result index 2 in
I still get the error even if I use something like if (!empty($back))
How can I avoid the error? Any alternative methods are welcome
$sql = 'some query...';
$result = mysql_query($q);
if($result === false){
throw new Exception(mysql_error($connection));
}
if (mysql_num_rows($result) > 0) {
//Process Result
$back = mysql_result($query, 0);
} else {
// empty Result Set
}
Note the check on (! $result) -- if your $result is a boolean, it's certainly false, and it means there was a database error, meaning error is your query
Your error is caused by the fact that the query returned no rows - e.g. nothing matched.
I would do the mysql_query() first and check if anything was returned (if $result) before calling mysql_result.
BTW you should be using mysqli_query, etc. these days the earlier version is insecure...

Meaning of the code below [closed]

I just wanted to ask what this code means? I know that this code connects to the database and its meant to show $error[count($error)] and its in an array. But for some reason the errors wont show up
function query($iquery, &$error)
{
$connection = get_connection($error);
if ($connection) {
$result = mysql_query($iquery, $connection);
if (!$result)
$error[count($error)] = "Query '$iquery' failed" + mysql_error();
}
return $result;
}
especially what this line means:
$error[count($error)] = "Query '$iquery' failed" + mysql_error();
This is the function declaration, it accepts a variable to hold a query called $iquery and
it passes BY REFERENCE, an error containing variable called $error
function query($iquery, &$error)
{
This makes a connection to a database, and stores the connection in a variable called $connection
$connection = get_connection($error);
This checks to see if the connection was successful, and if so, runs the code within
if ($connection) {
This captures the result of the query once it has been run
NOTE This function mysql_query() should no longer be used.
$result = mysql_query($iquery, $connection);
This portion checks to see if the query completed successfully and if not, it adds an error to the array $error which has been passed by reference, so it will update the original array that has been passed to the function query($iquery, &$error) with the MySQL generated error.
if (!$result)
$error[count($error)] = "Query '$iquery' failed" + mysql_error();
}
The function then returns what it received from the query, which would be NULL in the case of a query error.
return $result;
}
because of the &$, the error array is being passed by reference. the line you ask about:
$error[count($error)] = "Query '$iquery' failed" + mysql_error();
... is then updating that array with data than can be accessed from after where query() was called
It just attempts to run a query and returns the query resource. If there's an error, it adds it to an array.

Warning: mysql_num_rows() [duplicate]

Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
I am getting this error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/valerie2/public_html/elinkswap/snorris/filename.php on line 89
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/valerie2/public_html/elinkswap/snorris/filename.php on line 90
Here are the lines of code it is talking about:
dbConnect();
$SQL="SELECT fileID FROM uploads WHERE fileName='".$result."'";
//echo $SQL;
$rs=mysql_query($SQL);
echo mysql_num_rows($rs); // line 89
if(mysql_num_rows($rs)!=0){ // line 90
$extension=strrchr($result,'.');
$result=str_replace($extension,time(),$result);
$result=$result.$extension;
}
return $result;
}
Can someone explain to me why I keep getting this error?
This is because your mysql_query has failed because of syntax or execution errors in the SQL command.
Make sure you can execute your SQL query (the one that you get using echo $SQL; line) in phpMyAdmin or whatever tool you have.
Whenever mysql_query fails, it returns False instead of a mysql resource. So you should always check if (!$rs) or use the or die(...) mechanism.
While the error is being reported here:
echo mysql_num_rows($rs);
The cause of the error is here:
$SQL="SELECT fileID FROM uploads WHERE fileName='".$result."'";
//echo $SQL;
$rs=mysql_query($SQL);
Likely candidates are:
$result contains un-escaped quotes leading to the SQL being malformed - and also your code is susceptible to SQL injection attacks
You didn't call mysql_connect() before this code executes
you didn't select a database (and haven't qualified the table name in the query) before this code executes.
you don't have a table called uploads in the current DB, or this table doesn't contain columns named fileID or fileName
Issue 1 should be evidence if you uncomment the 'echo $SQL;', issues 2, 3 and 4 would be evident if you check mysql_error() after calling mysql_query();
You query has failed, so $rs holds boolean false, which will result in that error. You need to do something like:
dbConnect();
$query = "SELECT fileID
FROM uploads
WHERE fileName='".mysql_real_escape_string($result)."'
LIMIT 1";
// Added LIMIT 1 for speed - you are only checking if the record exists, so you
// can stop as soon as you find one.
if (!$rs = mysql_query($query)) {
// Handle query error here
// e.g.
echo "Oh no! The query failed! Error: ".mysql_error();
// BUT you should NEVER show the result of mysql_error() in a production environment!
}
if (mysql_num_rows($rs)) { // This is sufficient for detecting whether there were any results
// A better way of doing what you did:
$result = explode('.',$result);
array_splice($result,-1,0,time());
$result = implode('.',$result);
// This is better, because str_replace() will replace ALL occurrences of the
// extension - this way only inserts the timestamp before the extension and
// doesn't ever do anything else
}
return $result;
You should see what mysql_error() says, place it after the mysql_query;
mysql_query($SQL) or die(mysql_error());
This should produce an error, which is why your code isn't going any further than the query function thus making your num_rows invalid.

Getting error “Commands out of sync, you can't run the command now” while executing a stored procedure in mysql/PHP

I have written a stored procedure for a table, and after that i am executing queries for same table in php function, But i am getting error :
Error in db : Commands out of sync, you can't run the command now..
I tried mysqli: multi_query also instead of mysqli:query, but i got null output.
Can anyone please help me to sort out this problem.
P.S : stored proceure is working as expected and query are also correct.. but together it returns the error.
You have to consume all selects, and navigate to next result
$sql="";
if (mysqli_multi_query($link, $sql)) {
do {
if ($result = mysqli_store_result($link)) {
while ($row = mysqli_fetch_array($result)) {
array_push($arrows,$row);
}
mysqli_free_result($result);
}
} while (mysqli_next_result($link));
}

Resources