Jump to content

Recommended Posts

Hi everyone,

 

I've been using Microsoft ACCESS for a long time now. (BTW thats a short summary of what I do currently)

I've done all my databases on that and in PHP to access my database I connect this way:

 

$conn = new COM("ADODB.Connection") or die('Connect start ADO.');

$conn->Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\\Program Files\\Apache Group\\Apache2\\htdocs\\folder\\database.mdb");

And get my data from my tables I use this technique:

 

$getInfo = $conn->Execute("SELECT n.news_id, n.news_date, n.news_title, n.news_message, n.staff_id, s.staff_id, s.staff_name, s.staff_email, s.staff_rank FROM news n, staff s WHERE n.staff_id = s.staff_id ORDER BY news_date DESC");

 

To display:

 

$news_date = $getNews->Fields['news_date']->Value;

 

...

 

Alright, we'll recently I was asked to make a database, but this time I have to use phpMyAdmin.

I understand how the actual application works, how to manage the data, add tables and all...

But there is seriously something I'm not getting, and I know its probably really simple but i'm not figuring out apparently ???

 

Like, in phpMyAdmin, you can create multiple databases at the same time, and from that have your tables and fields in them... How do you make your php code to go for one database and not the other one?

 

Also, i've seen people retrieving their data this way (this is an example out of my head):

$value = $db['db']['table']'['apples'][1];

 

Does this also work with Microsoft Access? I'm used to the technique I've mentioned above the last one. I'm having a really hard time trying to familiarize myself with different technique other than the one I currently have.

 

If someone can understand which "concept" I'm having a hard time with, please mention and maybe a place where I can read more about this, which is clear. Again, its not the actual sql command or phpMyAdmin; Its more about the code that I actually have to type in PHP that is really confusing me

 

Thank you very much for your patience

 

Andrew

Link to comment
https://forums.phpfreaks.com/topic/50735-solved-phpmyadmin-php-code/
Share on other sites

For all of my mysql based sites I use a custom created function

 

function connectdb(){
$connection=mysql_connect('localhost', 'username', 'password') or die(mysql_error());
mysql_select_db('DATABASE_NAME') or die('Unable to select database!');
};

 

any time I need to make a database connection, I simply use

 

<?php connectdb(); ?>

 

Generally, you will have one database per site so the line above mysql_select_db() is what actually selects the database to work with. If your using more than one database per site, you could always modify this function like so

 

function connectdb($db_name){
$connection=mysql_connect('localhost', 'username', 'password') or die(mysql_error());
mysql_select_db($db_name) or die('Unable to select database!');
};

 

Then when you wanted to connect to a particular database you specify that when you call the function.

 

connectdb('test');

 

This would pass test into the $db_name variable hence connecting to the database named test. So you could have 3 databases, and connect to them this way

 

connectdb('database1');

connectdb('database2');

connectdb('database3');

 

I hope this makes sense.

 

Nate

 

Thank you very much chronister, the thing I was clearly not seing was to specify which database to access! Again thank you!

 

Now I have this other issue. What I don't get now is that my sql statement is giving me a fatal error

 

I try:

 

$query = $dbh->Execute("select * from frank");

 

and get this:

 

Fatal error: Call to a member function on a non-object in /home/website/public_html/website/dbpaths.php on line 16

 

It is rather odd, since the it does correctly connect to the database,  I get a successful message, and I created the simplest of tables with one field in it to try and extract the data out of it and I get this error. I tried reading a few articles from google but could still not figure out what the error was.

 

Are there any settings I need to change with phpMyAdmin? My user does have the privileges to do actions to the database but still it does this...

 

Thanks again in advance

 

 

 

Did you instantiate the dbh class?

 

IE:

<?php
$dbh = new dbh();
?>

 

Now I am unsure if that is correct as you did not post the DBH class you are using. That might prove to be helpful.

 

If all else fails this will work

 

<?php
$query = mysql_query("SELECT * FROM frank");

while ($row = mysql_fetch_array($query)) {
      echo  '<pre>' . print_r($row) . '</pre><br />';
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.