Jump to content

Problem with PHP connecting to MySQL...


maxiscool1994

Recommended Posts

  • Replies 51
  • Created
  • Last Reply

Top Posters In This Topic

I've got a new issue:

 

I am trying again to connect to my jokes database, this time with a few added things: an author name and e-mail address. The PHP is connecting to my database, but none of my jokes are showing up... Here is the code:

 

<!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
  
$jokelist = @mysql_query(
    'SELECT joketext, name, email
    FROM joke, author WHERE authorid=author.id');
if (!$jokelist) {
  exit('<p>Error performing query: ' . mysql_error() . '</p>');
}

while ($joke = mysql_fetch_array($jokelist)) {
  $joketext = $joke['joketext'];
  $name = $joke['name'];
  $email = $joke['email'];
  
  // Display the joke with author information
  echo "<p>$joketext<br />" .
      "(by <a href='mailto:$email'>$name</a>)</p>";
}

?>
</blockquote>
</body>
</html>

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:

 

$result = @mysql_query('SELECT joketext FROM joke');
if (!$result) {
exit('<p>Error performing query '. mysql_error() . '</p>'
}

 

The reason the '}' is unexpected is because you have no ');' on the end of line 52. It should be

 

$result = @mysql_query('SELECT joketext FROM joke');
if (!$result) {
exit('<p>Error performing query '. mysql_error() . '</p>');
}

 

Sorry if I'm behind on times and you've already fixed it :P

Link to comment
Share on other sites

Sorry for the double post; i couldn't edit my last post..

 

<?php
$jokelist = @mysql_query(
    'SELECT joketext, name, email
    FROM joke, author WHERE authorid=author.id');
if (!$jokelist) {
  exit('<p>Error performing query: ' . mysql_error() . '</p>');

}

while ($joke = mysql_fetch_array($jokelist)) {
  $joketext = $joke['joketext'];
  $name = $joke['name'];
  $email = $joke['email'];
  
  // Display the joke with author information
  echo "<p>$joketext<br />" .
      "(by <a href='mailto:$email'>$name</a>)</p>"; /* you need to break the string eachtime to add the variable otherwise it won't parse. it's possibly better to use div as well; that may be a personal preference, though. */
}
?>

 

 

Would probably work a lot better as:

<?php
$sql = 'SELECT `joketext`, `name`, `email`
    FROM `joke`, `author` WHERE `authorid`= author.id'; //What is 'author.id'?

$jokelist = mysql_query($sql) or die(mysql_error()); 


while ($joke = mysql_fetch_array($jokelist)) {
  $joketext = $joke['joketext'];
  $name = $joke['name'];
  $email = $joke['email'];
  
  // Display the joke with author information 
  echo "<div>".$joketext."<br />";
  echo "(by <a href='mailto:".$email."'>".$name."</a>)</div>";
}
?>

 

If you try it I think that will work! Although... how are you actually inserting data into your database? On that page there is no INSERT INTO statement. Or is that on another page?

 

Sam

Link to comment
Share on other sites

n~ link=topic=168706.msg744857#msg744857 date=1195792405]

Hmmm again some stuff which i don't understand

$jokelist = mysql_query(
    'SELECT joketext, name, email
    FROM joke, author WHERE authorid=author.id');

 

Have you made 2 tables joke and author  ???

 

I now doubt if you are following the example properly  ;D

 

Yes, I have, as it told me to do in the book. It is moving on into relational database design, and it said that it is easy to display info from more than one table with a join...This book seems to be pointing me in a bad direction. None of the code works...I think I will drop by the book store and pick up Sam's or something...

Link to comment
Share on other sites

n~ link=topic=168706.msg744935#msg744935 date=1195801747]

Paste both the table structures here , i am going to follow your way and Upload it. I still don't think that book you are using is a bad one. If you got the CD in that book try running the examples from it and see the difference in your code and in the CD.

 

 

I have been, and none of the stuff works...

 

Here are the structures:

 

Joke:

 

mysql> show columns from joke;

+----------+-------------+------+-----+---------+----------------+

| Field    | Type        | Null | Key | Default | Extra          |

+----------+-------------+------+-----+---------+----------------+

| jokeid  | int(9)      | NO  | PRI | NULL    | auto_increment |

| joketext | blob        | NO  |    |        |                |

| jokedate | varchar(50) | NO  |    |        |                |

| authorid | int(11)    | YES  |    | NULL    |                |

+----------+-------------+------+-----+---------+----------------+

4 rows in set (0.02 sec)

 

 

Author:

mysql> show columns from author;

+-------+--------------+------+-----+---------+----------------+

| Field | Type        | Null | Key | Default | Extra          |

+-------+--------------+------+-----+---------+----------------+

| id    | int(11)      | NO  | PRI | NULL    | auto_increment |

| name  | varchar(255) | YES  |    | NULL    |                |

| email | varchar(255) | YES  |    | NULL    |                |

+-------+--------------+------+-----+---------+----------------+

3 rows in set (0.01 sec)

Link to comment
Share on other sites

@Neon: That code didn't change anything. It still doesn't display any jokes...

 

@wsantos: Here is the result for the first query:

mysql> SELECT joketext, name, email FROM joke, author WHERE authorid=author.id;

Empty set (0.00 sec)

 

The second...:

mysql> SELECT j.joketext, a.name, a.email FROM joke j JOIN author a ON j.authori

d=a.id;

Empty set (0.00 sec)

Link to comment
Share on other sites

n~ link=topic=168706.msg744959#msg744959 date=1195804596]

See there is no jokes in your tables so it returned empty results.... see here

http://shankhadharshakwa.org/check.php i added manually (5) and it is displaying  with your code...

 

Wow...I'm freaking retarded...There used to be a ton of jokes in there...what happened to them?

Link to comment
Share on other sites

You should probably condsider a link to clear the page of jokes too. At the moment you can view them but then have to change the URL in order to clear them again. Which isn't very user friendly ; also the layout could be better, though I understand that you're still just making the code for it rather than layout.

 

The code /works/ now.

Link to comment
Share on other sites

I have two I like, one is How to do everything with PHP & MYSQL, and is a great starter book, and I found it on Amazon for like $5.00

 

Helraizer, please realize that I am doing this as an example out of a book, so as long as I can work through it, its good....

 

Does anyone have any good books on learning PHP/MySQL they'd recommend?

Link to comment
Share on other sites

Helraizer, please realize that I am doing this as an example out of a book, so as long as I can work through it, its good....

 

Does anyone have any good books on learning PHP/MySQL they'd recommend?

 

I know, and that is fair enough. I was meaning if you were to take it further. :)

 

Depends on how you learn but are a number of good books out there; or if needs be, Google it and there are a lot of PDFs you can download which fit your purpose.

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.