jkewlo Posted July 6, 2008 Share Posted July 6, 2008 ok i keep getting "cannot select DB" this is my code <?php $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="Forum"; // Database name $tbl_name="forum_question"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // get value of id that sent from address bar $id=$_GET['id']; $sql="SELECT * FROM $tbl_name WHERE id='$id'"; $result=mysql_query($sql); $rows=mysql_fetch_array($result); ?> <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <td><table width="100%" border="0" cellpadding="3" cellspacing="1" bordercolor="1" bgcolor="#FFFFFF"> <tr> <td bgcolor="#F8F7F1"><strong><? echo $rows['topic']; ?></strong></td> </tr> <tr> <td bgcolor="#F8F7F1"><? echo $rows['detail']; ?></td> </tr> <tr> <td bgcolor="#F8F7F1"><strong>By :</strong> <? echo $rows['name']; ?> <strong>Email : </strong><? echo $rows['email'];?></td> </tr> <tr> <td bgcolor="#F8F7F1"><strong>Date/time : </strong><? echo $rows['datetime']; ?></td> </tr> </table></td> </tr> </table> <BR> <?php $tbl_name2="forum_answer"; // Switch to table "forum_answer" $sql2="SELECT * FROM $tbl_name2 WHERE question_id='$id'"; $result2=mysql_query($sql2); while($rows=mysql_fetch_array($result2)){ ?> <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td bgcolor="#F8F7F1"><strong>ID</strong></td> <td bgcolor="#F8F7F1">:</td> <td bgcolor="#F8F7F1"><? echo $rows['a_id']; ?></td> </tr> <tr> <td width="18%" bgcolor="#F8F7F1"><strong>Name</strong></td> <td width="5%" bgcolor="#F8F7F1">:</td> <td width="77%" bgcolor="#F8F7F1"><? echo $rows['a_name']; ?></td> </tr> <tr> <td bgcolor="#F8F7F1"><strong>Email</strong></td> <td bgcolor="#F8F7F1">:</td> <td bgcolor="#F8F7F1"><? echo $rows['a_email']; ?></td> </tr> <tr> <td bgcolor="#F8F7F1"><strong>Answer</strong></td> <td bgcolor="#F8F7F1">:</td> <td bgcolor="#F8F7F1"><? echo $rows['a_answer']; ?></td> </tr> <tr> <td bgcolor="#F8F7F1"><strong>Date/Time</strong></td> <td bgcolor="#F8F7F1">:</td> <td bgcolor="#F8F7F1"><? echo $rows['a_datetime']; ?></td> </tr> </table></td> </tr> </table><br> <? } $sql3="SELECT view FROM $tbl_name WHERE id='$id'"; $result3=mysql_query($sql3); $rows=mysql_fetch_array($result3); $view=$rows['view']; // if have no counter value set counter = 1 if(empty($view)){ $view=1; $sql4="INSERT INTO $tbl_name(view) VALUES('$view') WHERE id='$id'"; $result4=mysql_query($sql4); } // count more value $addview=$view+1; $sql5="update $tbl_name set view='$addview' WHERE id='$id'"; $result5=mysql_query($sql5); mysql_close(); ?> <BR> <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <form name="form1" method="post" action="add_answer.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td width="18%"><strong>Name</strong></td> <td width="3%">:</td> <td width="79%"><input name="a_name" type="text" id="a_name" size="45"></td> </tr> <tr> <td><strong>Email</strong></td> <td>:</td> <td><input name="a_email" type="text" id="a_email" size="45"></td> </tr> <tr> <td valign="top"><strong>Answer</strong></td> <td valign="top">:</td> <td><textarea name="a_answer" cols="45" rows="3" id="a_answer"></textarea></td> </tr> <tr> <td> </td> <td><input name="id" type="hidden" value="<? echo $id; ?>"></td> <td><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td> </tr> </table> </td> </form> </tr> </table> what i dont get is why the other pages i have created work. and this one dosnt want to? Link to comment https://forums.phpfreaks.com/topic/113423-solved-gots-me-tangled/ Share on other sites More sharing options...
runnerjp Posted July 6, 2008 Share Posted July 6, 2008 ok try 2 things... 1st add or die(mysql_error()); to them! next try echoing your query to see what it shows... i do this as sometimes i find spelling misshaps... also if you are calling the same db from all your pages then adding <?php $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="Forum"; // Database name $tbl_name="forum_question"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); ?> to all your pages is a waste of time and can be insecure... just add it to i single file called connect.php and then just call from that... it will limit your errors... Link to comment https://forums.phpfreaks.com/topic/113423-solved-gots-me-tangled/#findComment-582773 Share on other sites More sharing options...
Barand Posted July 6, 2008 Share Posted July 6, 2008 Check you dbname is correct. Also, some systems are case-sensitive. Link to comment https://forums.phpfreaks.com/topic/113423-solved-gots-me-tangled/#findComment-582780 Share on other sites More sharing options...
jkewlo Posted July 6, 2008 Author Share Posted July 6, 2008 i have done what you said. still says cannot select DB its the only page i am having this problem with. i have checked and checked and checked over this and cannot figure it out. im not new to php at all and i cant figure out why this has got me stumped Link to comment https://forums.phpfreaks.com/topic/113423-solved-gots-me-tangled/#findComment-582963 Share on other sites More sharing options...
jkewlo Posted July 6, 2008 Author Share Posted July 6, 2008 Ok Ok your going to laugh. the page i was working on was saved to my external hard drive and i have been saving to there lol and not my www folder ugh i feel retarted Link to comment https://forums.phpfreaks.com/topic/113423-solved-gots-me-tangled/#findComment-582971 Share on other sites More sharing options...
DarkWater Posted July 6, 2008 Share Posted July 6, 2008 Ah, I've done that a time or two. I went crazy over it. I was like "WHY IS THIS NOT WORKING?!?!". =P Glad you sorted that out though. Link to comment https://forums.phpfreaks.com/topic/113423-solved-gots-me-tangled/#findComment-583007 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.