Jump to content

[SOLVED] my script will not connect to the database :@--must be error in code


runnerjp

Recommended Posts

<?

$conn = mysql_connect("localhost", "username", "password");

            $db = @mysql_select_db("runnerse_profiles", "images", $conn);

$sql = "SELECT * FROM $tbl_images WHERE image_id = $auth[member_id] AND default_pic = \"yes\"";

$result = @mysql_query($sql,$connection) or die("Couldn't execute query.");

$PICSnum=mysql_num_rows($result);

while ($row = mysql_fetch_array($result)) {

$url = $row['url'];

$image = $row['image'];

}

if(empty($url)) {$newurl="$imgdir";}else{$newurl="$userdir/$url";}

if(empty($image))$image="pic.gif";

$showimg="/$newurl/$image";

            ?>

            <script type="text/javascript">

function openpopup(popurl){

var winpops=window.open(popurl,"","width=610px,height=625px,resizable")

}

            </script>

            <p align="center">

            <a href="javascript:openpopup('showpic.php?img=<?echo $image ?>')"><IMG SRC="../../thumbs/phpThumb.php?src=<?echo $showimg ?>&w=150" border="0"></a>

            <?

            if($PICSnum > "0") {

            ?>

            <br><a href="<?echo $PHP_SELF ?>?action=pics">My Pictures >></a>

            <? }

                        if($profile_id == $auth[member_id]){

                        echo "<br><a href=\"$userurl/upload.php\">upload picture</a>";

                        } else {

                        if($profile_id != "00000001")

echo "<br><a href=\"/message.php?mail_to=$profile_id&p_name=$p_name\">

<img src=\"$imgurl/mail.gif\" border=\"0\" alt =\"Send Message!\"></a> 

            <a href=\"$siteurl/friends.php?action=invite&profile_id=$profile_id\">

            <img src=\"$imgurl/friend.gif\" border=\"0\" alt=\"Send Friend Request!\"></a>";

                        }

                        ?>

 

 

btw the script is retreving users images from the data base... like an avator

Link to comment
Share on other sites

if it help...all im doing is going into the table images and getting the users image they have uploaded

 

the table looking like this

 

CREATE TABLE `images` (
  `image_id` varchar( NOT NULL default '',
  `directory` varchar(150) NOT NULL default '',
  `image` varchar(60) NOT NULL default '',
  `url` varchar(150) NOT NULL default '',
  `displayname` varchar(25) NOT NULL default '',
  `default_pic` enum('yes','no') NOT NULL default 'no'
) TYPE=MyISAM;

 

i displayed the structure as its easyer for u guys 2 see then :)

 

the image id is the same is the users id

Link to comment
Share on other sites

$conn = mysql_connect("localhost", "username", "password");

$db = @mysql_select_db("runnerse_profiles", "images");

 

you dont need to put $conn iin the mysql_selecet_db() function..

 

$conn is needed so the mysql_* functions know what connection to use, if you do not do this you will have problems handling multiple db servers.

 

Why it is optional to pass the connection to mysql_* functions it is not optional in other database apis.

 

The reason for the connection problems is you are using two strings followed by $conn in your mysql_select_db, it should be

 

mysql_select_db($name, $conn) //$conn being optional

Link to comment
Share on other sites

$conn = mysql_connect("localhost", "username", "password");

$db = @mysql_select_db("runnerse_profiles", "images");

 

you dont need to put $conn iin the mysql_selecet_db() function..

 

$conn is needed so the mysql_* functions know what connection to use, if you do not do this you will have problems handling multiple db servers.

 

Why it is optional to pass the connection to mysql_* functions it is not optional in other database apis.

 

The reason for the connection problems is you are using two strings followed by $conn in your mysql_select_db, it should be

 

mysql_select_db($name, $conn) //$conn being optional

 

usually i'd agree... but unless your doing a server of massive proportions... having 2 databases powering one page... is not really necessary...

Link to comment
Share on other sites

$conn = mysql_connect("localhost", "username", "password");

$db = @mysql_select_db("runnerse_profiles", "images");

 

you dont need to put $conn iin the mysql_selecet_db() function..

 

$conn is needed so the mysql_* functions know what connection to use, if you do not do this you will have problems handling multiple db servers.

 

Why it is optional to pass the connection to mysql_* functions it is not optional in other database apis.

 

The reason for the connection problems is you are using two strings followed by $conn in your mysql_select_db, it should be

 

mysql_select_db($name, $conn) //$conn being optional

 

usually i'd agree... but unless your doing a server of massive proportions... having 2 databases powering one page... is not really necessary...

 

I never said having two databases was ideal, But to restrict your self to one when you could otherwise have support for multiple is just stupid. Also in other database apis it is not an optional parameter so I think it is best to do to be consistent.

 

Multiple mysql databases like you said is most probably pointless like you say in relation to mysql but in there are times multiple databases come in handy.

Link to comment
Share on other sites

<?php

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}
?> 

Link to comment
Share on other sites

One thing is for sure, the @'s just make the problem 10x harder to diagnose. It is never good to use those to surpress an error.

 

One option is to create 2 connections to the database and use 1 connection for 1 database and the 2nd for the other IE:

 

<?php
$conn1 = mysql_connect("localhost", "username", "password");
$conn2 = mysql_connect("localhost", "username", "password");
mysql_select_db("db1", $conn1);
mysql_select_db("db2", $conn2);

$query = mysql_query("SELECT * FROM TABLE", $conn2);

 

Unsure if it works and probably is not very proficient and may cause mysql max user problems down the line.

 

If at all possible and it is "unnescary" to use 2 database I would combine them into one.

 

I am sure there is someone who has expierence with this, but usually when more than 1 database is used the 2nd one is hosted on a seperate server.

 

Just incase the mysql_select_db() function still won't work with multiple database connections (as has happened to me before).

 

$dbh1 = mysql_pconnect($host,$user,$pass);

$dbh2 = mysql_pconnect($host,$user,$pass);

 

You could do this...

 

mysql_query("USE database1",$dbh1);

mysql_query("Use database2",$dbh2);

 

This does the same thing as the mysql_select_db() function...

 

or this...

 

You don't even have to select the database for each connection.

 

mysql_query("SELECT * FROM database1.table",$dbh1);

mysql_query("SELECT * FROM database2.table",$dbh2);

 

You could also try that too.

Link to comment
Share on other sites

what i have done is added it all into 1 database!

 

 

 <?
		$conn = mysql_connect("localhost", "", "") or die(mysql_error());
            $db = mysql_select_db("runnerse_profiles") or die(mysql_error());
		$sql = "SELECT * FROM $tbl_images WHERE image_id = $auth[member_id] AND default_pic = \"yes\"";
		$result = @mysql_query($sql,$connection) or die("Couldn't execute query.");
		$PICSnum=mysql_num_rows($result);
		while ($row = mysql_fetch_array($result)) {
		$url = $row['url'];
		$image = $row['image'];
		}
		if(empty($url)) {$newurl="$imgdir";}else{$newurl="$userdir/$url";}
		if(empty($image))$image="pic.gif";
		$showimg="/$newurl/$image";
            ?>
            <script type="text/javascript">
		function openpopup(popurl){
		var winpops=window.open(popurl,"","width=610px,height=625px,resizable")
		}
            </script>
            <p align="center">
             <a href="javascript:openpopup('showpic.php?img=<?echo $image ?>')"><IMG SRC="../../thumbs/phpThumb.php?src=<?echo $showimg ?>&w=150" border="0"></a>
             <?
             if($PICSnum > "0") {
             ?>
             <br><a href="<?echo $PHP_SELF ?>?action=pics">My Pictures >></a>
             <? }
                        if($profile_id == $auth[member_id]){
                        echo "<br><a href=\"$userurl/upload.php\">upload picture</a>";
                        } else {
                        if($profile_id != "00000001")
					echo "<br><a href=\"/message.php?mail_to=$profile_id&p_name=$p_name\">
					<img src=\"$imgurl/mail.gif\" border=\"0\" alt =\"Send Message!\"></a> 
            <a href=\"$siteurl/friends.php?action=invite&profile_id=$profile_id\">
            <img src=\"$imgurl/friend.gif\" border=\"0\" alt=\"Send Friend Request!\"></a>";
                        }
                        ?>

but it still wont connect to that database??...thing is i have other scripts connecting to it

Link to comment
Share on other sites

<?

$conn = mysql_connect("localhost", "", "") or die(mysql_error());

            $db = mysql_select_db("runnerse_profiles") or die(mysql_error());

$sql = "SELECT * FROM $tbl_images WHERE image_id = $auth[member_id] AND default_pic = \"yes\"";

$result = @mysql_query($sql,$connection) or die("Couldn't execute query.");

 

You are using $connection, which should be $conn and do not use the @ signs, they do no good. Be consistent with referecing variable names.

Link to comment
Share on other sites

now i have this and guess wat...still cannot connect lol duin my head in this

 <?
		$conn = mysql_connect("localhost", "", "") or die("Couldn't connect.");
            $db = mysql_select_db(runnerse_profiles) or die("Couldn't select database.");
		$sql = "SELECT * FROM $tbl_images WHERE image_id = $auth[member_id] AND default_pic = \"yes\"";
		$result = mysql_query($sql,$conn) or die("Couldn't execute query.");
		$PICSnum=mysql_num_rows($result);
		while ($row = mysql_fetch_array($result)) {
		$url = $row['url'];
		$image = $row['image'];
		}
		if(empty($url)) {$newurl="$imgdir";}else{$newurl="$userdir/$url";}
		if(empty($image))$image="pic.gif";
		$showimg="/$newurl/$image";
            ?>
            <script type="text/javascript">
		function openpopup(popurl){
		var winpops=window.open(popurl,"","width=610px,height=625px,resizable")
		}
            </script>
            <p align="center">
             <a href="javascript:openpopup('showpic.php?img=<?echo $image ?>')"><IMG SRC="../../thumbs/phpThumb.php?src=<?echo $showimg ?>&w=150" border="0"></a>
             <?
             if($PICSnum > "0") {
             ?>
             <br><a href="<?echo $PHP_SELF ?>?action=pics">My Pictures >></a>
             <? }
                        if($profile_id == $auth[member_id]){
                        echo "<br><a href=\"$userurl/upload.php\">upload picture</a>";
                        } else {
                        if($profile_id != "00000001")
					echo "<br><a href=\"/message.php?mail_to=$profile_id&p_name=$p_name\">
					<img src=\"$imgurl/mail.gif\" border=\"0\" alt =\"Send Message!\"></a> 
            <a href=\"$siteurl/friends.php?action=invite&profile_id=$profile_id\">
            <img src=\"$imgurl/friend.gif\" border=\"0\" alt=\"Send Friend Request!\"></a>";
                        }
                        ?>

Link to comment
Share on other sites

there error is in here $conn = mysql_connect("localhost", "", "") or die("Couldn't connect.");

            $db = mysql_select_db(runnerse_profiles) or die("Couldn't select database.");

$sql = "SELECT * FROM $tbl_images WHERE image_id = $auth[member_id] AND default_pic = \"yes\"";

$result = mysql_query($sql,$conn) or die("Couldn't execute query.");

 

or is it bad database>

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.