Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
Like Tree4Likes

Thread: Is this viable code?

  1. #1
    Member
    Join Date
    Mar 2012
    Posts
    50
    Member #
    31189

    Is this viable code?

    Hi this is my fist attempt at building a mysql database and I'm still trying to get my bearings in place (year-round school doesn't help lol). Anyway I wanted to ask if the following was viable code...I've been advised to get in the habit of using variables.

    <?php

    //variables for database entry
    $dbhost = "localhost";
    $dbuser = "root";
    $dbpass = ""; # no password for this database
    $dberror1 = "Can't connect to database. Sorry.";

    $connect = mysql_connect($dbhost, $dbuser, dbpass) ;

    if ($connect)
    {
    echo "Connected!";
    }
    else
    {
    echo "problem.";
    }

    $select_db = mysql_select_db('name_of_db') or die ("couldn't select database");

    //dictionary is database name
    $query = "SELECT * FROM dictionary WHERE id ='x';";

    $fetch = mysql_query('$query') or die ("Couldn't find user");

    while ($row = mysql_fetch_assoc ($fetch))
    {
    echo $row['word'].*<br>*;
    echo $row['origin'].*<br>*;
    echo $row['meaning'].*<br>*;
    echo $row['thes'];
    }

    ?>

    Also are $connect and $conn both valid?


    Many thanks in advance!!!

  2.  

  3. #2
    Senior Member Webzarus's Avatar
    Join Date
    May 2011
    Location
    South Carolina Coast
    Posts
    3,322
    Member #
    27709
    Liked
    770 times
    Drop the "where I'd=x" out of your SQL statement.

    if you want to loop through all the records... Using a "where" clause in the SQL statement will limit ( based on the value of X ) what records should be pulled.

  4. #3
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,485
    Member #
    425
    Liked
    2783 times
    I think he meant more along the lines of "rather than using strings as part of the code, put the strings into variables."

    Yes, variables are valid and you should use them whenever possible. Both $connect and $conn are valid...most half-decent developers should know what $conn is short for.

    As far as if it's viable...it's easy enough to read and a decent developer should be able to understand it, so yes to that too. You'll never write code that every developer wannabe understand...not possible (some developers just suck, and there's no writing code to compensate for that). Your key is that you want to use meaningful variable names, subs, functions, classes, properties, etc, and you've done that.
    acegreen likes this.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  5. #4
    Member
    Join Date
    Mar 2012
    Posts
    50
    Member #
    31189
    Hi -

    Thank you for the replies. I ran through the code today, but ran into a problem. I basically got this far:

    <?php
    //variables for database entry
    $dbhost = "localhost";
    $dbname = "sitename_myDatabase";
    $dbuser = "sitename_user";
    $dbpass = "password";
    $dberror1 = "Could not connect to the database!";

    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ($dberror1); #connect to mysql
    ?>

    So I can connect without any hiccups but when I code the next line I get my error message:

    $select_db = mysql_select_db($dbname) or die ($dberror1);

    I have double and triple checked the spelling on the $dbname variable. So I don't know what part of $select_db = mysql_select_db($dbname) is the problem. My database exists in the phpmyadmin. I've got a several table all ready. But can't figure out where that line goes off.

    Also is there anything to the fact that my host requires that "sitename_" precede my database name and user?

    Please help!

    Thank you.
    Last edited by acegreen; Aug 21st, 2013 at 12:22 AM.

  6. #5
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,485
    Member #
    425
    Liked
    2783 times
    Maybe your user doesn't have permissions on that particular database. If it's connecting fine, it means your connection string is okay. If your database name is spelled correctly, then that's likely what it is.
    acegreen likes this.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  7. #6
    Member
    Join Date
    Mar 2012
    Posts
    50
    Member #
    31189
    Hi - thanks. It worked!!!

    My user wasn't "linked" to the db itself. Oddly, my host forces me to create databases and users through its Database Wizard.

    Is there any way to get $row['heading'] echoed out alongside the data?
    for example, suppose I have:

    $fetch = mysql_query($query) or die ($dberror1);
    while ($row = mysql_fetch_assoc ($fetch))
    {
    echo $row['origin']."<br>";
    echo $row['syn'];
    }


    I'd like the output to be origin: corresponding data from table
    synonyms: corresponding data from table


    Thanks again!
    Last edited by acegreen; Aug 21st, 2013 at 02:10 PM. Reason: echo $row

  8. #7
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,485
    Member #
    425
    Liked
    2783 times
    Sounds like the control panel (I'm guessing Parallels, as I've had similar issues with it) rather than the host.

    Not sure about Excel importing into MySQL. I primarily use SQL Server.
    acegreen likes this.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  9. #8
    Member
    Join Date
    Mar 2012
    Posts
    50
    Member #
    31189
    Excel seems to work relatively well thus far actually (at least as .csv). My db is up and running. Very happy. Is there a way to populate the db from phpmyadmin?

    Also concering $query, I've noticed I can't use a variable.
    For example: $query = "SELECT * FROM dictionary WHERE id ='x';";
    gives me an empty page

    $query = "SELECT * FROM dictionary WHERE id ='1';";
    gives me the info for item id 1.

    I was expected that $query = "SELECT * FROM dictionary WHERE id ='x';"; would give me a run down of all items in my db.

    I mention this because ultimately I need the db to be searchable by users and so $query variable has to be determined by the user (and they will be entering a word, which then my php page has to link to the right id column...do you have any recommendations as to how to start coding that part?

    Much appreciate the feedback! Thanks !

  10. #9
    Senior Member Webzarus's Avatar
    Join Date
    May 2011
    Location
    South Carolina Coast
    Posts
    3,322
    Member #
    27709
    Liked
    770 times
    If you want to SHOW ALL records...

    dont use the WHERE clause... You only use that to "limit" the results

    For a SEARCH you WOULD use the WHERE clause and pass the "search criteria" into the SQL string.

    myPhpAdmin is a tool, yes you can use it to populate the DB, one record at a time... Just like creating a form and use an insert statement...

    if all of your data is in an excel .csv file and you have the appropriate drivers and connection strings, you possibly could... I typically put data into a access DB from the excel... And access has an "export to ODBC" option, I setup a ODBC connection ...

    Although I'm sure there are other ways of doing it... I've been usin access like this for years with SQL, MySQL, oracle and other DB's.

  11. #10
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,485
    Member #
    425
    Liked
    2783 times
    What WZ said. WHERE filters results...unless you do something weird like WHERE 1=1. Since 1 always equals 1 it'll return everything (and there are limited circumstances where that specific query will come in handy).

    If you want to use a variable, you can do something like this:

    $query = "SELECT * FROM dictionary WHERE id ='.$dictionaryId.';";

    Bear in mind, though, that this opens you up to some risks (specifically, SQL injection) so you'll need to make sure that you sanitize your input. If your dictionaryId is a number, make sure that the variable only allows numbers. Make sure that you escape any ' characters. Make sure that you don't allow for any <tags> unless you absolutely must (and even then, be careful). etc.
    Last edited by TheGAME1264; Aug 22nd, 2013 at 10:54 AM.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 12:03 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com