0

I have searched around here and other sources for a solution to this issue but so far no luck finding the answer that solves the issue.

When trying to query and fetch the result from a MS SQL 2008 database I get an Array to string conversion error in the sqlsrv_query line.

This is the php code for accessing and querying.

 <?php
/* Specify the server and connection string attributes. */
$serverName = "DATABASE";
$connInfo = array( "Database"=>"Suggestion");
$conn = sqlsrv_connect( $serverName, $connInfo);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}

$query = "SELECT * FROM dbo.Suggestions";

$result = sqlsrv_query($conn, $query) OR die(sqlsrv_errors());

$val=sqlsrv_fetch_array($result,SQLSRV_FETCH_ASSOC);

echo $val;

?>

EDIT: As requested the actual error code below:

Notice: Array to string conversion in C:\wamp\www\DBTest\index.php on line 24

When taken into context of the entire file index.php line 24 is:

$result = sqlsrv_query($conn, $query) OR die(sqlsrv_errors());

Thanks in advance for any help!

5
  • 1
    Post the actual error. Commented Jul 24, 2013 at 12:10
  • Are you sure it's that line? echo $val; will cause a notice as $val is an array. Use print_r($val); Commented Jul 24, 2013 at 12:13
  • @JasonMcCreary The error is now posted as an edit to the original post. Thank you. Commented Jul 24, 2013 at 12:26
  • I see too many questions like this. Take the time to learn how to interpret errors and fix your code. Commented Jul 24, 2013 at 12:35
  • I am all for learning, Thank you. Commented Jul 24, 2013 at 12:50

1 Answer 1

1

You can not print an array with 'echo' you have to use either 'print_r()' or var_dump()

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I realized that after posting the code and fixed that however the code wont execute to that point with the standing error. Thank you though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.