0

I am trying to set the two outputs from this MySQL stored procedure as PHP variables:

$result = mysql_query("CALL mst2('$q', @eset, @leng)");
if (!$result) {  
  die('Invalid query: ' . mysql_error());
} 

while($row = @mysql_fetch_assoc($result))
{
debug($row);

}
$eset = $row->{'@eset'};
$length= $row->{'@leng'};

The last two line are throwing an error Trying to get property of non-object . Does anybody know the proper way to do this?

7
  • 1
    $row is an associative array so you need to use $row['indexName'] to access the values stored there. Commented Dec 6, 2012 at 0:52
  • It is cause $row is probably false on these two last lines cause it is after the while loop which is conditioned by it evaluating to true. So try to put these lines inside the while or get rid of the while as I am not sure if it is really needed. Commented Dec 6, 2012 at 0:53
  • 2
    Also, the mysql_ functions are now deprecated, you should be using mysqli_ instead. Commented Dec 6, 2012 at 0:54
  • @clime I tried getting rid of the loop but it threw another error : Undefined variable: row', I guess you can't reference the associative array outside of the loop. When I put the statement inside the loop though, the $eset` doesn't get set to the proper value, it instead remains the default value which is '' Commented Dec 6, 2012 at 1:03
  • 1
    @Mike It should be the column name you want to extract from the results of your query. Commented Dec 6, 2012 at 2:15

2 Answers 2

2

mysql_fetch_object instead of mysql_fetch_assoc should fix your query up.

Secondly though you should really look at either using mysqli_ or pdo statements.

Links here:

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

1 Comment

Petrogad, I attempted the mysqli_ approach, I got stuck with that about 2 hours ago: (stackoverflow.com/questions/13733873/…) I also tried your suggestion with mysql_fetch_object, but I had the same issues as I described to clime above.
1

Here's how I got it to work with mysql_query:

$result = mysql_query("CALL mst2('$q', @eset, @leng)");
$result = mysql_query("SELECT @eset, @leng");
if (!$result) {  
  die('Invalid query: ' . mysql_error());
} 

while($row = @mysql_fetch_object($result))
{
$eset = $row->{'@eset'};
}

right after the procedure I called a SELECT statement, then in the while loop, the $eset variable gets set properly.

1 Comment

One other thing i'd mention, when you do things like @mysql_fetch_object; you're essentially suppressing error messages (which is costly in PHP) and will be slower.

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.