Click here to Skip to main content
15,919,774 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I appear to have a syntax problem using the list feature in PHP. In the code below I am trying to convert two array values into two seperate variables, using one line of code. I know this is possible with a simple variable. I can do it successfully on two seperate lines - as per the lines commented out work just fine.
Can somone please assist with the correct suntax for the line :-
list($outlying, $base) = (array($row['outlying_price'] ['base_price']));
as this line returns the following error:-

Warning: Illegal string offset 'base_price' in C:\wamp64\www\test.php on line 19
Notice: Undefined offset: 1 in C:\wamp64\www\test.php on line 19

What I have tried:

<?php

			include 'database.php';
			$pdo = Database::connect();
			try{
			$collhub = "bet";
			$delhub = "jnb";
			echo $collhub;
			echo $delhub;
			echo "<br>";
			$sql = "SELECT * FROM `new_ice_price_table` WHERE collection_town_code = '$collhub' AND delivery_town_code = '$delhub'";
			$result = $pdo->query($sql);
						if($result->rowCount() > 0){
						//echo "<table class='table table-striped table-bordered sortable'>";
						while($row = $result->fetch()){
						//print_r($row);
						//list($base) = array($row['base_price']);
						//list($outlying) = array($row['outlying_price']);
						list($outlying, $base) = (array($row['outlying_price'] ['base_price']));
						echo "<br>";
						echo $base;
						echo "<br>";
						echo $outlying;
					}
						echo "</table></br>";

						unset($result);
					} else{
						echo "No records matching your query were found.";
					}
					} catch(PDOException $e){
					die("ERROR: Could not able to execute $sql. " . $e->getMessage());
		}			
	?>
Posted
Updated 8-Aug-18 0:58am
Comments
Richard MacCutchan 8-Aug-18 6:41am    
The message is quite clear, the second offset in $row['outlying_price'] ['base_price'] is illegal, since the row array is one-dimensional.

$row is a one dimensional array. Because the items in this array are strings, you can access the characters of the items using the [] operator. But that requires passing a numeric index instead of a string index as indicated by the error message.

Why do you want to pack the returned row items into a list? It makes no sense at all. And so does using array() on items:
PHP
// These are the values which are strings
$base_price_value = $row['base_price'];
$outlying_price_value = $row['outlying_price'];
// Would this makes sense?
list($base) = array($base_price_value);
In any case you are creating a much more complex copy of the already existing array $row.
 
Share this answer
 
I would guess that what you actually need is:
PHP
list($outlying, $base) = (array($row['outlying_price'], $row['base_price']));

as shown at PHP: list - Manual[^].
 
Share this answer
 
Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
PHP
<?php

include 'database.php';
$pdo = Database::connect();
try{
  $collhub = "bet";
  $delhub = "jnb";
  echo $collhub;
  echo $delhub;
  echo "<br>";
  $sql = "SELECT * FROM `new_ice_price_table` WHERE collection_town_code = '$collhub' AND delivery_town_code = '$delhub'";
  $result = $pdo->query($sql);
  if($result->rowCount() > 0){
    //echo "<table class='table table-striped table-bordered sortable'>";
    while($row = $result->fetch()){
      //print_r($row);
      //list($base) = array($row['base_price']);
      //list($outlying) = array($row['outlying_price']);
      list($outlying, $base) = (array($row['outlying_price'] ['base_price']));
      echo "<br>";
      echo $base;
      echo "<br>";
      echo $outlying;
    }
    echo "</table></br>";

    unset($result);
  } else{
    echo "No records matching your query were found.";
  }
}
catch(PDOException $e){
  die("ERROR: Could not able to execute $sql. " . $e->getMessage());
}
?>

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
PHP
$sql = "SELECT * FROM `new_ice_price_table` WHERE collection_town_code = '$collhub' AND delivery_town_code = '$delhub'";

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
v2
Comments
Member 13840807 8-Aug-18 8:02am    
Thanks for the spelling bee check @ppolymorphe.
I never asked for a lesson in coding techniques, nor sql injection, but rather for help on one line of code. The submitted code is simply put together to include the list() string so I could get help with that.
I thought your response would be reserved for stackoverflow.
Your response is therefore of no use to anyone.
Patrice T 8-Aug-18 9:32am    
"I never asked for a lesson in coding techniques, nor sql injection, but rather for help on one line of code."
You never said that you knew indentation and SQL injection and that you don't care.
Note that indentation is not for you, it is to help us reading your code.
"Your response is therefore of no use to anyone."
Speak for yourself.
Member 13840807 8-Aug-18 10:00am    
You never once attempted to answer my actual question, but rather only to criticize my code. Rather don't reply if you are not contributing to the actual question as this is counter-productive, even at your expense of maintaining a top expert spot......
Patrice T 8-Aug-18 10:57am    
You already had 2 solutions on topic when I did it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900