what in the following code is causeing Warning: Illegal string offset 'quantity' in ' because of PHP 5.4 version cant find a fix?
<?php
include "data.php";
function checkStock($id_film){
$query_stock = "SELECT stockquantity FROM table WHERE id = {$id_film}";
$result_stock = mysql_query($query_stock);
$row = mysql_fetch_row($result_stock);
echo $row[0]['quantity']; //this where the error is
}
If you'd like to debug, you could write:
$row = mysql_fetch_row($result_stock);
echo "<pre>";
print_r($row);
echo "</pre>";
That will give you an idea about what the array looks like. From your MYSQL query, it's probably going to be like:
Array(
"quantity" => 5
)
So, this should work:
echo $row['quantity'];
instead of
echo $row[0]['quantity'];
Hope this helps.
Peace! xD
Obligatory chastisement: Don't use the mysql API; it's deprecated and has been removed completely from PHP 7. Now that that's out of the way...
$row = mysql_fetch_row($result_stock); returns a single row of data as a one-dimensional array. Your reference to $row[0]['quantity'] is attempting to dereference a two-dimensional array, so of course you're getting an error because the second dimension doesn't exist.
rjdown is correct; you just want $row['quantity'] which selects the quantity column of the current row.
You have used mysql_fetch_row which returns single row of database as an array which you can access information like $row[0], $row[1], ... In your case it seems that you are accessing one field of a unique record then you have only one field to be returned if found or null if not found. If you want to access information like an associated array use mysql_fetch_assoc then the result will be stored in an associated array like $row['quantity'].
So try to edit this part
$row = mysql_fetch_row($result_stock); echo $row[0]['quantity'];
Like this
$row = mysql_fetch_assoc($result_stock); echo $row['quantity'];
Related
i'm still new towards php but after searching through multiple topics i can't seem to figure this one out.
$query2 = "SELECT COUNT(MAP_CODE2) FROM TCODE_MAPPING WHERE MAP_CODE2 = 'ABC123'";
$result2 = odbc_exec($connect,$query2);
echo $result2;
i have a query above that i'd like to use to get the total number of rows within the query that i've set, however for some reason i keep getting hit by the error
Array to string conversion in /var/www/html/xxx.php on line 85
Would highly appreciate if anyone could help out on what i'm doing wrong. Thank you!
Problem is:-
You are trying to echo a result-set object of array type.
Solution (check the code comments):-
$query2 = "SELECT COUNT(MAP_CODE2) AS MYCOUNT FROM TCODE_MAPPING WHERE MAP_CODE2 = 'ABC123'"; // given a name to the count
$result2 = odbc_exec($connect,$query2); //prepare and execute query
while (odbc_fetch_row($result2)) { //iterate over result-set object
echo odbc_result($result, "MYCOUNT"), "\n"; // echo count
}
What is happening here is you are echoing an array object type that is also a resource type and php echo only string and other primitive type variables.
so you need to use a loop to access the row or to get row count and access row just use a foreach($results as $result)
to get row count visit this sqlsrv-num-rows . on this official php link you can get more example how to fetch data.
Am I using mysqli_fetch_row() the right way ??
I know the customer name and I wanna find out the Env_lines value in that same record. There is only one record with that customer name. I'm not getting an output when i do echo $env_lines. What might be the problem?
<?php require_once("config.php") ?>
<?php require_once("functions.php") ?>
test
<?php
$customer = 'Bickery';
echo ' <br> 1)'.$customer;
$sql_customer = "SELECT * FROM `Customer` WHERE Cust_Name = '$customer' LIMIT 0,1";
echo ' <br> 2)'.$sql_customer,'<br>';
$customer_selection = mysqli_query($conn,$sql_customer);
var_dump($customer_selection);
$customer_row = mysqli_fetch_row($conn, $customer_selection);
$env_lines = $customer_row["Env_Lines"];
echo ' <br> 3)'.$env_lines;
?>
https://gist.github.com/anonymous/520319ac72e7f08419c4
Manual Says
mixed mysqli_fetch_row ( mysqli_result $result )
Does that show anywhere that you need to give it your connection reference? No
Remove your first parameter from here
$customer_row = mysqli_fetch_row($conn, $customer_selection);
^
Then
Fetches one row of data from the result set and returns it as an enumerated array, where each column is stored in an array offset starting from 0 (zero). Each subsequent call to this function will return the next row within the result set, or NULL if there are no more rows
That means this is not the right function to fetch an associative array, mysqli_fetch_assoc is (amongst others).
$customer_row = mysqli_fetch_assoc($customer_selection);
And oh, PHP guys on SO think this is our duty; here it goes
How can I prevent SQL injection in PHP?
Here is my code:
$counter = 0;
$result = mysql_query("select * from funds_backup");
echo mysql_num_rows($result)."</br>"; // <--this prints 48,173
while ($row = mysql_fetch_object($result)) {
$counter++;
$name = $row->Name;
$ticker = $row->Ticker ;
$current[$ticker] = $name;
echo $counter." ".$current[$ticker]."</br>"; //<--this prints to 48,173
}
echo count($current); // <--this prints 45,650
I cannot get mysql_fetch_object to initialize all 48,173 rows to the php array.
I have done this with larger queries before. I have no idea why this does not work. It does not truncate the end of the array ie omit 45,651-48,173. It is random. I have used TRIM() and this does not work. It is consistent in a way too. The same rows are always omitted.
Ideas?
Sounds like you have multiple rows that contain the same data - which means that the value of $ticker ends up the same more than once. In this case you are then overwriting the previous array entry with that key. Try $current[] = $name; or $current[$counter] = $name; to ensure that all array keys are unique.
The mysql_* functions are actually deprecated in PHP5.5, so you might want to consider changing your code to use PDO or MySQLi, instead.
Make sure that the field 'Ticker' is actually unique, otherwise values will keep getting overwritten. Also, are you getting any memory exhausted errors while running this? That might be one cause of this happening.
What does echo $counter; return? If it is 48,173 then the values are getting overwritten.
I'm getting this column in this bd
$result = mysql_query("SELECT short FROM textos");
and I'm trying to echo only one of the results based on the array it returns:
$col = mysql_fetch_assoc($result);
echo "<b>Short:</b>".$col[1]."<br/>";
apparently this $col array can't be accessed this way. How should it be done? Thanks
That has been already stated in comments above, so just a bit of explanation here.
mysql_fetch_assoc retrieves a result row for you presented as an associative array (an array where keys are field names and values are field values). Your query returns only one field (which is short), but still it doesn't make your row a single scalar value - it remains an array, only with a single element.
So you need to refer it as $row['short'], or in your sample $col['short'].
Bear in mind that query might return no results - you can learn that by checking if the returned value is not an array but scalar false instead, e.g.
if ($col === false) {
echo 'Error occured<br/>';
} else {
echo "<b>Short:</b>".$col['short']."<br/>";
}
Putting LIMIT into your query like again comments suggest is a good idea as well because you wouldn't be returning potentially huge amount of data when you only need one row actually. The result would still come as a multi-dimensional array though, so that part won't change.
To access the first element use $col[0]['short'].
If you only want to output one element anyways you can add LIMIT 1 to the MySQL query.
After querying you should check if the result array is set otherwise php will throw an error saying that $col[0]['short'] is not set.
There are three mysql_fetch functions which getting rows:
mysql_fetch_array() Fetch an array with both indexes (numeric and associative)
mysql_fetch_num() Fetch an array with numeric indexes
mysql_fetch_assoc() Fetch an array with associative indexes
In your example you will get an array that is looking like this one for the function mysql_fetch_array():
array(2) {
[0]=>
string(3) "foo"
["short"]=>
string(3) "foo"
}
$statement = 'SELECT short FROM textos';
$result = mysql_result($statement);
if ($result) {
while ($row = mysql_fetch_assoc($result)) {
var_dump($row); // or echo "<b>Short:</b>".$row['short']."<br/>"; or something else you like to do with rows of the above statement
}
}
While working with PHP ,handling connection's with Database (MySQL)
$result = mysql_query('select * from products');
$row = mysql_fetch_array($result);
And yesterday founded out that array $row had Duplicate data within.
First you have Data's selected from database arranged in Indexes like : $row[0] = ID; than you also could find $row['ID'] = ID.
So is this only a feature of the Framework ,which copies data Virtually or are these Data's stored twice in array .
To get only one value set, you need to pass a second parameter to mysql_fetch_array.
either "MYSQL_ASSOC" to get an associative array, or "MYSQL_NUM" to get a normal array.
Example:
$row = mysql_fetch_array($result, 'MYSQL_ASSOC')
This will return:
$row['id']
Documentation
Straight from the manual:
mysql_fetch_array — Fetch a result row as an associative array, a
numeric array, or both
The prototype of the function says that
array mysql_fetch_array ( resource $result [, int $result_type = MYSQL_BOTH ] )
meaning that it returns both arrays by default - that is - duplicating the info
mysql_fetch_array fetches a result row as an associative array, a numeric array, or both. Please see: mysql_fetch_array.
Each row in $result has an index, a name and a value. You can look up a row's value by either index or name.
See the documentation for mysql_fetch_array()
Default setting is to fetch an array with both numeric and associative array. See PHP manual to change that.