Justtechjobs.com Find a programming school near you






Online Campus Both


php-general | 2001122

Re: [PHP] MySQL problem From: GoodFella (goodfella_gf <email protected>)
Date: 12/30/01

Hiya,

Thanks for the quick reply. I used the PHP manual example and it connects
to the database successfully but cannot select the database.

I'm not sure why this is? I've looked hard at it and I cannot see where I have
gone wrong.

Thanks.

GF.
  ----- Original Message -----
  From: David Jackson
  To: goodfella_gf <email protected>
  Cc: php-general <email protected>
  Sent: Monday, December 31, 2001 1:48 AM
  Subject: Re: [PHP] MySQL problem

  Here's the example from the PHP manual:
  The tutorial here are very helpfull:
  http://www.melonfire.com/community/columns/trog/

  -- David

  <?php
  // Connecting, selecting database
  $link = mysql_connect("mysql_host", "mysql_login", "mysql_password")
      or die("Could not connect");
  print "Connected successfully";
  mysql_select_db("my_database")
      or die("Could not select database");

  // Performing SQL query
  $query = "SELECT * FROM my_table";
  $result = mysql_query($query)
      or die("Query failed");

  // Printing results in HTML
  print "<table>\n";
  while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
      print "\t<tr>\n";
      foreach ($line as $col_value) {
          print "\t\t<td>$col_value</td>\n";
      }
      print "\t</tr>\n";
  }
  print "</table>\n";

  // Closing connection
  mysql_close($link);
  ?>

> Hello,
>
> I am extremely new to MySQL and have never managed to get working
> smoothly with PHP before. I am trying really hard to understand
> how to work it, and am almost there.
>
> I have a problem which I do not know how to resolve and was
> wondering if anybody could help me. I have no idea what is wrong
> with the code and why I am getting the error message;
>
> Warning: Supplied argument is not a valid MySQL result resource in
> C:\apache\htdocs\sams\chapter10\results.php on line 47
>
> I am currently using a book to aid me with MySQL, and this is an
> example from the book. It does not seem to work and I have no idea what
> I may have done wrong to obtain this warning.
>
> I have changed my login and password to question marks.
>
> <?
>
> if (!$searchtype || !$searchterm)
>
> {
> echo "You have not entered search details. Please go back and try
> again.";
>
> exit;
>
> }
>
>
> $searchtype = addslashes($searchtype);
>
> $searchterm = addslashes($searchterm);
>
> @ $db = mysql_pconnect("mesh", "bookorama", "bookorama123");
>
> if (!$db)
>
> {
> echo "Error: Could not connect to database. Please try again
> later.";
>
> exit;
>
> }
>
> mysql_select_db("booktest");
>
> $query = "select * from booktest where ".$searchtype." like
> '%".$searchterm."%'";
>
> $result = mysql_query($query);
>
> $num_results = mysql_num_rows($result);
>
> echo "<p>Number of books found: ".$num_results."</p>";
>
> for ($i=0; $i <$num_results; $i++)
>
> {
>
> $row = mysql_fetch_array($result);
>
> echo "<p><strong>".($i+1).". Title: ";
>
> echo stripslashes($row["title"]);
>
> echo "</strong><br>Author: ";
>
> echo stripslashes($row["author"]);
>
> echo "<br>ISBN: ";
>
> echo stripslashes($row["isbn"]);
>
> echo "<br>Price: ";
>
> echo stripslashes($row["price"]);
>
> echo "</p>";
>
> }
>
> ?>
>
> The problem seems to be around the lines of code;
>
> $result = mysql_query($query);
>
> $num_results = mysql_num_rows($result);
>
> Any assistance is appreciated.
>
> Yours,
>
> GF.
>
> _________________________________________________________________
> Chat with friends online, try MSN Messenger: http://messenger.msn.com
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe <email protected>
> For additional commands, e-mail: php-general-help <email protected>
> To contact the list administrators, e-mail:
> php-list-admin <email protected>

  --