runnerjp Posted May 9, 2007 Share Posted May 9, 2007 <? $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 Quote Link to comment https://forums.phpfreaks.com/topic/50638-solved-my-script-will-not-connect-to-the-database-must-be-error-in-code/ Share on other sites More sharing options...
only one Posted May 9, 2007 Share Posted May 9, 2007 $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.. Quote Link to comment https://forums.phpfreaks.com/topic/50638-solved-my-script-will-not-connect-to-the-database-must-be-error-in-code/#findComment-248899 Share on other sites More sharing options...
runnerjp Posted May 9, 2007 Author Share Posted May 9, 2007 thanks... sill mistake there lol but its still not connecting Quote Link to comment https://forums.phpfreaks.com/topic/50638-solved-my-script-will-not-connect-to-the-database-must-be-error-in-code/#findComment-248905 Share on other sites More sharing options...
runnerjp Posted May 9, 2007 Author Share Posted May 9, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/50638-solved-my-script-will-not-connect-to-the-database-must-be-error-in-code/#findComment-248918 Share on other sites More sharing options...
Nameless12 Posted May 9, 2007 Share Posted May 9, 2007 $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 Quote Link to comment https://forums.phpfreaks.com/topic/50638-solved-my-script-will-not-connect-to-the-database-must-be-error-in-code/#findComment-248928 Share on other sites More sharing options...
taith Posted May 9, 2007 Share Posted May 9, 2007 $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... Quote Link to comment https://forums.phpfreaks.com/topic/50638-solved-my-script-will-not-connect-to-the-database-must-be-error-in-code/#findComment-248930 Share on other sites More sharing options...
runnerjp Posted May 9, 2007 Author Share Posted May 9, 2007 ahhh lol ok so what do i do then if u dnt agree taith Quote Link to comment https://forums.phpfreaks.com/topic/50638-solved-my-script-will-not-connect-to-the-database-must-be-error-in-code/#findComment-248931 Share on other sites More sharing options...
taith Posted May 9, 2007 Share Posted May 9, 2007 i assume your getting a username/password error? Quote Link to comment https://forums.phpfreaks.com/topic/50638-solved-my-script-will-not-connect-to-the-database-must-be-error-in-code/#findComment-248936 Share on other sites More sharing options...
Nameless12 Posted May 9, 2007 Share Posted May 9, 2007 $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. Quote Link to comment https://forums.phpfreaks.com/topic/50638-solved-my-script-will-not-connect-to-the-database-must-be-error-in-code/#findComment-248937 Share on other sites More sharing options...
runnerjp Posted May 9, 2007 Author Share Posted May 9, 2007 no get "Couldn't select database." which is weird as that is the database name Quote Link to comment https://forums.phpfreaks.com/topic/50638-solved-my-script-will-not-connect-to-the-database-must-be-error-in-code/#findComment-248940 Share on other sites More sharing options...
taith Posted May 9, 2007 Share Posted May 9, 2007 $conn = mysql_connect("localhost", "username", "password") or die(mysql_error()); $db = mysql_select_db("runnerse_profiles", "images") or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/50638-solved-my-script-will-not-connect-to-the-database-must-be-error-in-code/#findComment-248944 Share on other sites More sharing options...
runnerjp Posted May 9, 2007 Author Share Posted May 9, 2007 no still just get "Couldn't select database." Quote Link to comment https://forums.phpfreaks.com/topic/50638-solved-my-script-will-not-connect-to-the-database-must-be-error-in-code/#findComment-248947 Share on other sites More sharing options...
jitesh Posted May 9, 2007 Share Posted May 9, 2007 <?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()); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/50638-solved-my-script-will-not-connect-to-the-database-must-be-error-in-code/#findComment-248948 Share on other sites More sharing options...
taith Posted May 9, 2007 Share Posted May 9, 2007 no still just get "Couldn't select database." meaning... a) you have a typo b) the database doesnt exist yet c) your mysql is pooched Quote Link to comment https://forums.phpfreaks.com/topic/50638-solved-my-script-will-not-connect-to-the-database-must-be-error-in-code/#findComment-248952 Share on other sites More sharing options...
runnerjp Posted May 9, 2007 Author Share Posted May 9, 2007 i have looked at it... yet all looks correct to me Quote Link to comment https://forums.phpfreaks.com/topic/50638-solved-my-script-will-not-connect-to-the-database-must-be-error-in-code/#findComment-248958 Share on other sites More sharing options...
runnerjp Posted May 9, 2007 Author Share Posted May 9, 2007 should i start from scratch...is there an easyer way of doing this???...does it have to be done by db Quote Link to comment https://forums.phpfreaks.com/topic/50638-solved-my-script-will-not-connect-to-the-database-must-be-error-in-code/#findComment-248963 Share on other sites More sharing options...
taith Posted May 9, 2007 Share Posted May 9, 2007 technically... no... you can switch a database out for straight files... but databases make everyones lives SO EASY! Quote Link to comment https://forums.phpfreaks.com/topic/50638-solved-my-script-will-not-connect-to-the-database-must-be-error-in-code/#findComment-248974 Share on other sites More sharing options...
only one Posted May 9, 2007 Share Posted May 9, 2007 could be probleme with the host.. it might not be localhost (if your using a webhost) Quote Link to comment https://forums.phpfreaks.com/topic/50638-solved-my-script-will-not-connect-to-the-database-must-be-error-in-code/#findComment-248991 Share on other sites More sharing options...
per1os Posted May 9, 2007 Share Posted May 9, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/50638-solved-my-script-will-not-connect-to-the-database-must-be-error-in-code/#findComment-249063 Share on other sites More sharing options...
runnerjp Posted May 9, 2007 Author Share Posted May 9, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/50638-solved-my-script-will-not-connect-to-the-database-must-be-error-in-code/#findComment-249089 Share on other sites More sharing options...
runnerjp Posted May 9, 2007 Author Share Posted May 9, 2007 still getting not connecting to DB Quote Link to comment https://forums.phpfreaks.com/topic/50638-solved-my-script-will-not-connect-to-the-database-must-be-error-in-code/#findComment-249232 Share on other sites More sharing options...
per1os Posted May 9, 2007 Share Posted May 9, 2007 <? $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. Quote Link to comment https://forums.phpfreaks.com/topic/50638-solved-my-script-will-not-connect-to-the-database-must-be-error-in-code/#findComment-249242 Share on other sites More sharing options...
runnerjp Posted May 9, 2007 Author Share Posted May 9, 2007 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/50638-solved-my-script-will-not-connect-to-the-database-must-be-error-in-code/#findComment-249259 Share on other sites More sharing options...
runnerjp Posted May 10, 2007 Author Share Posted May 10, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/50638-solved-my-script-will-not-connect-to-the-database-must-be-error-in-code/#findComment-249461 Share on other sites More sharing options...
runnerjp Posted May 10, 2007 Author Share Posted May 10, 2007 "bump" Quote Link to comment https://forums.phpfreaks.com/topic/50638-solved-my-script-will-not-connect-to-the-database-must-be-error-in-code/#findComment-249588 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.