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
https://forums.phpfreaks.com/topic/66387-newbie-phpmysql-problem-question/
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

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'.

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;

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.