Error when changing MySQL syntax to Oracle syntax in SQL code in PHP

326 Views Asked by At

So I got this project to take a page that was previously using MySQL and now using Oracle. So I installed the oci8 PHP module and created a test page to make my changes on. This is a page I am updating and I am not an Oracle guy. I am a MySQL/PostgreSQL guy.

Anyway, there are these original parameters:

$webIndex = mysql_db_query('main', 'SELECT * FROM links2');
while ($garbage = mysql_fetch_array($webIndex)) {
    $descriptions = $garbage['descr'];
    $urls = $garbage['url'];
    print "<a href=\"$urls\">$descriptions</a><br />";
    $i++;
}

So I replaced the lines as follows:

$webIndex = oci_parse('main', 'SELECT * FROM links2');
while ($garbage = oci_fetch_array($webIndex)) {
    $descriptions = $garbage['descr'];
    $urls = $garbage['url'];
    print "<a href=\"$urls\">$descriptions</a><br />";
    $i++;
}

Upon doing this, tailed -f the apache error log and got:

[Thu Jan 02 14:41:24 2014] [error] [client 10.10.102.75] PHP Warning:  oci_parse() expects parameter 1 to be resource, string given in /srv/www/htdocs/db/index-woods.php on line 15
[Thu Jan 02 14:41:24 2014] [error] [client 10.10.102.75] PHP Warning:  oci_fetch_array() expects parameter 1 to be resource, null given in /srv/www/htdocs/db/index-woods.php on line 17
[Thu Jan 02 14:41:24 2014] [error] [client 10.10.102.75] PHP Notice:  Undefined variable: pageText2 in /srv/www/htdocs/db/index-woods.php on line 105
[Thu Jan 02 14:41:24 2014] [error] [client 10.10.102.75] PHP Notice:  Undefined variable: pageText3 in /srv/www/htdocs/db/index-woods.php on line 106

The last two errors are related to the first two. Based on these errors, it appears that oci_parse and oci_fetch_array are incorrect. These are lines 15 and 17 BTW.

Once again I do not know Oracle...

1

There are 1 best solutions below

1
On

This has nothing to do with knowing Oracle or not. This has to do with knowing PHP.

PHP Warning: oci_parse() expects parameter 1 to be resource, string given in /srv/www/htdocs/db/index-woods.php on line 15

You should learn that this PHP is warning about parameter type, and be able to look up the function arguments in the manual.

If you look at the documentation for oci_parse(), you'll see that the first argument should be a connection resource, which you get from oci_connect(). There's also a code example of using oci_connect(), and subsequently using the $conn as the first argument to oci_parse().

The following errors in your log are all due to the fact that the call to oci_parse() did not work.

Always check the return value for each of oci_parse() and oci_execute() because if there's any problem, those functions return false (this is also stated in the manual). The false value cannot be used as a query resource in subsequent calls to oci_fetch_array() and other functions.

See examples of checking for errors here: http://php.net/manual/en/function.oci-error.php