Error - Bad Row Offset - php

When getting all values from db table, I get this error on the last result value.
Warning: mssql_result() function.mssql-result:
Bad row offset (32) in C:\ms4w\Apache\htdocs\mapserver\data\.... on line 38
Line 38:
$str = "MyMap_".mb_convert_encoding(mssql_result($result_set, $row, 0),"UTF-8","SJIS")."_".mb_convert_encoding(mssql_result($result_set, $row, 1),"UTF-8","SJIS");
AND my settings($sql, $con):
$con = mssql_connect ("myServer", "myUsername", "myPassword");
$sql = "SELECT * FROM m_group_layer WHERE group_id=\"".$_SESSION["group_id"]."\" ORDER BY display_order";
$rs_group_layer = mssql_query ($sql, $con);
$group_layer_row = mssql_num_rows($rs_group_layer);
/* EDIT:
Function:
function getLayer($result_set, $row){
$str = "MyMap_".mb_convert_encoding(mssql_result($result_set, $row, 0),"UTF-8","SJIS")."_".mb_convert_encoding(mssql_result($result_set, $row, 1),"UTF-8","SJIS");
return "var ".$str.";\n\n";
}
Loop:
for($i=0; $i<=$group_layer_row; $i++){
echo getLayer($rs_group_layer, $i);
}
*/
Honestly I am not exactly sure what this error this. So if I could get some suggestions first about what typically causes this error, if there is other code that may be responsible I will post as needed.
Thanks for your help.

Check your main loop, the index variable must start with 0, so if you has 3 lines returning, the last index must be 2.
EDIT:
Change to for($i=0; $i<$group_layer_row; $i++){, without =, it's a common mistake

Related

mysqli_query parameter given boolean and Can't use function return value in write context [duplicate]

This question already has answers here:
"Can't use function return value in write context" error in PHP
(3 answers)
Closed 6 years ago.
Guys i read all the questions asked in site but there is no answer for me, please forgive me to ask new question.
I need to fetch mysqli db. I did it but it has 18k rows and i'm using each row in a bot. It takes so long time and i took error for execution time. I need to do this in short time.
Here is my solution for this problem but it didnt work any.
<?php
include "baglan2.php";
$q = mysqli_query($baglanti, "SELECT bolumlink FROM bolumler WHERE id=?");
$row = mysqli_fetch_assoc($q);
$r = $row['bolumlink'];
for($a=1; $a<=count($r); $a++){
for($b=1; $b<=count($r); $b+300) {
$statement = $baglanti->prepare($query);
$statement->bind_param("i", $id);
$statement->execute();
$statement->bind_result($bolumlink);
$statement->fetch();
$link_array($a) = array();
if ($query) {
while ($row = mysqli_fetch_assoc($query)) {
$link_array($a)[] = $row['bolumlink'];
}
}
}
$statement->close();
}
?>
this codes receipt is:
Fatal error: Can't use function return value in write context in C:\xampp\htdocs\xampp\dizipub\playerbaglan.php on line 15
i tried to delete ($a) on link_array s then it gives error:
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\xampp\dizipub\playerbaglan.php on line 5
Notice: Undefined variable: link_array1 in C:\xampp\htdocs\xampp\dizipub\playerlinkler.php on line 8
Warning: file_get_contents(): Filename cannot be empty in C:\xampp\htdocs\xampp\dizipub\playerlinkler.php on line 8
<html>
<p><font face=Arial color=Black size=2>
<?php
include "baglan.php";
include "playerbaglan.php";
$parcala = '#<iframe width="100%" height="300" src="(.*?)" frameborder="0" allowfullscreen></iframe></span></div><div id="2">#si';
$bot2ara = file_get_contents($link_array1);
preg_match_all($parcala,$bot2ara,$playerlar);
for($a=0; $a<count($playerlar[0]); $a++) {
foreach (array($playerlar[0][$a]) as $playerlar2);
$playerlar3 = explode('"', $playerlar2,-5);
echo "<pre/>"; print_r($playerlar3[5]);
}
}
?>
</font></p>
</html>
i'm trying to explode link_array cause file_get_contents cant work with 18k rows. I tried to share this 18k rows to 60 variables by using $a and for. but every method i tried is not working. I tried making 60 php for 60 variable also but it didnt work too and it is kind of a stupidity :)
Please help me guys :)
It appears to be a typo.
Your $link_array uses parenthesis instead of square brackets.
I'd suggest you to initialize it as an empty array before the for loops.
include "baglan2.php";
$q = mysqli_query($baglanti, "SELECT bolumlink FROM bolumler WHERE id=?");
$row = mysqli_fetch_assoc($q);
$r = $row['bolumlink'];
$link_array = [];
for ($a=1; $a<=count($r); $a++) {
for ($b=1; $b<=count($r); $b+300) {
$statement = $baglanti->prepare($query);
$statement->bind_param("i", $id);
$statement->execute();
$statement->bind_result($bolumlink);
$statement->fetch();
$link_array[$a] = array();
if ($query) {
while ($row = mysqli_fetch_assoc($query)) {
$link_array[$a][] = $row['bolumlink'];
}
}
}
$statement->close();
}
Beware, also, that $link_array1 does not exist. Maybe you meant $link_array[1]?

Strange Illegal string offset in foreach from mysqli_fetch_array() and mysqli_fetch_assoc()

I am just testing out a data set I am looking to return from the DB.
I am running this in command line mode. When I var_dump() the data, I can see data being returned, but when I try to traverse the array, which has duplicate data in it, I get the warning message below and can not print the array item.
I am sure to some that is obvious to some, but I do not know why this is happening. I am sure I am doing something wrong here...but what?
Consider:
$link = mysqli_connect("localhost","username","password","mydatabase") or die("Error " . mysqli_error($link));
$query = "SELECT * FROM citizen_application";
$execute = $query or die("Error in the consult.." . mysqli_error($link));
//execute the query.
$result = $link->query($execute);
$data = mysqli_fetch_array($result); // also tried mysqli_fetch_assoc() and the issue persists
//display information:
//var_dump($data); //This show duplicates in the array returned???
foreach($data as $data_unit){
print_r($data_unit["dob"]."\r");
}
The warning in the logs:
Illegal string offset 'dob'
There seems to be no way to do this with a foreach() when running the script in command line mode. But I found a solution below that gives me what I was looking for:
while($data = mysqli_fetch_assoc($result)) {
print_r($data["dob"]."\n");
}
I noticed all the examples in the documentation where doing this way. I thought it was just a preference. It does not seem so. I hope this helps someone else, because this was quite irritating. You used to be able to do this easily with the previous mysql functions.
mysqli_fetch_array returns an array, you're traversing the array with the foreach, $data_unit will most likely be a single element and not an array... try just
foreach($data as $data_unit){
echo $data_unit."\r";
}
or use mysqli_fetch_assoc() and try
foreach($data as $fieldname => $data_unit){
echo "$fieldname = $data_unit\r";
}

Selecting a single field from a MySQL database for comparison of a variable

I need to get 'groupLeader' field from the table 'groups' in the database and store it in a variable and then compare to the current logged in username but I get the error:
Warning: mysql_numrows() expects parameter 1 to be resource, object given in /home/content/00/7923300/html/uber/tasks.php on line 31
The code I currently have is this:
$sq = "SELECT * FROM groups WHERE groupID='".$groupID."'";
$result=$db->query($sq);
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
$gLeader= mysql_result($result, $i, 'groupLeader');
$i++;
}
if($_COOKIE['$username'] == $gLeader) {
echo "User is leader.";
}
Forgive me if this seems like a rather simple request. I'm new to php and working with databases.
EDIT: Forgot to mention line 31 is the line that contains
$num=mysql_numrows($result);
You might need to use with PHP's mysql_query or your framework row number method.
$result = mysql_query($sq);
Or perhaps
$db->number_of_rows($sq);
The problem here is, the query is not executed properly for an unknown reason. That is why it is returning False to $result, which is really not a resource.
use:
$sq = "SELECT * FROM groups WHERE groupID='".$groupID."'";
$result = mysqli_query($connection, $sq);
or
$result = mysql_query($sq);
Most of that code is redundant and pointless. if all you want is a single field from a single row, then don't do SELECT *, and don't use a loop.
$sql = "SELECT groupLeader FROM ..."
$result = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_assoc($reuslt);
if ($row['groupLeader'] == $_COOKIE['username']) {
...
}
}
However, note that the mysql_*() function complex is officially deprecated in PHP and should not be used anymore. Consider switching to mysqli instead, which is generally drop-in compatible, or better yet, use PDO instead.

how to fix that my method returns nothing for the second index?

I already asked this but I dont know how to fix it please help, my $obj->obtainID($a); i returns nothing for the second index.
Description:
I got an $arrayDirectoy variable which contains just 2 values, then I tried to pass that variable to a query in a for loop to retrieve the ids of each one, but It just echo me one id, and there are two ids, I dont know how to fix that, It just print me 1 id.
Example $arrayDirectory[] = {user1, user2};
it must echo 1 2 but just print me 1
for($i=0;$i<sizeof($arrayDirectory);$i++){
$res[$i] = $obj->obtainID($arrayDirectory[$i]);
echo $res[$i];
}
this is my obtainID method
public function obtainID($user){
$conexion = $this->objConexion->configuracion();
$query = "CALL sp_xxx('$user')";
$stmt = $conexion->prepare($query);
$stmt->execute();
$resultado = $stmt->fetchColumn();
return $res;
}
There are a couple issues. First, you have a syntax error -- you forgot a closing parenthesis to sizeof():
for($i=0;$i<sizeof($arrayDirectory;$i++){
Also, if you're not changing the number of elements, there's no need to call sizeof() more than once:
$count = count($arrayDirectory); // or sizeof(), doesn't matter
for($i=0; $i<$count; $i++)
Lastly, you should be calling count() or sizeof() on $arrayDirectory[0], not $arrayDirectory. This is because the array only has one element, which happens to be an object that contains two fields. Alternatively, it probably makes sense for $arrayDirectory to just be an object, not an array.
So, your final code should look like:
$arrayDirectory = {'user1', 'user2'};
$count = count($arrayDirectory); // or sizeof(), doesn't matter
for($i=0; $i<$count; $i++)
$res[$i] = $obj->obtainID($arrayDirectory[$i]);
echo $res[$i];
}
You aren't checking for errors. Try checking for
if (!$stmt->execute()) { print_r($stmt->errorInfo()); }
also, I wouldn't use fetchColumn() I'd use either fetch() or fetchAll() if you have more than one result.

Using PHP/MySQL to Calculate Percentages

I'm trying to calculate the number of students who've completed an their homework for an online gradebook, and I can't figure the code out...
// SELECT THE TOTAL
$gettotal = "SELECT enroll FROM student_course WHERE classID = $classID";
$showtotal = #mysqli_query ($dbc, $gettotal); // Run the query.
//THIS IS LINE 108
$numtotal = mysql_num_rows($showtotal);
echo '$numtotal';
// SELECT THOSE PASSED
$getpassed = "SELECT entry FROM grades WHERE classID = $classID AND test_result >= 80";
$showpassed = #mysqli_query ($dbc, $getpassed); // Run the query.
$numpassed = mysql_num_rows($showpassed);
//THIS IS LINE 117
echo '$numpassed';
// PERFORM THE PERCENTAGE FUNCTION
function percent($numpassed, $numtotal) {
$count1 = $numpassed / $numtotal;
$count2 = $count1 * 100;
$count = number_format($count2, 0);
echo $count;
}
//THIS IS LINE 124
percent($numpassed, $numtotal);
I get the following error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource on line 108
$numtotal
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource on line 117
$numpassed
Warning: Division by zero in on line 124
0
Okay - while I thank everyone for their concern removing the # ... no one noticed that the problem was using mysqli_query and then mysql_num_rows. It needed to be changed to mysqli_num_rows.
Thanks though :)
Chances are the query has failed.
Also mysql_query will return FALSE on error so you can check for that.
$result = mysql_query('SELECT * WHERE 1=1');
if (!$result) {
die('Invalid query: ' . mysql_error());
}
Btw: You might want to remove the # operator before mysql_query so you see if something goes wrong there.
Remove error suppression (#) in your code and use inspection methods print_r($var) and var_dump($var) to verify the returned database call values.

Categories