Newbie Help

Hi,
I’m new to mysql & php. I am building a small site that I want to connect to my db and edit tables. I have a connection called ‘test’ where I built my db & I am trying to connect to it. The script I am using is where I have hardcoded the variables (I know not proper coding but this is for learning purposes)…

$username = “Bob”;
$password = “mypassword”;
$hostname = “localhost”;

$dbhandle = mysql_connect($hostname, $username, $password)
or die(“Unable to connect to MySQL”);

When I load the page the result is…

UN=Bob
PW=mypassword
HN=localhost
Unable to connect to MySQL

The mysql I believe was setup when I built the computer & the password my be incorrect. How can I determine my password? Is the coding correct?

In mysql workbench under SQL Development I created a connection called Test and it shows my username as ‘Bob’ and Host as 127.0.0.1:3306. Is there more I need to setup under mysql?

Thanks for the help in advance.

K

You are off to the wrong start using deprecated code. There is no point in you learning it. It is obsolete. I have provided a complete PDO database download to get you started in the right direction. Download it, set your login credentials and import the sql for the database.

If you are still having trouble at that point I will be happy to help.

http://www.phphelp.com/forum/the-occasional-tutorial/beginners-pdo-bumpstart-code-use-it-now!

Thank you…off to work. I will work on it tonight.

I went to create the database in mysql workbench and this is the error I received…

ERROR 1044: Access denied for user ‘’@‘localhost’ to database ‘pdo_bumpstart’
SQL Statement:
CREATE SCHEMA pdo_bumpstart

ERROR 1044: Access denied for user ‘’@‘localhost’ to database ‘pdo_bumpstart’
SQL Statement:
CREATE SCHEMA pdo_bumpstart

I think somewhere my password got messed up or I set this up with 1 I cannot remember. How do I retrieve my password?

You are not using a correct mysql login. What is your setup?

Are you running Mysql on a local server (on your own computer) or is it on a remote server?
Is Mysql on Windows or Linux?
Do you have root access to the server? If not, whoever has root access will have to reset it.

I am running mysql locally on Mint 3.5.0-39-generic #60-Ubuntu SMP (I believe it is 14). I cannot login to root from the login screen, it always tells me I cannot login to root from there.

Can you get to a command prompt?

http://bit.ly/NiU0eZ

I was an asp programmer for 12 years and have been running linux for 4 but this is the first php/mysql website.

oh, yes i can get to a cmd prompt

If you can get to a root command prompt you can reset it the easiest. I posted a google link to several easy tutorials on how to reset the password. Since it has been answered numerous times on the web there is no point in me doing it. Google is your friend on the mysql password reset.

I currently have 2 issues and trying to work around each while learning PDO.

  1. While using the PDO connection I get “could not find driverConnected to MySQL.” So I found a little help on google.
    -dpkg --get-selections | grep php5-mysql
    -sudo apt-get install php5-mysql
    -sudo /etc/init.d/apache2 restart
    I could not get apache to restart to I rebooted the computer and still getting the error.

  2. I am trying to still get something build so I am trying the old coding, helps to learn a little php. I am trying to connect to my Test db and that works with the old code (temp using root). When I try to extract table data nothing comes out. Once I get issue 1 resolved I will convert to PDO.

$str=“Select * From tbl_Users Where UserName=‘Myname’ and Password=‘Mypassword’”
$rSet = mysql_query($db, $str);
$row = mysql_fetch_array($rSet);
echo “UNAME=” . $row[‘UserName’];

UserName is the name in the table Users and nothing ever shows up. What is missing or wrong?

Attach a zip file of ALL your files and a dump of your database.

Here is the zip for my site and mysql

Because not all pages need to have db access I have files db_connect.php and db_disconnect.php to help keep code clean, then I can just use an include statement. Currently the css is ugly and I am changing from 3 to 1 column but that is moot when it comes to the db work. All db work will go through the login.


DB.zip (6.01 KB)

Website.zip (5.34 KB)

Have you got your PDO connect issue handled? There is no point in messing with the deprecated code.

Ok, I finally got my db connection established. I have done a lot of research to get a single row from a table but it is all still very confusing. In ASP once I had my connection I had 1 line of sql and 1 line to execute which gave me a recordset. I am currently trying to do the same, I’m going to use a login with username and password, where I get the record from the table and check to see if the recordset exists. If it does, set a couple variables and move forward otherwise back to login screen.

Everything I find on google is either the php manual or forums which are confusing. It should not be this difficult. Below is what I would like to try to integrate…

$str=“Select * from tbl_users where username=’” . $_GET[uname] . “’ and password=’” . $GET[pword] . “’”

My connection is $DBH and connect with $str

Then use an IF statement to check if recordset exists.

In ASP I could simply use…
str=“Select * from tbl_users where username=’” & Request.Form(uname) & “’ and password=’” & Request.Form(pword) & “’”
rst=myconn.execute(str)

It’s really not that hard. Try the following code:

[php]<?php
$hostdb = ‘localhost’;
$dbname = ‘YOURDATABASENAME’;
$username = ‘root’;
$password = ‘’;

$table =‘YOURTABLENAME’;

try
{
$pdo = new PDO(“mysql:host=localhost;dbname=$dbname”, $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql  = "SELECT * FROM $table";
$stmt = $pdo->prepare($sql);
$stmt->execute();

$result = $stmt->fetchAll();

if (count($result))
    {
    foreach ($result as $row)
        {
        echo '<pre>';
        print_r($row);
        echo '</pre>';
        }
    }
else
    {
    echo "No rows returned.";
    }
}

catch (PDOException $e)
{
echo 'ERROR: ’ . $e->getMessage();
}
?>[/php]

Were you storing plain text passwords? o.O

Anyway, post your working code. It’s then easier to advise on how to proceed. You are correct in assuming this should be easy, but nothing is easy before you learn how to do it :slight_smile:

Ok, I have it working!!! WooHoo. I believe the last thing I might need for a bit would be to extract data from the recordset. For example, if I want to set a cookie called “name” where the value would be the field “Name” from table users. Below I am just trying to write the field value to the screen…

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$str=“Select * From tbl_Users Where UserName=’” . $_POST[‘un’] . “’ and Password=’” . $_POST[‘pw’] . “’”;
$stmt = $pdo->prepare($str);
$stmt->execute();
$rst = $stmt->fetchAll();

echo “RSTCOUNT=” . count($rst);
echo “
FirstName=” . $rst[FirstName];

I modified Kevin Rubio’s code and it works because I was able to write all records with that code to the screen, then just 1 who logged in. Now I just need a single field to set a cookie.

  1. never store plain text passwords.

  2. never have user submitted data in variables in the query. You are atm wide open for sql injection attacks, which combined with the plain text passwords make your site extremely unsecure.

This should be:

[php]$str=“Select * From tbl_Users Where UserName=? and Password=?”;
$stmt = $pdo->prepare($str);
$stmt->execute(array(
$_POST[‘un’],
$_POST[‘pw’]
));[/php]

Plus what Jim said.

Sponsor our Newsletter | Privacy Policy | Terms of Service