Forum Moderators: coopster

Message Too Old, No Replies

Help / undefined function mysql connect()

mysql_connect()

         

Ghaleb

10:35 am on Feb 19, 2007 (gmt 0)

10+ Year Member



Dear all

I am a php programing beginner .... and the below code is from a learning lesson ....

I am trying to connect to the data base to store data using the below code, bearing in mind that the i created the table in the data base and code for retrieving data is working well ... but the problem is that i can not submit new data using the below page or code

<html>
<head>
<title> The Internet Joke Database </title>
</head>
<body>
<?php
if (isset($_GET['addjoke'])): // If the user wants to add a joke
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<p>Type your joke here:<br />
<textarea name="joketext" rows="10" cols="40" wrap>
</textarea><br />
<input type="submit" name="submitjoke" value="SUBMIT" />
</p>
</form>
<?php
else: // Default page display
// Connect to the database server
$dbcnx = @mysql_connect('localhost', 'root', '123456');
if (!$dbcnx) {
die( '<p>Unable to connect to the ' .
'database server at this time.</p>' );
}
// Select the jokes database
if (! @mysql_select_db('jokes') ) {
die( '<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['submitjoke'])) {
$joketext = $_POST['joketext'];
$sql = "INSERT INTO Jokes 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 Jokes');
if (!$result) {
die('<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>

I am getting this message:

Forbidden

You don't have permission to access /Training PHP/< on this server.
Apache/2.0.59 (Win32) PHP/5.2.0 Server at localhost Port 80

Also, result of debugging is:

Debug Error: C:\wamp\www\Training PHP\The Internet Joke Database.php line 19 - Call to undefined function mysql_connect()

What should i do ...
10x in advance

camilord

12:08 pm on Feb 19, 2007 (gmt 0)

10+ Year Member



are you working on your computer, localhost?

did you change your root password?

coz the default setup after installation is the root has no password at all....

coopster

4:22 pm on Feb 19, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, Ghaleb.

Looks like you have not enabled MySQL [php.net] in your PHP installation yet.

rokec

5:50 pm on Feb 19, 2007 (gmt 0)

10+ Year Member



If you are using own host, then you probably didn't set mysql or you didn't set it correctly.

If you are hosting somewhere else, they probably don't support mysql.

Ghaleb

8:24 am on Feb 21, 2007 (gmt 0)

10+ Year Member



Hi to all ...

Yes I am working on my computer localhost, and the root password is 123456 (and I am working on WAMP, if this make it clearer to you guys)

Let me rephrase my question; why I can connect to the data base to retrieve data using this code:

<html>
<head>
<title> Our List of Jokes </title>
</head>
<body>
<?php
// Connect to the database server
$dbcnx = @mysql_connect('localhost', 'root', '123456');
if (!$dbcnx) {
die( '<p>Unable to connect to the ' .
'database server at this time.</p>' );
}
// Select the jokes database
if (! @mysql_select_db('jokes') ) {
die( '<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 Jokes');
if (!$result) {
die('<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>

While I can not submit any?

Using this code
<html>
<head>
<title> The Internet Joke Database </title>
</head>
<body>
<?php
if (isset($_GET['addjoke'])): // If the user wants to add a joke
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<p>Type your joke here:<br />
<textarea name="joketext" rows="10" cols="40" wrap>
</textarea><br />
<input type="submit" name="submitjoke" value="SUBMIT" />
</p>
</form>
<?php
else: // Default page display
// Connect to the database server
$dbcnx = @mysql_connect('localhost', 'root', '123456');
if (!$dbcnx) {
die( '<p>Unable to connect to the ' .
'database server at this time.</p>' );
}
// Select the jokes database
if (! @mysql_select_db('jokes') ) {
die( '<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['submitjoke'])) {
$joketext = $_POST['joketext'];
$sql = "INSERT INTO Jokes 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 Jokes');
if (!$result) {
die('<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>

10x to all ...

mcibor

8:59 am on Feb 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try not to use root for phpscripts:

open phpmyadmin and in sql create a new user:

GRANT [dev.mysql.com] ALL ON jokes.* to 'new_user'@'localhost' IDENTIFIED BY 'password';

then try the following code:

<?php
echo 'I will try to connect do db<br />';
$host = 'localhost';
$user = 'new_user';
$pass = 'password';

$db = mysql_connect($host, $user, $pass) or die('Cannot connect to db');

echo '<br />Test successful';
?>

If you are still getting errors, then I recommend moving to xampp [apachefriends.org] distribution, I have no problems with it (and it doesn't even require installation)

Regards
Michal

And welcome to Webmasterworld!