1

I have a MySQL db, with a functioning table. I am trying to get to it via a php page. The whole setup is WAMP2. Everything seems to work OK, I have connected to the MySQL and pulled back a basic 'select all' via PHP. I am now trying to search via PHP.

I can test the connection that comes with the install:

<?php 
$link = mysql_connect('localhost','testuser','testpass'); 
if (!$link) { 
    die('Could not connect to MySQL: ' . mysql_error()); 
} 
echo 'Connection OK'; mysql_close($link); 
?>

And I see the 'Connection OK' reply. Every attempt to connect with the table has failed in various ways, and I suspect I have something amiss with my config of either apache/php/mysql.

I tried the code here: http://www.tutorialized.com/view/tutorial/PHP-MySQL-database-search/416, and I get an error:

 Parse error: syntax error, unexpected T_VARIABLE in C:\wamp\www\search3.php on line 22

the line that gets the error?

$trimmed = trim($var) //trim whitespace from the stored variable

// rows to return 

$limit=10;      // line 22

My last attempt is to try this code: http://www.wallpaperama.com/forums/simple-php-mysql-connection-test-script-example-t5702.html which according to its preamble should let me test any connection.

When I run the page, I get errors pointing at any variable I have 'Notice: Undefined index" or "Notice: Undefined variable:".

I tried suppressing the warning, by using the @ prefix to a variable to see if it was a critical failure, and I get a follow on error of 'Parse error: syntax error,' or similar that relates to the function the variable is invoked for.

I suspect that there is something amiss in the variable declaration / handling, but I don't know where to look for pointers. I have checked the various logs and there is nothing I can see that illuminates further than the errors thrown on the browser page.

I'm a bit lost as to where to look next - this is my first play with PHP. Any suggestions?

5
  • If an unexpeted variable error occurs, the problem is not the unexpecte variable but the line before. Please include that as well. Commented Feb 12, 2012 at 6:22
  • the line that gets the error? no, something BEFORE that line (missing ; or } or )) triggers that error. Show the previous line. Commented Feb 12, 2012 at 6:22
  • Ok, thanks, previous line is '// rows to return' I am assuming that its commented out, the previous functional line is of interest: '$trimmed = trim($var) //trim whitespace from the stored variable' Commented Feb 12, 2012 at 6:26
  • What about use an IDE, and let it find out where the syntax error is. Commented Feb 12, 2012 at 6:26
  • @Jay If you are serious about PHP in a degree that you are willing to pay for it, then certainly JetBrains PhpStorm is the best PHP IDE I have seen so far (comes with a 30 day evaluation period and after that you must either pay or crack!). We use it in our company and we are living happily ever after. Otherwise your second best (and free) option is PDT (Php Development Tools) from the Eclipse project. The last time I checked it was a little buggy and somehow slow. I must warn you though. They both are fully fledged and professional IDEs targeted at pro PHP devs. Commented Feb 12, 2012 at 7:44

2 Answers 2

2

the line

 $trimmed = trim($var) //trim whitespace from the stored variable

is missing a semi-colon.

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

1 Comment

Ahh - thank you, that fixed the inital error - I think in every script I have tried, there must be missing ';'s. Thats odd. Ok, thank you - I will poke around somemore. Appreciated.
1

When I run the page, I get errors pointing at any variable I have 'Notice: Undefined index" or "Notice: Undefined variable:".

As the name implies, these are notices, not errors or even warnings. It is a common practice in dynamic languages to rely on this particular aspect of the language (using variables or indexes that might be undefined at the point they are used). To put it simple, in PHP such cases often just simply get evaluated to null. The problem is that, this is not a very good technique to stick with so you can ask PHP to issue proper messages (notices in this case) when encountering an undefined variable or index. You may suppress all notices using the error_reporting function: error_reporting(E_ALL & ~E_NOTICE) for a script or globally through the error_reporting settings in php.ini.

2 Comments

Ah - I think I get it! Awesome - the php page is being strictly evaluated, and so these 'minor' errors are being thrown back, which is causing the rest of the page to fall over. OK. Would you know what the loosest compliance setting I should use for error_reporting?
@Jay Yeah I think you got it right in that a notice may cause all the script to fall. Well I suppose E_ALL & ~E_NOTICE is reasonable (not too much relaxed nor very strict) but to answer your question you may suppress all things (except for certain very naughty circumstances of course) with error_reporting(0)

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.