Original SQL query is this;
SELECT id,post_title,post_date FROM wp_posts where id='1'
When I retrieve the record, I am finding it but when it comes to returning the results, I am puzzled. Here is the where I got stuck.
while ($row = mysql_fetch_assoc($RS)) :
print_r ($row);
list($id,$post_title,$post_date) = $row;
endwhile;
print_r ($row) outputs this;
Array ( [ID] => 1 [post_title] => Hello world! [post_date] => 2012-03-27 03:28:27 )
And when I run the list function in there ( for debug purposes obviously ), I get this;
Notice: Undefined offset: 2 in F:\inetpub\wwwroot\whatever\sql.php on line 147
Notice: Undefined offset: 1 in F:\inetpub\wwwroot\whatever\sql.php on line 147
Notice: Undefined offset: 0 in F:\inetpub\wwwroot\whatever\sql.php on line 147
What's causing this?
Replace:
mysql_fetch_assoc($RS)
with:
mysql_fetch_array($RS, MYSQL_NUM)
then it should work, because the list function trys to access the array using numeric keys.
I guess the answer lies somewhere within this;
list() only works on numerical arrays and assumes the numerical indices start at 0.
:(
You might be able to use extract() here instead, as well; (documentation here.)
You used mysql_fetch_assoc, so the resulting array per row has data under a key by column name, whereas "list" tries to match variables to values using numerical array indexes. You can use mysql_fetch_array instead.
$categ = val1 | val2
list($one,$two,$three)=#split('[|]',$categ);
If you try to list the value which is not available it will return the error Undefined Offset.
Here the error will be Undefined Offset 2.
Because while spliting $categ, it will have only two values, if you try to access third value then it will return error.
Related
Why cant the output three values store in the array with indexes as number ?
its giving error as follows:
Notice: Undefined offset: 1 in
F:\xampp\htdocs\dashboard\working_files\admin_home.php on line 63
Notice: Undefined offset: 2 in
F:\xampp\htdocs\dashboard\working_files\admin_home.php on line 64
$m_name_detail1_query="SELECT m_name FROM movies";
$run_m_name_detail1_query=mysqli_query($connection,$m_name_detail1_query);
$fetch_m_name_details=mysqli_fetch_array($run_m_name_detail1_query,MYSQLI_NUM);
?> <?php echo $fetch_m_name_details[0] ?>//working
<?php echo $fetch_m_name_details[1] ?>//not working
<?php echo $fetch_m_name_details[2] ?>//not working
mysqli_fetch_array() returns (in your case) a numerically indexed array of a row of data from your SQL statement, as your SQL is
$m_name_detail1_query="SELECT m_name FROM movies";
each row will only contain 1 value - which is the $fetch_m_name_details[0] you find works.
If you want the value from a sequence of rows, use mysqli_fetch_all()
$fetch_m_name_details=mysqli_fetch_all($run_m_name_detail1_query,MYSQLI_NUM);
you will then find $fetch_m_name_details will contain an array of rows, so you can use
echo $fetch_m_name_details[0][0];
echo $fetch_m_name_details[1][0];
echo $fetch_m_name_details[2][0];
if there are at least 3 rows
I try to get some information out of my database to my webpage. Everything seems to be fine but there is one thing that doesn't want to go right. I put all my information out my database into $data. When i do this
print_r($data);
My webpage gives me this:
(
[0] => stdClass Object
(
[reparatie_id] => 19
[customer_id] => 4
[medewerker] => 4
[name] => Joost
)
)
Everything seems to be good but when i try to do this:
echo $data->voornaam;
I keep getting this error
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: reparaties/cases.php
Line Number: 7
Backtrace:
File: C:\Ampps\www\beco\application\views\reparaties\cases.php
Line: 7
Function: _error_handler
File: C:\Ampps\www\beco\application\controllers\Reparaties.php
Line: 57
Function: view
File: C:\Ampps\www\beco\public\index.php
Line: 315
Function: require_once
Since your $data is a single-dimensional-array,so it need to be-
$data[0]->reparatie_id;
$data[0]->customer_id;
$data[0]->medewerker;
$data[0]->name;//so on for other indexes
Actually the $data array has an object at 0 position. So you need to any property of object. do like this:
<?php
$data[0]->reparatie_id;
$data[0]->customer_id;
$data[0]->medewerker;
$data[0]->name; ?>
output will be:
19, 4, 4, joost
First of all, your $data array does not have the voornaam element in it. So, assuming you want to echo out the elements that are inside the array, you would use the foreach array, like this:
foreach($data as $value) {
echo $value->name;
echo $value->voorname; //if it exists
}
But, if you just want to access that single element from the array then you would do this:
echo $data[0]->name;
I'm trying to extract some variables from my REQUEST_URI:
liquidfinger.com/key1-value1/key2-value2/key3-value3
I want to transform these values into a php associative array. My code is as follows:
$pState = [];
$stateString = substr($_SERVER['REQUEST_URI'],1);//remove leading slash
if($stateString){
$statePairs = explode("/",$stateString);
foreach($statePairs as $statePair){
$statePairArray = explode("-",$statePair);
$pState[$statePairArray[0]] = $statePairArray[1];
}
}
The $pState array is being created correctly and I can echo all the keys and values. However, I am getting an error_log:
Undefined offset: 1
I am even getting an error_log when there are no key-value pairs, so the IF statement shouldn't be executed, but possibly that is a characteristic of the error_log?
Okay, just to recap, the code was working but I was getting error messages. Further tests yielded the following:
url: www.liquidfinger.com
print_r($pState): Array ( )
[11-Mar-2016 10:01:02 UTC] PHP Notice: Undefined offset: 1 in /home/adamglynsmith/public_html/index.php on line 20
url: http://www.liquidfinger.com/user-2/tab-browseAll/marker-101
print_r($pState): Array ( [user] => 2 [tab] => browseAll [marker] => 101 )
print_r($statePairArray): Array ( [0] => user [1] => 2 ) Array ( [0] => tab [1] => browseAll ) Array ( [0] => marker [1] => 101 )
[11-Mar-2016 10:14:41 UTC] PHP Notice: Undefined offset: 1 in /home/adamglynsmith/public_html/index.php on line 20
[11-Mar-2016 10:14:43 UTC] PHP Notice: Undefined offset: 1 in /home/adamglynsmith/public_html/index.php on line 20
Since I have spent quite enough time on it and since I ultimately wanted to end up with a JavaScript array, I solved - or avoided - the problem by using the php to construct a string for the JavaScript like so:
$stateString = str_replace("/","', ",$stateString);//get rid of slashes
$stateString = str_replace("-",":'",$stateString);//get rid of dashes
$stateString .= "'";//add final single quote
<script>
jState = {<?php echo $stateString; ?>};
</script>
Thanks.
PHP does not guard your code implicitly, so you have to do some extra work to make sure you're actually able to do the correct thing:
$pState = [];
$stateString = substr($_SERVER['REQUEST_URI'],1);//remove leading slash
if($stateString){
$statePairs = explode("/",$stateString);
if (count($statePairs)) >= 1 { // Technically unnecessary unless you want to log the fact you didn't find anything.
foreach($statePairs as $statePair){
$statePairArray = explode("-",$statePair);
if (count($statePairArray) == 2) { // Found two and only two elements
$pState[$statePairArray[0]] = $statePairArray[1];
} else {
//It is very helpful in log statements to include the values of things that produced unexpected results
Log.warn("Invalid key-pair found for statePairArray=" + $statePairArray);
}
}
} else {
LOG.warn("No state pairs found for stateString=" + $stateString);//Or some other appropriate log or exception
}
}
explode may return an empty array if there is nothing to explode on. Therefore you need to check and take an appropriate action if it isn't there.
I am trying to read the array from the following code. I thought it should be easy but I am struggling.
My code pulls back two variables in the array.
$stmt5=$mysql_link->prepare("SELECT stationlong AS stationlong,tpl AS tpl FROM station WHERE stationlong=:stationlong LIMIT 1");
$stmt5->execute(array(':stationlong'=>$q));
$stations=$stmt5->fetchAll(PDO::FETCH_KEY_PAIR);
var_dump($stations);
$stationlongs=$stations[0];
$stationshorts=$stations[1];
An example array is as follows:-
array(1) {["Leicester"]=>string(6) "LESTER"}
My error is NOTICE: undefined offset: 0 in........... for $stationlongs=$stations[0]; and the same again for $stationshorts but with offset 1
Key 0 doesn't exist. As you see in dump, you have one record. There are no keys 0 and 1 in your one-item array.
echo $stations['Leicester']; // returns LESTER
Facing a weird situation with arrays..
I am using LinkedIn API to get profile info which returns data in two formats..
If user has just one educational item
educations=>education=>school-name
educations=>education=>date
...
If more than one education item
educations=>education=>0=>school-name
educations=>education=>0=>date
...
educations=>education=>1=>school-name
educations=>education=>1=>date
...
Now I am trying to make it consistent and convert
educations=>education=>school-name
to
educations=>education=>0=>school-name
But getting error in code that i believe should work
if(empty($educations['education'][0]['school-name']))
{
$temp = array();
$temp['education'][0]=$educations['education'];
$educations = $temp;
}
This fails for "just one educational item", generates error on the first line for (isset,is_array and empty)
PHP Fatal error: Cannot use string offset as an array in ...
print_r returns
[educations] => Array
(
[education] => Array
(
[id] => 109142639
[school-name] => St. Fidelis College
[end-date] => Array
(
[year] => 2009
)
)
)
Usually you'd write the assignment like this:
$temp = array(
"education" => array($educations['education'])
);
To avoid any issues with indexes. This might also fix yours.
If you're unsure about the contents of $educations['education'][0]['school-name'] you can simply check each part:
if(isset($educations['education'], $educations['education'][0], $educations['education'][0]['school-name']))
This works because isset doesn't behave like a normal function. It takes multiple arguments in a lazy manner.
You want:
if(array_key_exists('school-name',$educations['education']))
{
$educations['education'] = array($educations['education']);
}
Today I experienced the same problem in my application. Fatal error: Cannot use string offset as an array in /home/servers/bf4c/bf4c.php on line 2447
line 2447
if (!isset($time_played[$player]["started"])) {
$time_played[$player]["started"] = $time;
}
$time_played was overwritten elsewhere and defined as a string. So make sure you do use unique variable names.
Here's a tip if you're running through a loop, and it breaks:
if( $myArray != "" ){
// Do your code here
echo $myArray['some_id'];
}