Hi, Im learning php, and I cannot get the following code bellow to work: PHP: <?php //Connect to mysql $odbc = mysql_connect ('localhost', 'root', '') or die("Could not connect to database"); //connect to database mysql_select_db('IDBBooks',$odbc) or die("Can not find database"); //Run query $sql = "SELECT * FROM books"; //Write data to screen $result = mysql_query($sql,$odbc)or die("Can not run query"); if(mysql_num_rows($result)==0) { //If no results echo "no records returned"; } else { //If results while($row=mysql_fetch_object($result)) { echo "<H1>"; echo $row->Title; echo "</h1>"; echo "<p>"; echo $row->Image; echo ","; echo $row->Format; echo ","; echo $row->Price; echo "</p>"; echo "<BR>"; } } ?> The response i have is: Code: "; echo '$row->Title'; echo ""; echo " "; echo $row->Image; echo ","; echo $row->Format; echo ","; echo $row->Price; echo " "; echo " "; } } ?> This is copied straight from the lecturer's notes essentially with small changes to look at my database, im just very confused (And yes I know I need to password root.) Thanks in advanced for the help DT.
You need to run the script through PHP interpreter. You should install one of the preconfigured Apache+MySQL+PHP packages.
I have xampp with MYSQL and APACHE running currently, so unless I have broke xampp i doubt it will be that :S Now its suddenly working, and i haven't touched anything, anyone have any ideas why this would happen?
If you actually copied it straight from the lecturers notes my guess is that some of the double quotes weren't actually double quotes but special characters that look like double quotes.
Actually no tehBoris, because if you look at the output, then it is simply the browser trying to interpret the PHP file itself as-is, not interpreted by the server. Everything from <?php to echo "<H1> was interpreted as one tag, not rendered by the browser (because it doesn't know such tag). Then the following code was rendered as text : Code: "; echo $row->Title; echo " The "errorneus" end-tag </h1> was ignored, the rest was rendered according to HTML rules. Now the question is why didn't the server interpreted the PHP file, and i have no answer to that. The only thing i could think of is if your PHP file didn't had a .php file extension (or registered to the PHP interpreter) or if the PHP module didn't start due bad configuration, but that wouldn't explain why it did work when he tried it for second time.
faugusztin is correct. It wasn't being served up by xampp as a php file, instead just being displayed by the browser. You can confirm this by copying the code, putting it into notepad, saving as (all files) whatever.html and double clicking. I would have to guess that either xampp hadn't started correctly, or instead of navigating to localhost:80/whatever.php (or .html) deathtaker just double clicked on the file.
lint in the future you can do php -l filename.php to highlight syntax errors, make sure the code is good before altering your environment