maxiscool1994 Posted November 22, 2007 Share Posted November 22, 2007 I am learning with Kevin Yanks book, "Build you Own Database Driven Website," and I have run into a problem. I have uploaded a PHP file to my local server, and none of my content is being displayed from my database. According to the book, the jokes that I have entered into my database should be displayed using this code. Here is the error I am getting: Fatal Error: Call to undefined function mysql_connect() in C:\.... on line 14 Here is my PHP file: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Our List of Jokes</title> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> </head> <body> <?php // Connect to the database server $dbcnx = @mysql_connect('localhost', 'root', 'new password'); if (!$dbcnx) { exit('<p>Unable to connect to the ' . 'database server at this time.</p>'); } // Select the jokes database if (!@mysql_select_db('ijdb')) { exit('<p>Unable to locate the joke ' . 'database at this time.</p>'); } ?> <p>Here are all the jokes in our database:</p> <blockquote> <?php // Request the text of all the jokes $result = @mysql_query('SELECT joketext FROM joke'); if (!$result) { exit('<p>Error performing query: ' . mysql_error() . '</p>'); } // Display the text of each joke in a paragraph while ($row = mysql_fetch_array($result)) { echo '<p>' . $row['joketext'] . '</p>'; } ?> </blockquote> </body> </html> Note: I have MySQL installed and running on my machine... Quote Link to comment Share on other sites More sharing options...
luxe Posted November 22, 2007 Share Posted November 22, 2007 try changing your password to "root" also Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted November 22, 2007 Share Posted November 22, 2007 what ever version of php is installed lacks mysql thus you will need taht package installed. Quote Link to comment Share on other sites More sharing options...
maxiscool1994 Posted November 22, 2007 Author Share Posted November 22, 2007 I fixed that last problem, but now I have a new one. This is a revised version of that page, but with a whole bunch of new functions, and it is not showing up as it should. It says that it can't connect to the MySQL server.. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>The Internet Joke Database</title> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> </head> <body> <?php if (isset($_GET['addjoke'])): // If the user wants to add a joke ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <label>Type your joke here:<br /> <textarea name="joketext" rows="10" cols="40"> </textarea></label><br /> <input type="submit" value="SUBMIT" /> </form> <?php else: // Default page display // Connect to the database server $dbcnx = @mysql_connect('localhost', 'root', 'new password'); if (!$dbcnx) { exit('<p>Unable to connect to the ' . 'database server at this time.</p>'); } // Select the jokes database if (!@mysql_select_db('ijdb')) { exit('<p>Unable to locate the joke ' . 'database at this time.</p>'); } // If a joke has been submitted, // add it to the database. if (isset($_POST['joketext'])) { $joketext = $_POST['joketext']; $sql = "INSERT INTO joke SET joketext='$joketext', jokedate=CURDATE()"; if (@mysql_query($sql)) { echo '<p>Your joke has been added.</p>'; } else { echo '<p>Error adding submitted joke: ' . mysql_error() . '</p>'; } } echo '<p>Here are all the jokes in our database:</p>'; // Request the text of all the jokes $result = @mysql_query('SELECT joketext FROM joke'); if (!$result) { exit('<p>Error performing query: ' . mysql_error() . '</p>'); } // Display the text of each joke in a paragraph while ($row = mysql_fetch_array($result)) { echo '<p>' . $row['joketext'] . '</p>'; } // When clicked, this link will load this page // with the joke submission form displayed. echo '<p><a href="' . $_SERVER['PHP_SELF'] . '?addjoke=1">Add a Joke!</a></p>'; endif; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
Stephen68 Posted November 22, 2007 Share Posted November 22, 2007 Are you putting the "new password" there so that we don't see your password (good idea) or is it in the code? When I connect to the DB on my local machine I use blank for the password. Mind you my machine that I run code on is not connected to the Internet in any way. EG. $dbcnx = @mysql_connect('localhost', 'root', ''); Just my two cents, if this works maybe you should fire up PHPMyAdmin and create a new user and password for you DB use. hope I helped in some way cheers! Stephen Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted November 22, 2007 Share Posted November 22, 2007 Uncomment this line in php.ini file and restart Apache Server extension=php_mysql.dll If you have MySQL installed... Quote Link to comment Share on other sites More sharing options...
maxiscool1994 Posted November 22, 2007 Author Share Posted November 22, 2007 I took both of your guy's advice and none of it worked. I have already uncommented the extension in PHP.ini, and leaving the password blank changed nothing. Will PHPmyadmin work on a local machine? I have it on my web host, but what I am doing is not online. Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted November 22, 2007 Share Posted November 22, 2007 Try connecting like this <?php error_reporting(E_ALL); $conn = mysql_connect("localhost","root",''); $db=mysql_select_db("db_name"); if($conn=="") { trigger_error('Unable to connect to database: ' . mysql_error()); } ?> Quote Link to comment Share on other sites More sharing options...
maxiscool1994 Posted November 22, 2007 Author Share Posted November 22, 2007 I get this error now: Parse error: syntax error, unexpected '}' in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\jokes1.php on line 53\ Here is the code with the different connection method: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>The Most Basic Web Page in the World</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> </head> <body> <?php if (isset($_GET['addjoke'])) : //User Wants to add a Joke ?> <form action="<?php echo $_SERVER['PHP_SHELF']; ?>" method="post"> <label>Type your joke here:<br /> <textarea name="joketext" rows="10" cols="40"> </textarea></label><br /> <input type="submit" value="SUBMIT" /> </form> <?php else: //Default page display //connect to the database server error_reporting(E_ALL); $conn = mysql_connect("localhost","root",''); $db=mysql_select_db("db_name"); if($conn=="") { trigger_error('Unable to connect to database: ' . mysql_error()); } //Selects the jokes database if (!@mysql_connect_db('ijdb')) { exit('<p>Unable to locate the joke '.' database at this time.</p>'); } //If a joke has been submitted, add it to the database. if (isset($_POST['joketext'])) { $joketext = $_POST['joketext']; $sql = "INSERT INTO joke SET joketext= '$joketext', jokedate=CURDATE() "; if (@mysql_query(@sql)) { echo '<p>Your joke has been added.</p>'; } else { echo '<p>Error adding submitted joke: '. mysql_error() . '</p>'; } } echo '<p>Here are all the jokes in our database:</p>'; //Request the text of all the jokes $result = @mysql_query('SELECT joketext FROM joke'); if (!$result) { exit('<p>Error performing query '. mysql_error() . '</p>' } //display the text of each joke in a paragraph while ($row = mysql_fetch_array($result)) { echo '<p>' . $row['joketext'] . '</p>'; } //When Clicked, this link will load this page with the joke submission form displayed. echo '<p><a href="' . $_SERVER['PHP_SHELF'] . '?addjoke=1">Add a joke!</a></p>'; endif; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted November 22, 2007 Share Posted November 22, 2007 I am getting confused what you are trying to do. Seeing your code i am <?php if (!@mysql_connect_db('ijdb')) { // on line 23 this is defined above no need to define it twice and I haven't seen this mysql_conect_db not even in the manual?> You are using exit in place of echo.... or is it exit dunno exit('<p>Unable to locate the joke '.' database at this time.</p>'); You have defined variable $sql you need to use $sql below.. see if (@mysql_query(@sql)) { I think this is PHP_SELF you are using SHELF.... dunno if PHP guys ever made that $_SERVER['PHP_SHELF'] Quote Link to comment Share on other sites More sharing options...
maxiscool1994 Posted November 22, 2007 Author Share Posted November 22, 2007 I am using Kevin Yanks Book, and it has taught me all of these things that you seem to be confused about. Here is a link: http://www.sitepoint.com/books/phpmysql1/ I am an absolute beginner, so I really don't know where to start... Is there a better book? Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted November 22, 2007 Share Posted November 22, 2007 Now Try this <?php error_reporting(E_ALL); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>The Most Basic Web Page in the World</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> </head> <body> <?php if (isset($_GET['addjoke'])) { //User Wants to add a Joke ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <label>Type your joke here:<br /> <textarea name="joketext" rows="10" cols="40"> </textarea></label><br /> <input type="submit" value="SUBMIT" /> </form> <?php } else { //Default page display //connect to the database server $conn = mysql_connect("localhost","root",''); $db=mysql_select_db("ijdb"); // keep your db name here if($conn=="") { trigger_error('Unable to connect to database: ' . mysql_error()); } else { echo ("DB connection was successful"); } //If a joke has been submitted, add it to the database. if (isset($_POST['joketext'])) { $joketext = $_POST['joketext']; $sql = "INSERT INTO joke SET joketext= '$joketext', jokedate=CURDATE() "; if (mysql_query($sql)) { echo '<p>Your joke has been added.</p>'; } else { echo '<p>Error adding submitted joke: '. mysql_error() . '</p>'; } echo '<p>Here are all the jokes in our database:</p>'; //Request the text of all the jokes $result = mysql_query('SELECT joketext FROM joke'); if (!$result) { echo('<p>Error performing query '. mysql_error() . '</p>'); } //display the text of each joke in a paragraph while ($row = mysql_fetch_array($result)) { echo '<p>' . $row['joketext'] . '</p>'; } //When Clicked, this link will load this page with the joke submission form displayed. echo '<p><a href="' . $_SERVER['PHP_SELF'] . '?addjoke=1">Add a joke!</a></p>'; } } ?> </body> </html> And for the book, here is the link http://hudzilla.org/phpwiki/index.php?title=Main_Page Quote Link to comment Share on other sites More sharing options...
maxiscool1994 Posted November 22, 2007 Author Share Posted November 22, 2007 n~ link=topic=168706.msg744085#msg744085 date=1195708362] Now Try this And for the book, here is the link http://hudzilla.org/phpwiki/index.php?title=Main_Page It said that the DB connection was successful, but the textbox and the jokes were not displayed. Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted November 22, 2007 Share Posted November 22, 2007 See this line (isset($_GET['addjoke'])) your addjoke is not set so this condition is false and it goes to ELSE... change this to (!isset($_GET['addjoke'])) and it will show... What are you trying to do, i still don't understand ??? And your insert query is also wrong Quote Link to comment Share on other sites More sharing options...
maxiscool1994 Posted November 22, 2007 Author Share Posted November 22, 2007 n~ link=topic=168706.msg744092#msg744092 date=1195709358] See this line (isset($_GET['addjoke'])) your addjoke is not set so this condition is false and it goes to ELSE... change this to (!isset($_GET['addjoke'])) and it will show... What are you trying to do, i still don't understand ??? And your insert query is also wrong What I am trying to do is make a form that will insert a joke into my MySQL joke database. This is a project from Yank's book. Do you suggest that I just scrap that book and learn from that link you gave me? Is there a way I can download that entire website? Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted November 22, 2007 Share Posted November 22, 2007 uhm remove the at symbol first of this suppresses stuff! Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted November 22, 2007 Share Posted November 22, 2007 Now try this, this will work. <?php error_reporting(E_ALL); $conn = mysql_connect("localhost","root",''); $db=mysql_select_db("ijdb"); // keep your db name here if($conn=="") { trigger_error('Unable to connect to database: ' . mysql_error()); } // defien the variables $currpage = "joke.php"; $user_wants_to_view_joke = ""; // /* Now if user has pressed View Jokes then display the jokes......... */ if (isset($_REQUEST['viewjoke'])) { $joke=$_REQUEST['viewjoke']; $query = "SELECT * FROM joke"; $result = mysql_query($query); // check if there are jokes in the table if (!$result) { $errmsg = 'Invalid query: ' . mysql_error() . "\n"; $errmsg .= 'Whole query: ' . $query; die($errmsg); } else { $user_wants_to_view_joke = TRUE; } // ends here } else { $joke = ""; } /* */ // check if the user has submitted jokes if (isset($_POST['submit'])) { $joketext = $_POST['joketext']; $sql = "INSERT INTO joke( `jokeid` , `joketext` , `jokedate` ) VALUES ( NULL ,'$joketext', CURDATE( ) );"; $res = mysql_query($sql); // check if there is error in submitting if ($res) { $errmsg = "Your joke has been added."; } else { $errmsg = "Error submitting joke."; } } // end of submiting ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>The Most Basic Web Page in the World</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> </head> <body> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <label>Type your joke here:<br /> <textarea name="joketext" rows="10" cols="40"> </textarea></label><br /> <input type="submit" name="submit" value="SUBMIT" /> <a href="<?php echo $currpage?>?viewjoke=yes">View jokes</a> <br /><?php if (isset($errmsg)) { echo $errmsg; }?> </form> <p> <?php // if user wants to view the joke, show him the jokes......... if ($user_wants_to_view_joke) { while ($row = mysql_fetch_assoc($result)) { echo "<h2>".$row['joketext']."</h2><br />"; } } ?> </p> </body> </html> Quote Link to comment Share on other sites More sharing options...
maxiscool1994 Posted November 22, 2007 Author Share Posted November 22, 2007 That works, except that there is an error when I try to submit a joke. Should I just dump this book and use the one that you recommended? Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted November 22, 2007 Share Posted November 22, 2007 No that book is good too.. you could start from some simple examples and later jump to Database part. You can download PHP Manual from php.net too... so you can refer in that manual when you get certain errors. I don't know why you got error while submitting the joke. Maybe because of table structure mine was like this and for the page name I had named it joke.php -- -- Table structure for table `joke` -- CREATE TABLE `joke` ( `jokeid` int(9) NOT NULL auto_increment, `joketext` blob NOT NULL, `jokedate` varchar(50) NOT NULL, PRIMARY KEY (`jokeid`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ; Quote Link to comment Share on other sites More sharing options...
maxiscool1994 Posted November 22, 2007 Author Share Posted November 22, 2007 This is the first example in the book... The thing is, all of what you are saying isn't sinking in that much. Should I just bear through this project and do it again to understand it, or should I find a better book? Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted November 22, 2007 Share Posted November 22, 2007 Yes you do it again from scratch.. and see some tutorials here in phpfreaks site too... BTW i also started PHP 4 mths ago. I didn't study any books, Only the resources I have is a) Lots of tutorials downloaded from http://www.phpfreaks.com/ b) PHP Manual, MySQL reference Manual c) and this Forum (you can learn a lot from others problem and how they get solved Quote Link to comment Share on other sites More sharing options...
maxiscool1994 Posted November 22, 2007 Author Share Posted November 22, 2007 n~ link=topic=168706.msg744125#msg744125 date=1195713588] Yes you do it again from scratch.. and see some tutorials here in phpfreaks site too... BTW i also started PHP 4 mths ago. I didn't study any books, Only the resources I have is a) Lots of tutorials downloaded from http://www.phpfreaks.com/ b) PHP Manual, MySQL reference Manual c) and this Forum (you can learn a lot from others problem and how they get solved OK...do you have any idea on where I should start? Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted November 22, 2007 Share Posted November 22, 2007 First study the book you have now, do some examples then try and make something on your own. Just like a basic membership type (e.g. User Registration and Login). Quote Link to comment Share on other sites More sharing options...
maxiscool1994 Posted November 22, 2007 Author Share Posted November 22, 2007 ok. But what should I do about this entire mess that we just went through? It doesn't work! Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted November 22, 2007 Share Posted November 22, 2007 Ok then finally, Create a database named ijdb Create a table named joke CREATE TABLE `joke` ( `jokeid` int(9) NOT NULL auto_increment, `joketext` blob NOT NULL, `jokedate` varchar(50) NOT NULL, PRIMARY KEY (`jokeid`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ; Create a page named joke.php and paste these entire thing and run joke.php <?php error_reporting(E_ALL); $conn = mysql_connect("localhost","root",''); $db=mysql_select_db("ijdb"); // keep your db name here if($conn=="") { trigger_error('Unable to connect to database: ' . mysql_error()); } // defien the variables $currpage = "joke.php"; $user_wants_to_view_joke = ""; // /* Now if user has pressed View Jokes then display the jokes......... */ if (isset($_REQUEST['viewjoke'])) { $joke=$_REQUEST['viewjoke']; $query = "SELECT * FROM joke"; $result = mysql_query($query); // check if there are jokes in the table if (!$result) { $errmsg = 'Invalid query: ' . mysql_error() . "\n"; $errmsg .= 'Whole query: ' . $query; die($errmsg); } else { $user_wants_to_view_joke = TRUE; } // ends here } else { $joke = ""; } /* */ // check if the user has submitted jokes if (isset($_POST['submit'])) { $joketext = $_POST['joketext']; $sql = "INSERT INTO joke( `jokeid` , `joketext` , `jokedate` ) VALUES ( NULL ,'$joketext', CURDATE( ) );"; $res = mysql_query($sql); // check if there is error in submitting if ($res) { $errmsg = "Your joke has been added."; } else { $errmsg = "Error submitting joke."; } } // end of submiting ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>The Most Basic Web Page in the World</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> </head> <body> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <label>Type your joke here:<br /> <textarea name="joketext" rows="10" cols="40"> </textarea></label><br /> <input type="submit" name="submit" value="SUBMIT" /> <a href="<?php echo $currpage?>?viewjoke=yes">View jokes</a> <br /><?php if (isset($errmsg)) { echo $errmsg; }?> </form> <p> <?php // if user wants to view the joke, show him the jokes......... if ($user_wants_to_view_joke) { while ($row = mysql_fetch_assoc($result)) { echo "<h2>".$row['joketext']."</h2><br />"; } } ?> </p> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.