Hi there chaps. I've decided to learn PHP - I can already edit it pretty well from making wordpress themes / alterations. I'm stuck with a piece of sample coding. I've got a centos box setup with PHP (latest) and mysql with apache installed. If I try the standard 'Hello world' script it works fine. But if I try this: <html> <head> <title>Sample Connection</title> </head> <body><br><br> <div align="center"> <?php $db = mysql_pconnect("localhost", "root", "password"); if (!db) { print "Error: Could not connect to database."; exit; } mysql_select_db("programming"); $query = "select * from languages order by name asc"; $result = mysql_query($query); $num_results = mysql_num_rows($result); print "Today's web developers should be familiar with<br><br>"; for ($i=0; $i<$num_results; $i++) { $row = mysql_fetch_array($result); print ($row["name"]); print " on "; print ($row["platform"]); print "<br>"; } ?> </div> </body> </html> Code (markup): It displays the page title in the header but doesn't do anything from then on. It just returns a blank page. I've checked and double checked the mysql details (obviously in the piece of code I'm using I've changed it to suit my setup). Any help?!
try it like this mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $query = "select * from languages order by name asc'"; $result = mysql_query ($query); if ($result) { while($row=mysql_fetch_array($result)) { $name = $row["name"]; $platform = $row["platform"]; echo "$name on $platform"; } }
Ok, sorted that. error displayed: Fatal error: Call to undefined function mysql_pconnect() in /var/www/html/index.php on line 8
You haven't loaded the PHP MySQL extension in your PHP.INI file. Edit: With your OS you'll have to install the MYSQL extension, I have only experienced on Windows and Ubuntu/Debian. I believe the package is php-mysql and mysql-server.
you can test your mysql connection by creating a info.php file. As long as you have php loading create a file with <?php phpinfo(); ?> this will display all your php information and show you is you have mysql working with your php.