PHP - Mysql_connect() Works On One Page, But Not Another.
I don't know how else to explain this. I have two php pages in the same domain, same folder, with exact same line to connect to mysql, and one works, and the other doesn't. I have no clue. This is the line
$con = mysql_connect("localhost", "myusername", "mypassword"); Similar TutorialsHi, I am a php newbie, who has a page that relies on some php scripts, and to which I am trying to add a login page written in php. I took the example from he http://www.howtodothings.com/computers-internet/how-to-make-a-login-system-for-your-website Basically it consists of adding: <? require("log.php"); ?> to the top of any page I want to protect, a log.php file which performs the actions of the form, linking to a mySQL database, and a login.php file which contains the form. I have the login working fine, but it breaks one of the PHP scripts on the page that is protected. It is an upload script, called Weaverbox, based on FancyUpload. The uploads which are handled by a file called upload.php, aren't happening. The progress shows that they are being uploaded, but nothing is uploaded, and there is no success message. As soon as I remove the code from the top of the page requiring log.php all works fine again. I think I may have to add some rules/extensions to resolve this conflict, but I don't know how to go about this. Would someone be able to help me get it sorted? Thanks Nick i think this has to do something with me dropping my table and trying to reinstall the blog but now i get "can't connect to mysql5.000webhost.com" so then i tried using localhost and i get... "can't connect to localhost using password YES" so i assume my password is right and for some reason it isnt connecting yet all the variables match up correct password, database,user i double and triple checked spelling When I try to connect to my database I am getting this error: Quote Warning: mysql_connect() [function.mysql-connect]: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\wamp\www\Envne\envne.com\mysqlAdmin\classes\Database.php on line 9 Warning: mysql_connect() [function.mysql-connect]: [2002] php_network_getaddresses: getaddrinfo failed: No such host is kn (trying to connect via tcp://root:3306) in C:\wamp\www\Envne\envne.com\mysqlAdmin\classes\Database.php on line 9 Warning: mysql_connect() [function.mysql-connect]: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\wamp\www\Envne\envne.com\mysqlAdmin\classes\Database.php on line 9 Here is the EXACT code on line 9: mysql_connect('root', '', 'localhost'); I have never gotten this error before... how do I fix it? I am trying to extend my knowledge from using basic mysql_connect etc. into something better. I am searching on Google, and looking up as much info as possible but find that I learn best with a brief overview that allows me to channel down my searches. (1) I know PDO v mysql_connect v mysqli isn;t right - I think PDO is a class, mysql_connect is a command and mysqli is something else. Am I on the right track? (2) What is the best solution? I am using a full OOP coding style and want to learn the best - I am a novice at this but would consider myself an 8/10 programmer at other parts of PHP. An very brief overview to get me started and channel my Google searches would be much appreciated. - I have heard PDO is slower. - I don't need support for different databases (only MySQL). Thanks in advance. mysql_connect("$host", "$username", "$password")or die("cannot connect to server"); i create a connection and let say there is a problem with the server so that the connection can't established my question is how can i avoid the default error to be displayed which is WARNING: mysql_connect() blablabla as it is annoying to see such error message. thanks in advance Hi, I currently have an HTML form that posts data (method:POST) to a PHP script. The PHP script then retrieves the data and displays it on screen so the user can do a final check. I then want to have a button, so that when pressed, the data gets sent to a MySQL database. What I have so far is along these lines: Code: [Select] <?php if(isset($_POST['var1'])) {$var1 = $_POST['var1'];} else {$var1 = "";} if(isset($_POST['var2'])) {$var2 = $_POST['var2'];} else {$var2 = "";} echo "<b>You have entered the following:</b><p></p>", "$var1 <br>","$var2<p></p>", "If this is correct, please press continue."; print "<html><body><button name=Button1 onclick="">Continue</button></body></html>"; and onclick I want the following to run: Code: [Select] $link = mysql_connect ('localhost', 'root'); if (!$link) { die('Could not connect: ' . mysql_error()); } mysql_select_db ('DB1', $link); mysql_query ("INSERT INTO table1 (field1, field2) VALUES ('$var1','$var2')"); mysql_close($link); How can I do this?! Is it neccesary to use another "submit" button that submits $var1 & $var2 to a different PHP script, which then runs the mysql_connect part? Or can I enclose the mysql_connect part in an IF statement that runs only if Button1 has been pressed (and I enclose the rest of the PHP script in an IF statement that runs only if Button1 has NOT been pressed) and get Button1 to reload the same PHP script? Either way, how do I define an onclick event inside echo? This prob a noobish question, but I seem to get errors if I try to put (single) quotes "" ' ' inside the quotes of the echo: echo " onclick"" "; Thanks! Quote Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'nyhungry_builder'@'localhost' (using password: YES) in /home3/nyhungry/public_html/musicmakerstore/storescript/connect_to_mysql.php on line 19 could not connect to mysql I am getting this error from the code below how come? Code: [Select] <! <?php // Place db host name. Sometimes "localhost" but // sometimes looks like this: >> ???mysql??.someserver.net $db_host = "localhost"; // Place the username for the MySQL database here $db_username = "nyhungry_builder"; // Place the password for the MySQL database here $db_pass = "blablabla"; // Place the name for the MySQL database here $db_name = "nyhungry_musicbeatmaker"; // Run the actual connection here mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql"); mysql_select_db("$db_name") or die ("no database"); ?> I am working on a cpanel kind of host and you know how the databases are setup like blablab_databasename etc.. What is going on here i have check the database name, the table, password and the host thousands of times and nothing.. Help Hi, this is more of a question than asking for help so if its in the wrong section could someone please move. my question is could you connect to a database by using an array that has been generated from getting contents from a file, for example... Code: [Select] $file_c = file_get_contents("example.txt"); $colms = explode(",",trim($file_c)); mysql_connect("$colms[0]","$colms[1]","$colms[2]") or die(mysql_error()); mysql_select_db("$colms[3]") or die(mysql_error()); if not how else would someone go about doing this. Hi, I get this error when I load the page, but after hitting ctrl f5 to refresh.. the error goes away and the page works perfectly. I have edited out my details. Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'user'@'localhost' (using password: YES) Access denied for user 'user'@'localhost' (using password: YES) I know everything is set right.. I just don't get it.. I am going crazy of this. Ive cleared my browser cache.. even tried it on my android phone and it does the same thing. Thanks, Deceevn I am using a script that gets connected to PHP table and returns the visitors country name from: Database MyProjectA Table: ip2country and inside the script it uses the mysql_connect() function and it works fine BUT the code used in other pages to get data from other tables does not work. I mean, I have a combo box for example, that populates data from: Database MyProjectA Table: mybookings But it doesn't populate as long as the code for the 'user's country script' is active. The moment I comment out the 'user's country script', the combo box works. Perhaps I am using 2 database pooling (or connection pooling) so what I did, I closed the previous connection after I get the visitor's country name but still the combo box has no values in it. Do you get the picture. Please read the question again. Thank you Hi, I am really plugging into how to write functions in PHP. But I was going to delve into a user management program and try to create it, but dont want to be the older version of what I was before if you like, where I just type in for the sake of typing in code so I thought I would question what their doing. But a peice of code, a very small snippet, this came up: @mysql_connect What does this actually mean with the @ sign infront of the mysql_connect function? Seen this a few times but just never appreciated what the at sign means, any help is wonderfully appreciated of course. Thanks, Jeremy Hi all I have been trying to do a query but I get Call to undefined function mysql_connect() What other way can I perform a query? Or do I need to get something enabled? Thank in advance Dave httpd-2.2.17-win32-x86-openssl-0.9.8o php-5.3.6-Win32-VC9-x86 mysql-5.5.11-win32 PHP code: $dbcnx = mysql_connect('localhost','root','admin'); Error message: No connection could be made because the target machine actively refused it. I used mysql.exe -u root -p to verify that mysql is working and using the right username and password. Any ideas how to fix it? Thanks, Richard I'm working on a pair of scripts that (among other tasks) download a file to the user's browser. It's not working right, and I'm having trouble figuring out why. The overall design is: the first script (I'll call it one.php) contains a form which displays a list of radio buttons representing actions that the site can perform. The user clicks a radio button, then a "submit" button. This loads two.php, which determines what action the the user selected, performs the action, and loads a page that contains another form. This form has a couple of "submit" buttons; one reloads one.php, and the other goes elsewhere. Everything works right except when the user selects the "download a file" option. In that case two.php downloads the file (and that part works perfectly), then -- nothing. The page defined in two.php never appears. The browser just sits displaying the page from one.php as it was when the submit button was clicked. I played with the code and found that if I comment out all of the download headers, so that the browser gets the raw contents of the downloaded file followed by an HTML page, the browser does just what I'd expect: it displays the file (represented as a stream of semi-binary garbage), followed by an HTML page. Something in the download headers is upsetting it... but I can't figure out what. Here is the code that sends the headers: Code: [Select] header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . basename($file) ); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file) ); if (ob_get_length() > 0) { ob_clean(); } flush(); readfile($file); flush(); Following that, the script includes a file that contains another block of PHP code, then the <!DOCTYPE> tag, with no intervening characters outside the PHP. (BTW, the PHP block contains nothing but comments!) Can anyone suggest what's going wrong here? why does this : $date = date('m d,Y', "$postdate");() work to format my date output on one page yet on the other I get the dec 31 1969 output for all my dates? Hi all, I am a bit of a noob when it comes to website building and php etc so please have patients with me I am learnig as I go. I have come across this problem before and cannot remember what I did to fix it but any way it is presenting its self to me again. I have this page: http://minecraftcons...a.com/index.php with an include file loading random images, however the same code on these pages: http://minecraftconsolecrafting.comxa.com/sections/build-navi.php http://minecraftconsolecrafting.comxa.com/sections/springhill-kingdom-01.php are not working. I have other websites that this method of coding is working fine on for example here is another of my websites: http://divinegardensandlandscapes.com/index.php here the include code for random image loader is working fine on all pages it appears on so what is happening when this fail takes place? Any help and or advice would be hugely appreciated, Thanks in advance. Please remeber I am a noob when answering. Thanks Edited by eGate-Network, 12 July 2014 - 10:55 PM. Please can soemone help me here? I am getting Undefined Index but my page still works?! here is the line it is erroring on: Code: [Select] $page = mysql_escape_string($_GET['page']); if($page){ $start = ($page - 1) * $limit; }else{ $start = 0; } We have a 10 year old system that has 100's of php files that all connect to a central database using a single shared php include file that does a mysql_connect before any other code executes
include_once("db_connect.php");
mysql_query(...);
We never used the returned connection id (link identifier) from the mysql_connect function call as it was always implied in every subsequent mysql_query statement etc.
Now we've moved some of our bigger tables to a separate server, so we have to update small areas of some files that need to connect to this other system and send the old existing query to the new server instead of to the main server and process the results
So, going back through hundreds of files and converting mysql_query($string) to mysql_query($string, $connection1) is not practical
Possible solution one: anytime I need to use the new server I do this
------------------------------------------------------------------------------
mysql_connect(NEW_SERVER_IP,"...",""");
mysql_query($string);
...
// get back to normal
mysql_connect(OLD_SERVER_IP,"...","");
Possible solution two: modify the master include file
------------------------------------------------------------------------------
//add a new line ABOVE the existing mysql_connect statement...
$new_server=mysql_connect(NEW_SERVER_IP,"...",""");
mysql_connect(OLD_SERVER_IP,"...","");
and then anytime I need to use the new database I have to go back and modify all of the mysql_query,mysql_affected_rows, etc to reference the $new_server link identifier
solution one seems much simpler, but all of the mysql_connect reconnections I assume will be extremely inefficient
solution two seems cleaner but if I miss one mysql_ statement that needs the new connection object as a reference, the code will completely break or worse yet it will produce unexpected results
Example: If I miss converting a mysql_errno() function that really needs to now be mysql_errno($new_server) it would cause major issues.
So, I'm wondering if others have faced this dilema of migrating single mysql_connect code to multi mysql_connect mode and if there is a better way of going about implementing it.
Thanks in advance for any help
Steve
I have a very weird problem. On my website i have a script that takes random pages and displays them on the homepage. It works without a problem on its own but when i have it included in the homepage using include('webmaster fav.php'); i get this error: Warning: Cannot use a scalar value as an array in /mnt/w0210/d28/s25/b02a8bb2/www/webmaster fav.php on line 18 and this error: Warning: array_unique() [function.array-unique]: The argument should be an array in /mnt/w0210/d28/s25/b02a8bb2/www/webmaster fav.php on line 20 and this error: Warning: array_unique() [function.array-unique]: The argument should be an array in /mnt/w0210/d28/s25/b02a8bb2/www/webmaster fav.php on line 21 and this error: Warning: Cannot use a scalar value as an array in /mnt/w0210/d28/s25/b02a8bb2/www/webmaster fav.php on line 23 I am kindof new to php but i think its a good script and it works without errors when it isn't included on the homepage it works. what could be the problem? heres the code: <?php $directory = "/mnt/w0210/d28/s25/b02a8bb2/www/data/"; //the list of pages i want to be random on the site $directory = (!strstr($directory,"*") || $directory =="./" ) ? $directory."*" : $directory; //Checks if the wildcard operator is present, and if not it adds it by default at the end; $files = glob($directory); //Yes, it was that easy to get all the files; $size=sizeof($files); for($i=0;$i<sizeof($files) ; $i++){ //Loop through the files and adds to array; $fp = fopen($files[$i],"r"); $contents[$i]=fgets($fp,999); fclose($fp); } for($x=0;$x<15;$x++){ $numb[$x]=rand(1, sizeof($files)); } $x=count($numb)-count(array_unique($numb)); $num = array_unique ($numb); for($q=0;$x<15;$x++){ $numb[$x]=rand(1, sizeof($files)); } //$imploded = implode(" ", $contents); //get rid of spaces //$newcontent=explode("~", $imploded); // sort into chucks so i can display the data. for($i=0;$i<15; $i++){ $number=$num[$i]; if($contents[$number]==""||$contents[$number]==" "||$contents[$number]==null){ } else{ echo "<li>"; $replacedcontent=str_replace(' ', '-',$contents[$number]); echo "<br/><a href='games/$replacedcontent'>"; $newrcontent=str_replace('-', ' ',$replacedcontent); echo "<img src='$newrcontent.jpg' border='2'></img>"; echo "<br/>$newrcontent</a></li>"; } } ?> i copied and pasted it from many sites examples so thats why some comments are weird... but basically it gets all the data files. reads the title and puts them in an array, then chooses some random ones and puts them in with their image so they can be displayed on the homepage. |