Jump to content

[SOLVED] Newbie question..no database selected


drumhrd

Recommended Posts

Ok so I am a noob.

 

I have attempted to use multiple tutorials and they all end up have script errors.  Which I cannot figure out.  It's hard to learn something when every tutorial out there is not accurate, and has script errors.

 

I am getting to errors from the following code.

 

Notice: Query:

MySQL Error: No database selected in /var/www/links2.php on line 16

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/links2.php on line 17

Sorry, no records were found!

 

 

According to this code I would have assumed I have already selected the DB by using mysql_select_db("mydb",$db);

 

It seems as if the selected database does not pass from the if logic to the else logic.

 

Thanks in advance for your help.

 

 

<?php

$db = mysql_connect("localhost", "root". "<ommited>");

mysql_select_db("mydb",$db);

// display individual record

if ($id) {

  $result = mysql_query("SELECT * FROM employees WHERE id=$id",$db)  or trigger_error("Query: \n<br />MySQL Error: " . mysql_error());

  $myrow = mysql_fetch_array($result);

  printf("First name: %s\n<br>", $myrow["first"]);

  printf("Last name: %s\n<br>", $myrow["last"]);

  printf("Address: %s\n<br>", $myrow["address"]);

  printf("Position: %s\n<br>", $myrow["position"]);

} else {

  // show employee list

  $result = mysql_query("SELECT * FROM employees",$db) or trigger_error("Query: \n<br />MySQL Error: " . mysql_error());

  if ($myrow = mysql_fetch_array($result)) {

    // display list if there are records to display

    do {

      printf("<a      href=\"%s?id=%s\">%s          %s</a><br>\n", $PHP_SELF, $myrow["id"],

$myrow["first"], $myrow["last"]);

  } while ($myrow = mysql_fetch_array($result));

  } else {

    // no records to display

    echo "Sorry, no records were found!";

  }

}

?>

 

 

try this...

<?php
$db = mysql_connect("localhost", "root". "<ommited>");
$db = mysql_select_db("mydb",$db);
// display individual record
if ($id) {
$result = mysql_query("SELECT * FROM employees WHERE id=$id") or die("Query: \n<br />MySQL Error: " . mysql_error()); 
$myrow = mysql_fetch_array($result);
printf("First name: %s\n<br>", $myrow["first"]);
printf("Last name: %s\n<br>", $myrow["last"]);
printf("Address: %s\n<br>", $myrow["address"]);
printf("Position: %s\n<br>", $myrow["position"]);
} else {
// show employee list
$result = mysql_query("SELECT * FROM employees") or die("Query: \n<br />MySQL Error: " . mysql_error());
if (mysql_num_rows($result) > 0) {
// display list if there are records to display
$myrow = mysql_fetch_array($result);
printf("<a href=\"%s?id=%s\">%s%s</a><br>\n", $_SERVER['PHP_SELF'], $myrow["id"],
$myrow["first"], $myrow["last"]);
} else {
// no records to display
echo "Sorry, no records were found!";
}
}
?>

nope..still getting the same errors

 

Notice: Query: 2

MySQL Error: No database selected in /var/www/links2.php on line 16

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/links2.php on line 17

Sorry, no records were found!

yes the DB name is correct..this code..the previous piece in the tutorial is working fine

 

<?php

$db = mysql_connect("localhost", "root", "ommited");

mysql_select_db("mydb",$db);

$result = mysql_query("SELECT * FROM employees",$db);

if ($myrow = mysql_fetch_array($result)) {

  do {

  printf("<a    href=\"%s?id=%s \">%s      %s</a><br>\n", $PHP_SELF, $myrow["id"], $myrow["first"],

          $myrow["last"]);

  } while ($myrow = mysql_fetch_array($result));

} else {

  echo "Sorry, no records were found!";

}

?>

and here is the table format

 

CREATE TABLE employees ( id tinyint(4)  NOT NULL AUTO_INCREMENT,

first varchar(20), last varchar(20), address varchar(255),

position varchar(50), PRIMARY KEY (id), UNIQUE id (id));

 

INSERT INTO employees VALUES (1,'Bob','Smith','128 Here St, Cityname','Marketing Manager');

INSERT INTO employees VALUES (2,'John','Roberts','45 There St , Townville','Telephonist');

INSERT INTO employees VALUES (3,'Brad','Johnson','1/34 Nowhere Blvd, Snowston','Doorman');

 

So, find out why the database was not selected. Change your mysql_select_db  code to the following -

 

mysql_select_db("mydb",$db) or die("Could not select database: " . mysql_error());

 

Edit: There is also a dot . in the following that should be a comma -

 

$db = mysql_connect("localhost", "root". "<ommited>");

Archived

This topic is now archived and is closed to further replies.

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