Click here to Skip to main content
14,831,860 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,

i am trying to connect to a simple database and i am given an error that is unable to connect, but i have no rights so no need of username and password, and the information is correct. please any help?

PHP
<?php
$username="uname";
$password="pword";
$database="db";
$hostname="localhost";

mysql_connect($hostname) or die("Unable to connect to SQLSERVER");
mysql_select_db ($database)or die( "Unable to select database");

$query="SELECT * FROM table";
$result=mysql_query($query) or die("SQL error: ".mysql_error());

echo "result = " + $result;

$num=mysql_num_rows($result);

mysql_close();



error :: Unable to select database
Posted
Updated 12-Feb-12 22:23pm
v3
Comments
CRDave1988 13-Feb-12 3:41am
   
use
mysql_connect($hostname) or die( mysql_error());
and see what is actual Error and tell us.
adnama 13-Feb-12 4:19am
   
You were right the error was not that one. It was actually
Notice: Undefined index: $event_id in C:\xampp\htdocs\dbConn\index.php on line 26

and on line 26 i have echo "event_id : {$row['$event_id']} <br>";

i am trying to display the data.
Dhol Gaurav 13-Feb-12 4:28am
   
Please try below code it might work. there are two ways to resolve this problem

1) echo "event_id : {$row['event_id']} <br>" ;

2) $event_id = 0;
echo "event_id : {$row['$event_id']} <br>" ;

Hello

You have to pass user name and password into mysql_connect like
PHP
mysql_connect($hostname,$username,$password) or die("Unable to connect to SQLSERVER");


here you must have to pass user name and it can not be blank, in case of password you can pass blank like

PHP
$password = ""
mysql_connect($hostname,$username,$password) or die("Unable to connect to SQLSERVER");
   
I have solved this myself as it was simply removing the $.

from "event_id : {$row['$event_id']} <br>";
to
"event_id : {$row['event_id']} <br>";

thanks for your help
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900