Jump to content

Problem with PHP connecting to MySQL...


maxiscool1994

Recommended Posts

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...

Link to comment
Share on other sites

  • Replies 51
  • Created
  • Last Reply

Top Posters In This Topic

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

I am getting confused what you are trying to do. Seeing your code i am  :o

<?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']

 

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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 ;

 

Link to comment
Share on other sites

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 ;)

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.