Ask PHP/MySQL/Javascript/Web questions!!!

Are you looking for PHP,MySQL,Javascript,Web related answers?

Have you faced difficult questions in your interview?

Are you planning for certification exams and you have questions unanswered?

do you want to correct your reasoning?

Then add questions to this post and you can add as many questions as you want. Every week I will answer or compile the answer for the questions. So lets make the question bank a bigger one.

33 thoughts on “Ask PHP/MySQL/Javascript/Web questions!!!

  1. Maleek says:

    hey Mizanur, my name is Maleek and i am a computer science student from the UK. I am currently doing a final year project that requires me to create a web-based front end for MySQL that application to automatically generate web interfaces to add/update/delete/display fields in the tables within a mySQL database.

    Now, i intend to interview my project proposer pretty soon but i am not sure what kind of questions to ask him to flesh out good answers so i can do a good requirements spec and design for the system, giving me a better understanding of how it should all work when i am actually programming.

    I have a basic understanding on the logic of what i have been asked to do. I have started coding and managed to get some code for a user registration and login facility and this works fine so far. I am using WAMP Apache, PHP and MySQL all on my windows laptop so everything including my webserver is all on my localhost.

    I would really appreciate your help, as i managed to come across your blog by chance, which i am finding quite interesting :-).

    Look forward to hearing from you

    MALEEK

  2. booleandreams says:

    Hi Maleek

    As far my understanding, you are going to build a code generator based on MySQL table information. There are few nice open source projects available in the internet. I have used one named “Code Crafter” for CodeIgniter framework. There are few things that you can consider and ask questions to your project proposer. First of all, are you going to use any design pattern for the project such as MVC, Active Record, Factory etc. Also whether you are going to give any option where the generated files will be in user defined templates instead of plain HTML. You have to think about the security features as well, like the SQL Injection and XSS filter etc. Make something which you can release it as open source project and we can get benefit out of it.

    You can ping me anytime you want. 🙂 best of luck

  3. bondy says:

    hello bro mizan…
    lost here because im looking for how to swap value(s) from 2 rows in SQL (mysql). well in application mode i think it’s common for me

    can u give some clues how to do this in sql?
    thx

    bondy
    bali indonesia

  4. Maleek says:

    Hi Mizan.

    I had an interview with my project propeser and we came to the conclusion, that, because of time,it would be best for me to develop the application from a standalone point of view. i.d the user will only be using it for his/her personal so therefore i wont have to worry about any security features because it will not be going online per say, but he also mentioned that it would be good for my writeup that i list all of the things the user would have to consider if they were to put it online. i.e, like u said, SQL injections and. There will be no need for login or registration anymore.

    Basically all i have to do now is build a system that can(this is my list of requirements below) :

    • The user should be able to view a list of databases that they own and select the database they wish to use
    • The user should be able to create a table within a database that they own
    • The user should be able to view an existing table within a database that they own along with all of the information in that table
    • The user should be able to edit table details within a database
    • The user should be able to delete an existing table within a database that they own
    • The user should be able to insert a column within a table in the database
    • The user should be able to edit details of an existing column within a database
    • The user should be able to delete an existing column within a table in the database
    • The user should be able to insert a row into a table in the database
    • The user should be able to edit details of an existing row within a database
    • The user should be able to delete an existing row from a table within the database

    The first requirement, which is the viewing of the database, i have already coded it. At the moment the user can dynamically view the list of databases that he/she owns in a drop down list. I coded this in the PHP itself and didnt have any html forms. Now i am stuck. I am not worrying about any of the fancy stuff for now, i just want to build a working prototype that hits all of those basic requirements. Basically as soon as you enter the app, the dropdown list is displayed and it shows the list of the databases. What i want to do is have a button so the user can select the database they choose to work with and then the details of that database is displayed, i.e tables within the database…..

    It would be great help if i can know how to do this, because it will make everything else much easier. Also with regards to the MVC, i am not entirely sure how to implement it with the system i am using. Any pointers?

    thanks

    Maleek

    🙂

  5. mizan says:

    Hi Maleek

    Can you have a look at the application called codecrafter, its a type of application which you are planning to build. it will give you idea i believe. this application is for CodeIgniter framework, which is a MVC based framework for PHP.

    thanks
    Mizan

  6. Asif Ahmed says:

    Hello Mizan bhai,

    My name is Asif, and I am a computer engineering student in Ahsanullah University of Science & Technology, Dhaka. I am having a problem. I have uploaded a website that has been developed in PHP, MySQL. The problem is when I click to the menu item “Contact Us”, then it goes to ” /contact.php”, but when I click another menu item “Our Team”, it does not go to “/team.php”. Rather it shows the whole list of the files and directory (the directory containing the images, db file etc). I don’t have he idea what to do. Can you please help me out of this problem ?

    Thanks in advance

    Asif

    • booleandreams says:

      hi Asif

      first check if you have typed the Link incorrectly. also put a simple index.html page in each directory to stop directory browsing. you can use .htaccess file for that purpose as well.

  7. ansar says:

    hiiii,
    i have a question . is it better to take values directly from database to plot a live chat(updating every 5 seconds) or to access it via a csv file. the csv file contains data in the database.

    which is better to have a reduced server load.assume the database to have 50 tables each 3 columns wide.

    please reply soon …
    if possible mail me..

    thanks in advance

    • booleandreams says:

      hi Ansar

      since you need to update the chat in every 5 seconds, dont go for querying the database. you can store the information there, but for quick retrieval go for memcache or APC (alternate php cache).

      thanks
      Mizan

  8. jeffwithag says:

    Hi. First, I apologize, I’m extremely new to php. It seems relatively straight forward, but I am unable to find how to address an issue. This may not be the correct location to ask, but I thought it couldn’t hurt.

    Instead of trying to break it all down, let me just explain my scenario. I have 2 accounts at godaddy.com

    Acc 1 is an older website I’ve created with asp and microsoft access.

    Acc 2 is a brand new website I’m creating with wordpress.org.

    I have the new website up and running, everything seems fine. Now I want to add a page to this wordpress.org website that is dynamically populated via my access database hosted on my other website. In other words, I want to connect to the mdb file on other site and extract it’s data directly unto a wordpress.org page in my new website.

    Can I do this on a wordpress.org page via the php or some asp loophole or something? If not, is this a wordpress limitation, or a remote database limitation or php vs asp limitation?

    Any suggestions?

    Thank you,
    Geoff

  9. Dennis says:

    i have this code that i came across on the net. it should
    update my facebook status without i having to login to
    facebook.com. i have put in the correct variables but the
    code still doesnt work.
    ‘.ucfirst($first_name).’/’, $page, $form_id);
    curl_setopt($ch,
    CURLOPT_POSTFIELDS,’post_form_id=’.$form_id[1].’&status=’.u
    rlencode($status).’&update=Update’);
    curl_setopt($ch, CURLOPT_URL,
    ‘http://m.facebook.com/home.php’);
    curl_exec($ch);
    ?>

    i am on Windows 7 and am using a wamp server.
    Please help out.
    Thanks a lot

  10. John Galloway says:

    Ok, here’s a design problem or question: I want to present a web page that contains a few buttons and some data. When a button is clicked I need some data to be passed to a script executed on TWO (or more) servers. It need not (could not) be perfectly concurrently and even one right after the other is fine. One such server can be (likely is) hosting the page in question (but that is not required). The browser is the other way around and could of course be running on one of the servers but very likely on a client windows system (presently via Fusion on my Mac). The script execution need not return a page (i.e. that is not its purpose) and in fact it is desirable that the button click but that its page does not reload visibly. There will be a little data from each server displayed (a count or two say) that needs to be updated at least 1/sec, or after any button is clicked, so whatever is easier. All on a private network in a very controlled env with full access to all the systems involved.

    So how does one button execute scripts on two servers? The easier and quicker to implement the better. The starting point is pretty much just a couple of linux/apache boxes and a windows client. The windows client is also using (a trial of) PowerGadgets from SoftwareFX to pull back and display some performance data from each server (and the web page is what alters the load. Sound like a setup for a demo? You guessed it!).

  11. John Galloway says:

    Oops, that got away from me before I added. In principle this seems easy enough, at worst I can have a script on the web server be the one that fires up scrips on the target servers. But is there a common setup for such things, some sort of redirection or … Looking at things like Adobe GoLive CS2 (which I barely know how to use anymore) I don’t see any obvious way to directly hook a button up too multiple server methods or scripts etc.

  12. adam lee riley says:

    Hi is it posible in javascript to show objects after a time has gone by for example if i wanted to show lyrics to a song at a time of a song and then stay on the screen. Any examples would be appreciated

  13. Heath says:

    Hey Mizan.

    My name is Heath, and I’m working on a personal php/mysql project. I’m hoping you can help me out.

    I have a search right now on my site (http://dev.mybrunchi.com , username: dev password: brunchi12) and I’m trying to get multiple search selections working for the multiple item selection boxes.

    This is one of the html forms:

    all neighborhoods
    east village/les
    midtown east
    midtown south/chelsea
    midtown west/hell’s kitchen
    murray hill/gramercy
    soho/trbca/findist
    upper east side
    upper west side
    uptown
    village/w village/noho

    and this is the search.php code:
    if(isset($_POST[s1_x]))
    {
    if(!empty ($_POST[CategoryID]))
    {
    $c = $_POST[CategoryID];
    }

    if(!empty($_POST[SubCategoryID]))
    {
    $s = $_POST[SubCategoryID];
    }
    if(!empty($_POST[search_SubCategoryID]))
    {
    $search_SubCategoryID = $_POST[search_SubCategoryID];
    }
    if(!empty($_POST[search_priceid]))
    {
    $search_priceid = $_POST[search_priceid];
    }

    if(!empty($_POST[search_prixfixeid]))
    {
    $search_prixfixeid = $_POST[search_prixfixeid];
    }

    if(!empty($_POST[search_drinksid]))
    {
    $search_drinksid = $_POST[search_drinksid];
    }

    if(!empty($_POST[search_timeid]))
    {
    $search_timeid = $_POST[search_timeid];
    }

    if(!empty($_POST[search_zipcode]))
    {
    $search_zipcode = $_POST[search_zipcode];
    }

    if(!empty($_POST[search_atmoid]))
    {
    $search_atmoid = $_POST[search_atmoid];
    }

    $my_kw = trim($_POST[kw]);

    if(!empty($my_kw))
    {
    $search_kw = $my_kw;
    }

    $url = “search_results.php?c=$c&s=$s&search_priceid=$search_priceid&search_SubCategoryID=$search_SubCategoryID&search_prixfixeid=$search_prixfixeid&search_drinksid=$search_drinksid&search_timeid=$search_timeid&search_zipcode=$search_zipcode&search_atmoid=$search_atmoid&search_kw=$search_kw”;

    header(“location:$url”);
    exit();
    }

    How can I get the multiple selections working in the search?

    Thanks so much for any help!

    Heath

    • booleandreams says:

      Hi Heath

      Multiple selections are send to server as an Array. now you can do it many ways you want.

      1. you can send one value for the particular post variable by concatenating the array values (using implode function with comma as the glue) to represent a single string. then you can do the reverse in your search script (explode the URL parameter and get all values). so from your example it will be

      $c = implode(“,”, $_POST[CategoryID]);

      2. you can contract the URL as multiple selected value. then your url will look like

      $url = “search_results.php?c[]=xxxx&c[]=yyyy”; (like this)

      you can check if the posted value is an array or not and run a foreach look to construct this URL.

      i hope that solves your problem

      have a good day. also let me know if you still have any problem understanding it.

      thanks
      Mizan

  14. Sharif Md. Wazedul says:

    Problem1:Example #3 Non-obvious Ternary Behaviour

    How can this code’s result would be ‘tt’? I can’t understand?

    Problem 2:example of object comparison: can’t understand this code?
    flag = $flag;
    }
    }

    class OtherFlag
    {
    public $flag;

    function OtherFlag($flag = true) {
    $this->flag = $flag;
    }
    }

    $o = new Flag();
    $p = new Flag();
    $q = $o;
    $r = new OtherFlag();

    echo “Two instances of the same classn”;
    compareObjects($o, $p);

    echo “nTwo references to the same instancen”;
    compareObjects($o, $q);

    echo “nInstances of two different classesn”;
    compareObjects($o, $r);
    ?>
    The above example will output:
    Two instances of the same class
    o1 == o2 : TRUE
    o1 != o2 : FALSE
    o1 === o2 : FALSE
    o1 !== o2 : TRUE

    Two references to the same instance
    o1 == o2 : TRUE
    o1 != o2 : FALSE
    o1 === o2 : TRUE
    o1 !== o2 : FALSE

    Instances of two different classes
    o1 == o2 : FALSE
    o1 != o2 : TRUE
    o1 === o2 : FALSE
    o1 !== o2 : TRUE

    Problem3:questions from logical operators:

    questions from type operators:
    I can’t understand following code?

    Problem 4:Example #4 Using instanceof for class

    The above example will output:
    bool(true)
    bool(true)

    Problem 5:User-defined Functions: conditional functions

    from this code question is : but how can we call bar() from here?

    Problem5:From function arguments:
    Passing function parameters by reference

    from this above code how can we get output from Function ‘add_ some_extra’ when we declare and print variable ‘$str’ outside of function ‘add_ some_extra’?

    Problem 6:Using non-scalar types as default values

    from the above code “return “Making a cup of “.join(“, “, $types).” with $device.n”; and echo makecoffee(array(“cappuccino”, “lavazza”), “teapot”);” this two statements are totally unapprehensible and unclear.

    Problem7:From this following code we cannot get any output??? Why?

    Problem 8:Static keyword: static member example

    staticValue() . “n”;
    print $foo->my_static . “n”; // Undefined “Property” my_static

    print $foo::$my_static . “n”;
    //A member declared as static can not be accessed with an instantiated class object. How can this $my_static can be accessed under foo class?

    $classname = ‘Foo’;
    print $classname::$my_static . “n”; // As of PHP 5.3.0

    print Bar::$my_static . “n”;
    $bar = new Bar();
    print $bar->fooStatic() . “n”;
    ?>

    Problem9:
    foo();
    A::foo();
    $b = new B();
    $b->bar();
    B::bar();
    ?>
    $this is defined (a)
    $this is not defined.
    $this is defined (b)
    $this is not defined.

    Question about this code: how can this
    code’s result Could be like this:
    $this is defined (a)
    $this is not defined.
    $this is defined (b)
    $this is not defined.

    Problem 10:Static data example
    <?php
    class foo {
    // As of PHP 5.3.0
    public $bar = <<<'EOT'
    bar
    EOT;
    }
    how can be this static data?
    Problem11:
    recipients as $recipient)
    { $headers = ‘MIME-Version: 1.0’ . “rn”; $headers .= ‘Content-type: text/html; charset=iso-8859-1’ . “rn”; $headers .= ‘From: {$this->sender}’ . “rn”; $result = mail($recipient, $this->subject, $this->body, $headers); if ($result) echo “HTML Mail successfully sent to {$recipient}”; } }}?>

    sendEmail();$hm->sendHTMLEmail();?>
    here do not we should prepend parent with this method $hm->sendEmail() like $hm-> parent:: sendEmail()
    Problem 12:Call back function examples:

    “’B’, ‘parent::who’”=> what kind of parameter is this?

    Problem 13:call_user_func() using namespace name

    Problem 14:another unclear code:
    Using lambda function

    Please explain these two codesProblem13&14. I can’t totally understand these.

  15. sachin agarwal says:

    Hello Mizan.

    My question is :

    Question 1: Please explain the concept of abstract class in php?

    As i know.

    Abstract class is used as a base class for other class. An abstract class must have at least one abstract method.

    Question 2: What if i don’t declare any method as abstract?

    Question3: Is it necessary to give the definition of all methods in child class, which are declared as abstract in base class?

  16. sachin agarwal says:

    Hello Mizan.

    My question is :

    Question 1: Please explain the concept of abstract class in php?

    As i know.

    Abstract class is used as a base class for other class. An abstract class must have at least one abstract method.

    Question 2: What if i don’t declare any method as abstract?

    Question3: Is it necessary to give the definition of all methods in child class, which are declared as abstract in base class?

    Waiting for ur reply

  17. sheetal says:

    hi,

    i am php programmer having 5 yr of experience in php. i am searching for freelance work. and also tell me certification course in php. i passed my b.e computer in 2003.

    waiting for reply,
    sheetal mistry

  18. stef says:

    Hi,
    Can somebody help me. I install the wp-recent-comment. However, it does not give link with pagination comment.
    My question, what should I do to tell wordpress to get the pagination comments, What modification of this code I need to do?

    $result .= ” . $element_avatar . sprintf(‘%1$s ‘ . __(‘on’, ‘wp-recentcomments’) . ‘ %2$s’, get_comment_author_link(), ‘comment_post_ID) . ‘#comment-‘ . $comment->comment_ID . ‘”>’ . get_the_title($comment->comment_post_ID) . ‘‘) . ” . $comment_excerpt . ”;

    Thank you
    stef

  19. Marcus says:

    Hey,

    I’m having trouble getting some PHP arrays from my database.

    This isn’t exactly what is in my database, but this is a good example of what I’m trying to implement.

    My database table (called ‘tablename’) looks like this (minus keys and ids and whatnot):

    +—————-+——————+
    | users | pagename |
    +—————-+——————+
    | user1 | page1 |
    | user1 | page2 |
    | user2 | page3 |
    | user2 | page4 |
    +—————–+—————–+

    And my PHP looks sort of like this:

    $user_array = mysql_fetch_array(mysql_query(“SELECT pagename FROM tablename WHERE user = ‘user1′”), MYSQL_NUM) or die(mysql_error());

    The output I’m looking for is like this:

    Array( [0] => ‘page1’, [1] => ‘page2’)

    But I’m just getting the index 0 in the array:

    Array([0] => ‘page1’)

    Do you know anyway I can solve this to get all of the pagenames that have the same user into one array?

    It’s probably really simple but I’ve been thinking for hours and can’t get it. Thanks for your help,

    Llewgnolm

  20. Lorene says:

    Dear Friends,

    I need to enable the user to add a drop down menu and a text field by only clicking on button (in case the user wants more) I have the following codes how do I make it better:

    Enter User :
    Truck Mixer

    <?php //this peice of code will retriev the type directly form the database.
    include 'connect.php';
    $query= "SELECT * FROM truckmixers ";
    $result= mysql_query ($query);
    while ($Record = mysql_fetch_assoc($result)){
    echo "”.$Record[“CompanyCode”];
    }
    ?>

    how do I enable the user to add more text fields and drop-down menu at the same time by clicking Add More button.

    Thank you,

    Lorene.

  21. Steve R says:

    A challenge for you!

    I’ve created a website for customers to convert CSV/Excel files to another format.

    The customer’s CSV files have various layouts, for example, layout #1 will have the “Date” data in column A and the “Name” in column B whereas layout #2 might have the Date in column C and the Name in column F. Some layouts will have header rows and while other layouts will not.

    My site is PHP5/MySQL. The customer logs in and selects the layout from their menu, then uploads their file to be converted.

    How can I test the incoming file to ensure it is not malicious?

  22. bbbb says:

    I am getting one problem. If I use browser back button to go from 2nd or 3rd php page to 1st php page, it is giving me “Confirm Form resubmission” why?
    I tried couple of things but it seems nothing is working………..

  23. Thanveer says:

    Sir,I am just a learner in php and mysql.
    I need an help from you people.
    I have a table with the column storing the year.Also i have an table with year stored in that column.
    To be clear:
    table name:users
    column names:name,year
    values: Anand,cse-1year.
    I have another table with name:cse-1year.
    In that i have stored the name:Anand.
    I have to display the details of anand from table cse-1year.
    How could i link that column value with another table name and display the user details.
    My code is as follows:

  24. mohan says:

    hai dude i’m mohan from India my queation is how can i send data from one system to another system pls suggest some code for that

Leave a Reply to booleandreams Cancel reply

Your email address will not be published. Required fields are marked *