Page 1 of 1

Scripting issues

Posted: Thu Mar 23, 2006 10:02 am
by andy_hall_4
hawleyjr | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hello,

I'm a bit of a newbie to PHP and just can't understand why the following code isn't working. Please could someone advise me on what is going wrong....

The script is...

Code: Select all

<?
$username="digslis_root";
$password="*****";
$database="dislis_addresses";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM contacts";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$forename=mysql_result($result,$i,"forename");
$surname=mysql_result($result,$i,"surname");


echo "<b>$first $last</b><br>";

$i++;
}

?>


Obviously I have changed the ***** for my password. However when I run the script all I get is the following message:

Warning: mysql_connect(): Access denied for user: 'digslis_root@localhost' (Using password: YES) in /home/digslis/public_html/test3.php on line 6
Unable to select database

I have a feeling that this isn't a script problem but more a problem with my account.

Could anybody advise?


hawleyjr | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Thu Mar 23, 2006 10:35 am
by mattcooper
Check your variable names... is the database named correctly? Looks to me like you've spelt it wrong:

Code: Select all

$username="digslis_root"; 
$password="*****"; 
$database="dislis_addresses"; // This is the variable you may have spelt wrong...
Hope this helps...

Posted: Thu Mar 23, 2006 11:01 am
by andy_hall_4
Thanks for spotting that I have changed it, it was indeed missing a 'g'.

I have now adjusted the code slightly and it now reads

Code: Select all

<?php
$username="digslis_root";
$password="*****";
$database="digslis_addresses";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
echo "Connected!";
$query="SELECT * FROM table";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$forename=mysql_result($result,$i,"forename");
$surname=mysql_result($result,$i,"surname");


echo "<b>$first $last</b><br>";

$i++;
}

?>
And now this returns the following result:

Code: Select all

Connected!
Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/digslis/public_html/test3.php on line 11

Database Output

So obviously I'm getting somewhere but don't understand why I'm now getting this message. Any ideas?


Cheers,

Andy


Posted: Thu Mar 23, 2006 11:15 am
by feyd
"table" is a reserved word in MySQL. If your table is in fact named "table" then it should be backticked

Code: Select all

SELECT * FROM `table`
In reality, all database, table and field references should be backticked in all queries regardless of being a reserved word or not.