473,395 Members | 1,401 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,395 software developers and data experts.

How to make my php/msql calls more efficient?

1
Hi, I'm pretty new to the php/mysql world and am building an article database for a website with multiple content types. I have an entry screen built that allows the site owner to enter articles, set article types, and set articles as features. Each page in the site calls the database for a feature article for that pages content type, then calls again for the 5 newest articles of that content type excluding any article that is identified as a feature.

I have this mostly working but it is just kind of hacked together. My question is how do I make this more efficient? Code is below

The Submit screen:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include(login file);
  3. $link = mysql_connect($hostname, $user, $password);
  4. mysql_set_charset("joshai_article",$link);
  5. $db_selected = mysql_select_db('joshai_article', $link);
  6. if (!$db_selected) { die ('Database access error : ' . mysql_error());}
  7. // Validate this user
  8. $test_username = $_POST['test_username'];
  9. $query = "SELECT password
  10.           FROM login
  11.           WHERE username = '$test_username'";
  12. $result = mysql_query($query);
  13. if (mysql_num_rows($result) != 1) {
  14.   echo "Something is wrong";
  15.   exit;
  16. }
  17. $password_row = mysql_fetch_array($result);
  18. $db_password = $password_row[0];
  19.  
  20. if ($_POST['test_password'] == $db_password &&
  21. $_POST['test_password'] != "") {
  22.   if ($_POST['Submit'] == 'Enter') {
  23.     // Enter new entry
  24.     $date = date('Ymd'); // Remember, date is an integer type
  25.     $article = $_POST['article'];
  26.     $author = $_POST['author'];
  27.     $byline = $_POST['byline'];
  28.     $title1 = $_POST['title'];
  29.     $title = addslashes($title1);
  30.     $feature = ($_POST['chkfeature'] == 1) ? "1" : "0";
  31.     switch($_POST['type']){
  32.             case '1':
  33.                $type = '1';
  34.                break;
  35.             case '2':
  36.                $type = '2';
  37.                break;
  38.             case '3':
  39.                $type = '3';
  40.                break;
  41.             case '4';
  42.             $type = '4';
  43.             break;
  44.             case '5';
  45.             $type = '5';
  46.             break;
  47.             default:
  48.                $type = '0';
  49.          }
  50.  
  51.     $query = "INSERT INTO content (ID, date, title, author, article, byline, approved, feature)
  52. VALUES(null, $date, '$title', '$author', '$article', '$byline','1', '$feature')";
  53. mysql_query($query) OR die(mysql_error());
  54. $article_ID = mysql_insert_id();    
  55. $query = "INSERT INTO content_type (ID, type_ID, article_ID) VALUES (NULL,$type,$article_ID)";
  56.  
  57.     $result = mysql_query($query);
  58.  
  59.     if (mysql_affected_rows() == 1) {
  60.       header("Location: db_login.php");
  61.     } else {
  62.       echo "There was a problem inserting your text.";
  63.       exit;
  64.     }
  65.   } else {
  66.     // Show the form
  67.     $php_self = $_SERVER['PHP_SELF'];
  68.     $test_password = $_POST['test_password'];
  69. $form_str = <<< EOFORMSTR
  70.  
  71. <HTML>
  72. webform goes here</html>
  73. ?>
The code for displaying content in content div of page

Expand|Select|Wrap|Line Numbers
  1. <?  // Open database connection
  2. include("login file");
  3. mysql_connect($hostname, $user, $password);
  4. mysql_select_db("joshai_article");
  5.  
  6. // Identify the featured entry for this section
  7. $entropia= "SELECT content.ID, content.title, content.author, content.byline, content.article FROM content, content_type, subject
  8. WHERE content.ID = content_type.article_ID
  9. AND feature=1
  10. AND content_type.type_ID = subject.type_ID
  11. AND subject.type_ID = 5
  12. AND content.approved = '1'";
  13. // Print the feature article at top of content div
  14. $result1= mysql_query($entropia)
  15. or die("dude you screwed up");
  16. while (list($link, $title, $author, $byline, $article1)=
  17. mysql_fetch_array($result1)){
  18. $article=stripslashes($article1);
  19.  
  20. echo "<h2>$title </h2>";
  21. echo "<h4>$author </h4>";
  22. echo "$byline <br>";
  23. echo nl2br("$article");
  24.  
  25.  //echo "<a href=\"view.php?id=". $link."\">View</a>";
  26. echo "<br><hr />";
  27.  
  28. // Identify the latest 5 articles for this page type while excluding any featured article
  29. $query = "SELECT content.ID, content.title, content.author, content.byline, content.article FROM content, content_type, subject WHERE content.ID = content_type.article_ID
  30. AND content_type.type_ID = subject.type_ID
  31. AND subject.type_ID = 5
  32. AND content.approved = '1' AND content.feature != '1' ORDER BY content.article_ID DESC LIMIT 5
  33.  
  34. ";
  35. $result = mysql_query($query)
  36. or die("something is wrong");
  37.  
  38. // print the latest 5 articles of this content type to the content div
  39.  
  40. while (list($link, $title, $author, $byline, $article1)=
  41. mysql_fetch_array($result)){
  42. $article=stripslashes($article1);
  43.  
  44. echo "<h2>$title </h2>";
  45. echo "<h4>$author </h4>";
  46. echo "$byline <br>";
  47. echo nl2br("$article");
  48.  
  49.  //echo "<a href=\"view.php?id=". $link."\">View</a>";
  50. echo "<br><hr />";
  51. ?>
I am trying to set up a code to grab an id from the page url to insert into the subject_type.ID field so I can just use an include to put this script in every page without having to manually go in and set the ID number for every new content category.

Any suggestions would really help this fish out of water.
Jan 3 '09 #1
1 2469
dlite922
1,584 Expert 1GB
@joshai
Your script is barely acceptable and could use a good organization and clean up. Consider classes or at least a database class.

what exactly do you need to grab an ID? You want a script that grabs a value from the URL parameter and puts it into a common variable? so you can include this in your scripts and access that variable without repeating the script? If this is the case, you need to meeting $_GET[], $_POSTS[] younger sister.

Let me know,



Dna
Jan 6 '09 #2

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

Similar topics

2
by: Glenn | last post by:
Hi, I'm using cygwin and am trying to install DBI-mSQL and am getting errors on make .. any idea how to fix this (TIA): cpan> install DBD::mSQL Running install for module DBD::mSQL Running...
0
by: Gill Bates | last post by:
so DOM calls should be calculated from Diff. Possible? How? (some URLs please) I guess this is in most cases not very efficient, but here's why: I have a data Document at the server. From...
3
by: Chris Tanger | last post by:
I am creating a class that has a method "Write" that I wish to make threadsafe. The method must block calling threads until the task performed in write is complete. Only 1 thread at a time can...
6
by: Chapman Flack | last post by:
Ok, I've registered all the right headers to send, with header( 'Content-Type: .../...') etc. The next thing to do is exec a program that will be producing the actual content. The content MUST...
0
by: MatchSQL | last post by:
When we want to save the database schema and SQL statement , we use the backup of SQLServer provide, but it always content the running data, now a new tool named MatchSQL, it can just save...
0
by: yogesh | last post by:
if i run my make file i am getting all the lines error .... can any one tell abt this my make file consist of the detais # cat Makefile_adm PRJ=adm.cgi _INSTALLDIR=../bin INSTALLUSER=root...
1
by: ABITECH | last post by:
hi guys I am working on a project in databases using the wamp software. So how do i connect msql to php. Pliz help mi Because i badly nid the code for my project. bye
12
by: Atropo | last post by:
Hi all. Having several strings how do i combine them to construct a command; lets say to run the date command. string str = "14/10/08 19:06:09"; strDD = str.substr(0,2); strMM =...
2
by: chazzy69 | last post by:
Ok this may seem a little strange but what i Im trying to add an altered version of information already stored in my database. Basically i have a colum that has strings of numbers and characters,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.