Testing MySQL PHP

Hello,

I have downloaded XAMPP and am trying to teach myself PHP MySQL using the book PHP MySQL for Dummies by Janet Valade 2010 edition. The book has not been very helpful so far. I was able, using a video on YouTube from River City Graphics learn how to set up XAMPP with a user name and password. Then I went into XAMPP and set up a trial database. With the book, I did a very simple test file for PHP that when I opened it in a web browser by typing in the address bar localhost.test.php, everything worked fine. I then went to the section on Testing MySQL. I typed the following code:

[php]<?php
/* Program: mysql_test.php

  • Desc: Connects to MySQL Server and
  •      outputs settings.
    

*/
echo "
Test MySQL
";
$host = “hostname”;
$user = “mysqlaccount”;
$password = “mysqlpassword”;

$cxn = mysqli_connect($host,$user,$password);
$sql=“SHOW DATABASES”;
$result = mysqli_query($cxn,$sql);
if($result == false) {
echo “

Error: “.mysqli_error($cxn).”

”;
} else {
if(mysqli_num_rows($result) < 1) {
echo “

No current databases

”;
} else {
echo “
    ”;
    while($row = mysqli_fetch_row($result)) {
    echo “
  1. $row[0]
  2. ”;
    }
    echo “
”;
}
}
?> [/php]

I saved it as instructed as mysql_test.php. I changed the $host = “hostname” to “localhost” as the book said to. Also, per the book’s instructions, I changed the $user = “mysqlaccount” to “root”. For $password I substituted my own password.

Yet, when I type in the address bar localhost/mysql_test.php I only get an error message stating "Parse error: syntax error, unexpected ‘:’ in C:\xampphtdocs\mysql_test.php on line 11 where my password goes.

The book is absolutely no help. It basically says if you receive an error message to figure it out yourself. Can anybody please help me? Can anyone tell me what I have done wrong or at tell me where to look? Any help or advice is greatly appreciated.

Thank you

Michelle Jensen

All it means is that it found a : somewhere it shouldn’t be. The line number is hardly ever accurate, just use it as a starting point. It might be misinterpreting the comment block. Try making it a regular comment like

// Program: mysql_test.php
// Desc: Connects to MySQL Server and outputs settings.

No sense in wasting spacing if you don’t have too.

I agree with richei about the comments near the beginning.

Also, you have no browser doctypes declared. This could be an issue depending which browser
you are using. I suggest placing your PHP code inside the body. Something like this:
[php]

Test MySQL <?PHP // Program: mysql_test.php // Desc: Connects to MySQL Server and outputs settings.

$host = “hostname”;
$user = “mysqlaccount”;
$password = “mysqlpassword”;

$cxn = mysqli_connect($host,$user,$password);
$sql=“SHOW DATABASES”;
$result = mysqli_query($cxn,$sql);
if($result == false) {
echo “

Error: “.mysqli_error($cxn).”

”;
} else {
if(mysqli_num_rows($result) < 1) {
echo “

No current databases

”;
} else {
echo “
    ”;
    while($row = mysqli_fetch_row($result)) {
    echo “
  1. $row[0]
  2. ”;
    }
    echo “
”;
}
}
?> [/php] There are many other ways of handling the DOCTYPES. This is just one basic way. Test this version in your system and let us know. Remember to tell us which browser you are using for your tests and we can duplicate it that way. But, it is hard for us to know your exact setup of your database system. Here is a nice site for you to read up on how connections work. On the third page (click on next-chapter links) there is a nice example of how to open your MySQL database with full error checking. It basically just shows the error, which you can read and give us so we can help you better. The error messages will help us help you.

Hope this helps… http://www.w3schools.com/php/php_mysql_intro.asp
(NOTE: W3-Schools have tons of nice, easy to understand tutorials, check them out! )

Sponsor our Newsletter | Privacy Policy | Terms of Service