Jump to content

Newbie PHP/MySQL problem & question


FutureCoder

Recommended Posts

Hello

 

My problem is with the code below:

 

<?php

$db = mysql_connect("localhost");

mysql_select_db("menagerie", $db);

$query = "select * from animals";

$result = mysql_query($query);

$table = mysql_field_table($result, 0);

echo $table;

 

This code is supposed to output the name of the table named in the code(animals table from a MySQL database called menagerie).  It is exactly like the code in the tutorial I'm going thru but doesn't output anything.  What am I doing wrong?

 

And a question:  I find that when I run scripts and then make corrections or changes, if I run in PHP Coder IDE sometimes the change does not happen immediately so the output will be the same as before a couple of times until I see the proper output or it will "take" in the coder ide but not in the browser immediately and vice versa.  What am I doing wrong?

Link to comment
Share on other sites

This is a little snippet of code i use on every project that will save you a lot of time and trouble.

 

<?php
$db_host = "localhost";
$db_user = "";
$db_password = "";
$db_name = "";

$connection = mysql_connect( $db_host, $db_user , $db_password) or die("Error Connecting");
mysql_select_db($db_name);
?>

 

If you put that in a separate file and just do a require() it will save you a lot of time.

 

As for your code, i think you need to specify a user.

 

~D

Link to comment
Share on other sites

BTY, Thanks for responding quickly!  I really appreciate it!  The suggestions didn't work but I'm probably using them wrong.  More suggestions would be greatly welcomed and appreciated!

 

"why don't you just use mysql_fetch_assoc($result)" phpSensei

That didn't work but thanks.

 

"do you have any passwords or such to your db?" uwannadonkey

I have a password to get into the MySQL Command line environment but none for any particular db's.

 

"<?php

$db_host = "localhost";

$db_user = "";

$db_password = "";

$db_name = "";

 

$connection = mysql_connect( $db_host, $db_user , $db_password) or die("Error Connecting");

mysql_select_db($db_name);

?>

 

If you put that in a separate file and just do a require() it will save you a lot of time.

 

As for your code, i think you need to specify a user.

 

~D" Daleeburg

That also didn't work but thanks. 

 

Could you please explain 'specify a user'.

Link to comment
Share on other sites

by specify user i meant you need to put in the user name and password that you need to get into.

 

<?php
$db_host = "localhost";
$db_user = "[put your user name here, most likely 'root']";
$db_password = "[put your password here]";
$db_name = "[put the database name here]";

$connection = mysql_connect( $db_host, $db_user , $db_password) or die("Error Connecting");
mysql_select_db($db_name);
?>

 

save that to file called "config.php" then at the top of the page with the query put this

 

require('config.php');

 

so the final page would e something like this.

 

<?php

require('config.php');

$query = "select * from animals";
$result = mysql_query($query);
$table = mysql_field_table($result, 0);
echo $table;

?>

Link to comment
Share on other sites

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.