Page 1 of 1

Problem connecting to Database in mysql using PHP

Posted: Fri Aug 29, 2003 3:47 pm
by phpcoder2003
hi ,

I am new to PHP. I am trying to connect to a database and then to select something from it.

$connection=mysql_connect("xxxxxxx","xxxxx","xxxxx") or die(mysql_error());

This works fine but then I have a database name student and user name dbadmin.

in mysql I have granted all privileges to dbadmin on student.but when I try to use student as my database with dbadmin as user it gives me the below error :

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource

this is my code below :
<?
$connection=mysql_connect("xxxx","dbadmin","xxxxx") or die(mysql_error());
if ($connection)
{
$msg = "Success !!";
}

$db_name = "student";
$db = mysql_select_db($db_name,$connection);
$query = "select * from system_user";
$result = mysql_query($query, $db);
if(!$result)
{
$msg = "ERROR : Not getting the database !!";
}
?>
<html>
<head><title> Mysql Connection </title></head>
<body>
<? echo "$msg"; ?>
</body>
</html>

PLEASE HELP !!
Thanks in advance

Posted: Fri Aug 29, 2003 4:24 pm
by d-m
:lol:

Grettings from Brasil,

So lets see your situation.

To connect to and mysql database and make a select do like thise.

Make a connection.inc file that you can call from all your php files
way do that? Becouse if you change the one parameter from test to production you just have to change one file. :)

-----------------
connection.inc
-----------------
<?php
$connection = mysql_connect ("localhost", "USERNAME", "PASSWORD")
or die ("The MySql is Down -->" . mysql_error());

mysql_select_db("DATABASENAME", $connection)
or die ("I could NOT connect to database DATABASENAME" . mysql_error());
?>
-----------------

You dont have to test the connection like you did as thise code becose if do not work the code enters at the DIE clause ! :)
if ($connection)
{
$msg = "Success !!";
}


sample of one select (printing the username from the login table)
--------------------------~
include('connection.inc');

$result = mysql_query ("SELECT username FROM login WHERE email = '$inEMAIL'")
or die ("ERROR: Making a Select at table login -> " . mysql_error());

$fetch = mysql_fetch_row($result);

if ($fetch[0] != "") {
echo $fetch[0];
}
else {
echo "It do not have a user with thise email ";
}
------------


I hope it could help U

Posted: Fri Aug 29, 2003 6:17 pm
by JAM
To refer to the asked question, you have

Code: Select all

$result = mysql_query($query, $db);
But $db in your case is refering to a mysql_select_db(). You should instead use $connection that is refering to the actual connection-link.

Code: Select all

$result = mysql_query($query, $connection);
Hope that helped.