473,382 Members | 1,225 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Finding the right db connections

nomad
664 Expert 512MB
Hello everyone:
I'm trying to learn PHP and I'm using this book to help me "PHP, MYSQL and Apache all in one" They have an example which is listed below. I'm using Dreamweaver to write the PHP and XAMPP to write the db.
I have set up the db connection with Dreamweaver.
My db is located in the default folder under XAMPP.

I get this error:
Incorrect database name 'storefront_db '


[PHP] <?php
session_start();
//connect to database
$conn = mysql_connect("localhost", "nomad", "nomad") or die(mysql_error());
mysql_select_db("storefront_db ",$conn) or die(mysql_error());
if ( POST[sel_item_id] != "") {
//validate item and get title and price
$get_iteminfo = "select item_title from store_items where id = POST[sel_item_id]";
$get_iteminfo_res = mysql_query($get_iteminfo) or die(mysql_error());
if (mysql_num_rows($get_iteminfo_res) < 1) {
//invalid id, send away
header("Location: seestore.php");
exit;
} else {
//get info
$item_title = mysql_result($get_iteminfo_res,0,'item_title');
//add info to cart table
$addtocart = "insert into store_shoppertrack values ('', '$PHPSESSID', ' POST[sel_item_id]', ' POST[sel_item_qty]', ' POST[sel_item_size]', ' POST[sel_item_color]', now())";
mysql_query($addtocart);
//redirect to showcart page
header("Location: showcart.php");
exit;
}
} else {
//send them somewhere else
header("Location: seestore.php");
exit;
}
?>
[/PHP]

Any help would be great
nomad
Jun 27 '07 #1
9 1582
ronnil
134 Expert 100+
have you setup a database in mysql yet?
if you have, please check for misspelling, if you haven't created a database yet, this is the cause of your problem :)
Jun 27 '07 #2
ronnil
134 Expert 100+
uhhh just noticed you have a space in the database name on line 5

Expand|Select|Wrap|Line Numbers
  1. mysql_select_db("storefront_db ",$conn)  or die(mysql_error());
this is probably why, erase the space so it looks like:

Expand|Select|Wrap|Line Numbers
  1. mysql_select_db("storefront_db",$conn)  or die(mysql_error());
Jun 27 '07 #3
nomad
664 Expert 512MB
uhhh just noticed you have a space in the database name on line 5

Expand|Select|Wrap|Line Numbers
  1. mysql_select_db("storefront_db ",$conn) or die(mysql_error());
this is probably why, erase the space so it looks like:

Expand|Select|Wrap|Line Numbers
  1. mysql_select_db("storefront_db",$conn) or die(mysql_error());
The correction you suggested did not work. I get this error now
Object not found!

The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

If you think this is a server error, please contact the webmaster

Error 404
localhost

06/27/07 08:59:52
Apache/2.2.4 (Win32) DAV/2 mod_ssl/2.2.4 OpenSSL/0.9.8e mod_autoindex_color PHP/5.2.1

You're other question about the db being made. Yes it is created which is called storefront_db.Do I have to call up the directory to get to the storefront_db?
Nomad
Jun 27 '07 #4
ronnil
134 Expert 100+
The correction you suggested did not work. I get this error now
Object not found!

The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

If you think this is a server error, please contact the webmaster

Error 404
localhost

06/27/07 08:59:52
Apache/2.2.4 (Win32) DAV/2 mod_ssl/2.2.4 OpenSSL/0.9.8e mod_autoindex_color PHP/5.2.1

You're other question about the db being made. Yes it is created which is called storefront_db.Do I have to call up the directory to get to the storefront_db?
Nomad
This means it's working :) cause you're being redirected to another page, which probably doesn't exist yet :)

(a 404 error means exactly that the document you are trying to see cannot be found on the server, it has nothing to do with database connectivity)

try commenting out your header('Location .....); statements and make it echo something like 'redirect to thispage', 'redirect to thatpage' instead. So you can see where it's going :)
Jun 27 '07 #5
nomad
664 Expert 512MB
This means it's working :) cause you're being redirected to another page, which probably doesn't exist yet :)

(a 404 error means exactly that the document you are trying to see cannot be found on the server, it has nothing to do with database connectivity)

try commenting out your header('Location .....); statements and make it echo something like 'redirect to thispage', 'redirect to thatpage' instead. So you can see where it's going :)
Thanks for the help
I found out that one of my php files was in the wrong directory.
I have another problem when I click a link on the main page it get this error
Unknown column 'id' in 'field list'
Here is the code

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. //connect to database
  4. $conn = mysql_connect("localhost", "nomad", "nomad") or die(mysql_error());
  5. mysql_select_db("storefront_db",$conn) or die(mysql_error());
  6. $display_block = "<h1>My Categories</h1>
  7. <P>Select a category to see its items.</p>";
  8. //show categories first
  9. $get_cats = "select id, cat_title, cat_desc from store_categories order by cat_title";
  10. $get_cats_res = mysql_query($get_cats) or die(mysql_error());
  11. if (mysql_num_rows($get_cats_res) < 1) {
  12. $display_block = "<P><em>Sorry, no categories to browse.</em></p>";
  13. } else {
  14. while ($cats = mysql_fetch_array($get_cats_res)) {
  15. $cat_id = $cats[id];
  16. $cat_title = strtoupper(stripslashes($cats[cat_title]));
  17. $cat_desc = stripslashes($cats[cat_desc]);
  18. $display_block .= "<p><strong><a href=\" [PHP]%4$s[/PHP] SERVER[PHP_SELF]?cat_id=$cat_id\">$cat_title</a></strong><br>$cat_desc</p>";
  19. if ( [PHP]%4$s[/PHP] GET[cat_id] == $cat_id) {
  20. //get items
  21. $get_items = "select id, item_title, item_price from store_items where cat_id = $cat_id order by item_title";
  22. $get_items_res = mysql_query($get_items) or die(mysql_error());
  23. if (mysql_num_rows($get_items_res) < 1) {
  24. $display_block = "<P><em>Sorry, no items in this category.</em></p>";
  25. } else {
  26. $display_block .= "<ul>";
  27. while ($items = mysql_fetch_array($get_items_res)) {
  28. $item_id = $items[id];
  29. $item_title = stripslashes($items[item_title]);
  30. $item_price = $items[item_price];
  31. $display_block .= "<li><a href=\"showitem.php?item_id=$item_id\">$item_title</a></strong> (\$item_price)";
  32. }
  33. $display_block .= "</ul>";
  34. }
  35. }
  36. }
  37. }
  38. ?>
  39. <HTML>
  40. <HEAD>
  41. <TITLE>My Categories</TITLE>
  42. </HEAD>
  43. <BODY>
  44. <?php echo $display_block; ?>
  45. </BODY>
  46. </HTML>
  47.  
I looked at my fields in my db and them seemed to be same as my php.
code.

nomad
Jun 27 '07 #6
ronnil
134 Expert 100+
this is from your sql query

$get_cats = "select id, cat_title, cat_desc from store_categories order by cat_title";

again make sure that no spaces have sneaked in to your code, this time it might be in the database setup.

It's pretty dangerous to use just "id", i would prefer calling it cat_id. with multiple tables it can be confusing to have a column named id in each table, if it's not foreign keys.

you could try using backqoutes in the statement making it look like

$get_cats = "select `id`, `cat_title`, `cat_desc` from `store_categories` order by `cat_title`";

but this probably won't help, check out the database and see if you haven't accidentally sneaked a space before or after id
Jun 27 '07 #7
nomad
664 Expert 512MB
this is from your sql query

$get_cats = "select id, cat_title, cat_desc from store_categories order by cat_title";

again make sure that no spaces have sneaked in to your code, this time it might be in the database setup.

It's pretty dangerous to use just "id", i would prefer calling it cat_id. with multiple tables it can be confusing to have a column named id in each table, if it's not foreign keys.

you could try using backqoutes in the statement making it look like

$get_cats = "select `id`, `cat_title`, `cat_desc` from `store_categories` order by `cat_title`";

but this probably won't help, check out the database and see if you haven't accidentally sneaked a space before or after id
Thanks for the all help.
and I will try to rewrite the code, this is an exercise out of the book.
The id are PK with auto_increments with not null.

nomad
Jun 27 '07 #8
nomad
664 Expert 512MB
this is from your sql query

$get_cats = "select id, cat_title, cat_desc from store_categories order by cat_title";

again make sure that no spaces have sneaked in to your code, this time it might be in the database setup.

It's pretty dangerous to use just "id", i would prefer calling it cat_id. with multiple tables it can be confusing to have a column named id in each table, if it's not foreign keys.

you could try using backqoutes in the statement making it look like

$get_cats = "select `id`, `cat_title`, `cat_desc` from `store_categories` order by `cat_title`";

but this probably won't help, check out the database and see if you haven't accidentally sneaked a space before or after id
well I'm still getting the same error code
Unknown column 'id' in 'field list'
I think it has to deal with these line of code
while ($cats = mysql_fetch_array($get_cats_res)) {
$cat_id = $cats[id];
the next lines below that seem to work.

I even tried draging the fields into dreamweaver so I know I got the right fields names. It might be the id field itself because there are least 4 tables that use the same name.

nomad
Jun 27 '07 #9
nomad
664 Expert 512MB
well I'm still getting the same error code
Unknown column 'id' in 'field list'
I think it has to deal with these line of code
while ($cats = mysql_fetch_array($get_cats_res)) {
$cat_id = $cats[id];
the next lines below that seem to work.

I even tried draging the fields into dreamweaver so I know I got the right fields names. It might be the id field itself because there are least 4 tables that use the same name.

nomad
found the problem. Boy do I feel dumb
I had a field called int when it should have been id.
Thanks for the tip ronnil.
nomad
Jun 28 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mudge | last post by:
Hi, My hosting provider only allows me to use 50 connections to my MySQL database that my Web site will use. I don't know what this 50 connections means exactly. Does this mean that only 50...
1
by: Bob | last post by:
After adding a Data Connection everything appears to be fine but when I right-click on Tables to add a table the option is not there, only refresh & properties are showing. I can see and review...
3
by: KL | last post by:
Well, I am back. This time our assignment has us filling a vector and then timing how long it takes to find a spot in the vector to insert a new item, and the time required to insert the item...
4
by: Angelos | last post by:
I get this error mysql_pconnect Too many connections ... every now and then. Does anyone knows where it comes from ? There are a lot of sites running on the server and all of them use the...
2
by: Lior | last post by:
Hi, I have an ASP.NET website that crashes under heavy load. I use a SQL Server DB. I get around 5500 hits per day. I keep getting the timeout expieried connection pool error. Sometimes it even...
9
by: dave m | last post by:
I need to be able to retrieve a unique ID from a users PC. I needs to be something a user could not easily change, like the computer name. Could someone point me in the right direction to find ...
5
by: Paul | last post by:
I am trying to create a log of certain events on the website by writing them to a text file. The logging code is in an "included" file that is inserted into the pages that need to be monitored....
1
by: t.mitchell | last post by:
Hi, I have a python gtk app that allows users to have one project open at a time. I have recently discovered that projects are not being freed when they are closed - the refcount is not hitting...
0
by: U S Contractors Offering Service A Non-profit | last post by:
This Sunday the 26th 2006 there will be Music @ Tue Nov Inbox Reply Craig Somerford to me show details 9:54 pm (26 minutes ago) #1St "CLICK" HeAt frOm A blanket...
5
by: Usman Jamil | last post by:
Hi I've a class that creates a connection to a database, gets and loop on a dataset given a query and then close the connection. When I use netstat viewer to see if there is any connection open...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.