Jump to content

mysql_query() can't find an existing table


juliejohnson26

Recommended Posts

First, I'm using MAMP with MySQL client version: 5.5.9.

 

I am very new to php/mysql and have been following along with Kevin Skoglund's tutorial on lynda.com which is very good.  I have successfully created a small database in mysql with three tables, one of which is called pages. In my php file, I have the following loop which is directly from the tutorial:

 

while ($subject = mysql_fetch_array($subject_set)) {

echo "<li>{$subject["menu_name"]}</li>";

$page_set = mysql_query("SELECT * FROM pages WHERE subject_id = {$subject["id"]}", $connection);

if (!$page_set) {

die("Database query failed here: " . mysql_error());

}

 

I am consistently breaking out of the loop after the first echo of the first $subject, with the mysql_error "table 'widget_corp.pages' doesn't exist" (widget_corp being the name of the database).

 

I've attached a screenshot of the pages table structure and browse views in mysql. I'd appreciate any advice on what's going wrong.

 

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

Are you signing into the database?

 

// Connect to mysql here or die
$link = mysql_connect($DBhost,$username,$password) or die("Unable to connect to host");

// Connect to the database you want here or die
$link = mysql_select_db($database) or die( "Unable to connect database");

Link to comment
Share on other sites

Yes, and I know it's working because  a previous call to  the database doesn't generate any errors:

 

	$subject_set = mysql_query("SELECT * FROM subjects", $connection);
			if (!$subject_set) {
				die("Database query failed: " . mysql_error());
			}

 

Are you signing into the database?

Link to comment
Share on other sites

I don't see anything that is obviously wrong.

 

First thing I would try is restarting mysql and apache. 

 

If that does not solve your problem, I would question the user you are using in the script and its permissions.  How were permissions granted and can it see the pages table.

Link to comment
Share on other sites

I only have the root user which is a superuser. When I log into the database as root through phpmyadmin, I see the pages table just fine (image attached above).

 

If that does not solve your problem, I would question the user you are using in the script and its permissions.  How were permissions granted and can it see the pages table.

Link to comment
Share on other sites

I'm really grasping at straws here, as I don't see anything wrong with any of the code you have, but just to rule it out try this in your content.php

 




</pre>
<table id="structure">




			$subject_set = mysql_query("SELECT * FROM subjects", $connection);
			if (!$subject_set) {
				die("Database query failed: " . mysql_error());
			}


while ($subject = mysql_fetch_array($subject_set)) {
	// Connect to mysql here or die
	echo "{$subject["menu_name"]}";
	$page_set = mysql_query("SELECT * FROM `pages` WHERE subject_id = {$subject["id"]}", $connection);
		if (!$page_set) {
			die("Database query failed here: " . mysql_error());
		}

	echo "";
	while ($page = mysql_fetch_array($page_set)) {
		echo "{$page["menu_name"]}";
	}
	echo "";
}


			?>




Content Area



</table>
<br>?&g

 

 

Link to comment
Share on other sites

your code is wrong... (content.php)

 

	while ($subject = mysql_fetch_array($subject_set)) {
	// Connect to mysql here or die
	$link = mysql_connect("localhost","root","OtlPHP07") or die("Unable to connect to host");
	echo "$link";
	echo "<li>{$subject["menu_name"]}</li>";
	$page_set = mysql_query("SELECT * FROM pages WHERE subject_id = {$subject["id"]}", $connection);
		if (!$page_set) {
			die("Database query failed here: " . mysql_error());

 

you are creating a new connection inside of the loop (OUTCH!!!) and trying to execute a query over a connection ($connection) that not longer exist, hence the error... assuming that the posted code is the one that was failing when you started the thread

Link to comment
Share on other sites

mikosiko,

  That's not the problem.  That code was put in after people suggested that as debugging, but it is neither here nor there, as there is no problem making multiple mysql connections.  In the case of that code, it makes a 2nd mysql connection assigning the resource variable to $link, but she never uses it for anything.  $connection is setup earlier, and a single connection can be used for many mysql queries.  None of this should be a problem.

Link to comment
Share on other sites

Oops...sent a test file that I created while I've been grasping at straws. The following two lines were NOT included in the code that started this thread.

 

 

	// Connect to mysql here or die
	$link = mysql_connect("localhost","root","OtlPHP07") or die("Unable to connect to host");
	echo "$link";

Link to comment
Share on other sites

mikosiko,

  That's not the problem.  That code was put in after people suggested that as debugging, but it is neither here nor there, as there is no problem making multiple mysql connections.  In the case of that code, it makes a 2nd mysql connection assigning the resource variable to $link, but she never uses it for anything.  $connection is setup earlier, and a single connection can be used for many mysql queries.  None of this should be a problem.

 

That is why I put the comment "assuming... etct..etc"... and "as there is no problem making multiple mysql connections"... is true only if you are not reaching the Max_connection limit defined for your installation

Link to comment
Share on other sites

mikosiko,

  That's not the problem.  That code was put in after people suggested that as debugging, but it is neither here nor there, as there is no problem making multiple mysql connections.  In the case of that code, it makes a 2nd mysql connection assigning the resource variable to $link, but she never uses it for anything.  $connection is setup earlier, and a single connection can be used for many mysql queries.  None of this should be a problem.

 

That is why I put the comment "assuming... etct..etc"... and "as there is no problem making multiple mysql connections"... is true only if you are not reaching the Max_connection limit defined for your installation

 

Again, this was not the problem, as she described in her first message that she received an error saying it couldn't find the table.

Link to comment
Share on other sites

gizmola...you are not reading/analyzing my answers are you?  .... but is ok... doesn't matter.... PBAb could be right.

 

Mikosiko,

  I did read your answers.  You simply didn't read the original question carefully enough and suggested fixes for problems that were irrelevant to the issue.  I have done the same countless times.  With as many experienced developers as we have at phpf, you will inevitably come up with the wrong answer from time to time.  I hope you won't take it personally because your post introduced red herrings that were not helpful, and I stated as much.  The goal here is to help the OP, and it doesn't help them to send them on a wild goose chase.

Link to comment
Share on other sites

Tried this...still nothing...sigh...

 

I'm really grasping at straws here, as I don't see anything wrong with any of the code you have, but just to rule it out try this in your content.php

 

<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header.php"); ?>
	<table id="structure">
	<tr>
		<td id="navigation">
			<ul class="subjects">
			<?php

			$subject_set = mysql_query("SELECT * FROM subjects", $connection);
			if (!$subject_set) {
				die("Database query failed: " . mysql_error());
			}


while ($subject = mysql_fetch_array($subject_set)) {
	// Connect to mysql here or die
	echo "<li>{$subject["menu_name"]}</li>";
	$page_set = mysql_query("SELECT * FROM `pages` WHERE subject_id = {$subject["id"]}", $connection);
		if (!$page_set) {
			die("Database query failed here: " . mysql_error());
		}

	echo "<ul class=\"pages\">";
	while ($page = mysql_fetch_array($page_set)) {
		echo "<li>{$page["menu_name"]}</li>";
	}
	echo "</ul>";
}


			?>

		</td>
		</ul>
		<td id="page">
			<h2>Content Area</h2>

		</td>
	</tr>
</table>
<?php require("includes/footer.php");
?>

Link to comment
Share on other sites

I don't know that it will make any difference, but you could try a cut down test page:

$user= "root";
$pass= "OtlPHP07";
$host= "localhost";
$db= "widget_corp";

$con = mysql_connect($host, $user, $pass) or die ("Can't Connect : ".mysql_error());
$cur_db = mysql_select_db($db, $con) or die ("Unable to open Database '$db' : ".mysql_error());

$qry = "SELECT menu_name, content FROM pages ORDER BY id DESC";
$return = mysql_query($qry) or die (Data retetreval failed running : $qry <br><br> with the following error :<br>".mysql_error());
WHILE ($row = mysql_fetch_assoc($return)){
echo "<p>{$row['menu_name']} ---- {$row['content']} </p>"
}

 

Cutting it down to the bare minimum might help find the problem - or not  :shrug:

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.