ginerjm Posted April 2, 2020 Share Posted April 2, 2020 (edited) I have no idea why your dbname is not getting passed. Did you try echoing it out from inside the function? Where is this other error? What is line 15? AND WHY DO YOU KEEP EDITING MY CODE? Do NOT include the db name in anything but the call to the connect!! Actually change the function header to this: function PDOConnect($l_dbname=NULL, $l_msg=null, $l_options=null) And get RID of the database name you supplied lower down. Tsk, tsk, tsk. Edited April 2, 2020 by ginerjm Quote Link to comment Share on other sites More sharing options...
larry29936 Posted April 2, 2020 Author Share Posted April 2, 2020 Done. Further question re: error in index.php. PHP Syntax Check: Parse error: syntax error, unexpected 'catch' (T_CATCH), expecting end of file in your code on line 26 catch(PDOException $e) Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 2, 2020 Share Posted April 2, 2020 Where's the code??????? Quote Link to comment Share on other sites More sharing options...
larry29936 Posted April 2, 2020 Author Share Posted April 2, 2020 // index.php <?php session_start(); // start of script every time. // setup a path for all of your canned php scripts $php_scripts = '/home/foxclone/php/'; // a folder above the web accessible tree // load the pdo connection module require $php_scripts . 'PDO_Connection_Select.php'; require $php_scripts . 'GetUserIpAddr.php'; //******************************* // Begin the script here $ip = GetUserIpAddr(); if (!$pdo = PDOConnect("foxclone_data")) <---- was missing closing quote per php syntax checker { echo "Failed to connect to database" ; <----- was missing ; per php syntax checker // exit? } else { $sql = "INSERT INTO `LOGIN` (`id`, `when_login`, `ip_address`) VALUES (NULL, current_timestamp(), '$ip'); <-- syntax copied from phpmyadmin // use exec() because no results are returned $pdo->exec($sql); echo "New record created successfully"; } catch(PDOException $e) <----- current error { echo "Query failed to run properly: $sql <br><br>" . $e->getMessage(); } exit(); ?> Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 2, 2020 Share Posted April 2, 2020 So you have solved the error? What now? And what is the catch for? Seems like an orphan Quote Link to comment Share on other sites More sharing options...
larry29936 Posted April 2, 2020 Author Share Posted April 2, 2020 The catch was copied from your second example near the bottom of page 1. I think it should be like this: //******************************* // Begin the script here $ip = GetUserIpAddr(); if (!$pdo = PDOConnect("foxclone_data")) { echo "Failed to connect to database" ; // exit? } elseif ($pdo = PDOConnect("foxclone_data")) { $sql = "INSERT INTO `LOGIN` (`id`, `when_login`, `ip_address`) VALUES (NULL, current_timestamp(), '$ip'); // use exec() because no results are returned $pdo->exec($sql); echo "New record created successfully"; } //catch(PDOException $e) else { echo "Query failed to run properly: $sql <br><br>" . $e->getMessage(); } exit(); ?> Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 3, 2020 Share Posted April 3, 2020 I don't actually use try/catch in my code so it must have been left over from something I copied from you and edited. Actually - the catch is commented out so it should not be a problem. Do we still have a problem? Tomorrow. Quote Link to comment Share on other sites More sharing options...
larry29936 Posted April 3, 2020 Author Share Posted April 3, 2020 The code runs fine locally but fails to pass the db name to PDO_Connection_Select when run on the webhost server. Waiting for feedback from the webhost. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 3, 2020 Share Posted April 3, 2020 So - add some echos to check what is going on. Or you could post the part of the current script that is making that connection call as well as the entire module that contains the connection code so that WE can see what you are seeing. That would be so very very helpful..... Quote Link to comment Share on other sites More sharing options...
larry29936 Posted April 3, 2020 Author Share Posted April 3, 2020 Here's a link to a screenshot of the error when run on the host: [/url]. Here's the code I'm running that works locally: Top of index.php: <?php session_start(); // start of script every time. // setup a path for all of your canned php scripts $php_scripts = '/home/foxclone/php/'; // a folder above the web accessible tree // load the pdo connection module require $php_scripts . 'PDO_Connection_Select.php'; require $php_scripts . 'GetUserIpAddr.php'; //******************************* // Begin the script here $ip = GetUserIpAddr(); if (!$pdo = PDOConnect("foxclone_data")): { echo "Failed to connect to database" ; // exit? } else: { echo "Connected to database"; $sql = "INSERT INTO `Logins` (`ip_address`) VALUES ('$ip')"; // use exec() because no results are returned $pdo->exec($sql); } endif; //exit(); ?> <!DOCTYPE html> PDO_Connection_Select.php: <?php function PDOConnect($l_dbname='', $l_msg=null, $l_options=null) { if ($l_options == null) { // set my default options $l_options = array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_FOUND_ROWS => true, PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC); } if ($l_dbname == null) $host="mysql:host=localhost;charset=utf8"; else $host="mysql:host=localhost;dbname=$l_dbname;charset=utf8"; $uid = "xxxxx"; $pswd = "yyyyyy"; try { $mysql = new PDO($host, $uid, $pswd, $l_options); } catch (PDOException $e) { if (strtoupper($l_msg) == "SHOWMSG") echo "Fatal Error<br>Failed to connect to mysql via PDO. PDO Error msg is:<br>".$e->getMessage(); else echo "Fatal Error<br>Possible bad dbname?<br>Failed to connect to mysql via PDO. Sensitive error msg may be viewed with additional parm to call to PDOConnect(dbname,'showmsg')"; return false; } if (!$mysql) return false; else // all worked - return handle to pdo connection. return $mysql; } ?> The structure is the same both locally and on the web host. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 3, 2020 Share Posted April 3, 2020 (edited) Why is your pdoconnect header not what I gave you??? I don't know what the mention of "structure" has to do with this topic. Nor the photograph. You're making it very difficult to help you. The error message obviously comes from the connect logic so that is where the problem lies. As for the main script - when you get a false result from the connect call why are you not exiting??? Add an echo to the pdoconnect logic to display the input dbname just to make sure you are actually executing this set of code. And - I assume that you are supplying the uid and pswd in the connect code and only left it as xxx and yyy for me. Edited April 3, 2020 by ginerjm Quote Link to comment Share on other sites More sharing options...
larry29936 Posted April 3, 2020 Author Share Posted April 3, 2020 I verified that the database name is being passed correctly by using an echo in PDO_Connection_Select.php. uid and pswd are the same both locally and on the host server. The error is coming from the catch in PDO_Connection_Select.php, not index.php Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 3, 2020 Share Posted April 3, 2020 And as I sadi last time the connect logic is failing you. Did you follow the instructions and have the module give you the full error message??? Quote Link to comment Share on other sites More sharing options...
larry29936 Posted April 3, 2020 Author Share Posted April 3, 2020 (edited) Got it fixed. While I can connect to the database thru phpmyadmin using a uid of larry locally, to connect from PDO_Connect_Select I needed to use foxclone_larry. Thanks for all your help and putting up with a newbie. Edited April 3, 2020 by larry29936 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 3, 2020 Share Posted April 3, 2020 So the problem was with the credentials? Did you use the SHOWMSG argument to get the true error message? Quote Link to comment Share on other sites More sharing options...
larry29936 Posted April 3, 2020 Author Share Posted April 3, 2020 Yes, I did. It told me about the invalid credentials. Quote Link to comment Share on other sites More sharing options...
larry29936 Posted April 3, 2020 Author Share Posted April 3, 2020 (edited) Now I need to figure out how to update my download table if they download a file. Hold on, I can do it all in the download table. It has the same fields as the login table plus a download name field. If I make that field to allow null, I can use it for logins and downloads. Need to modify the call in the index.php Edited April 3, 2020 by larry29936 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 3, 2020 Share Posted April 3, 2020 The connection module is just THAT. It is not a query module. It is not an update module. It is simply your permanent connection module. You use it when you need to establish a connection and you keep it in play all during your script, if the script needs to communicate to the db. That is it. It is the one and only place you make a connection and that means you only have one place to maintain should there be any changes in your credentials or your database tool. Quote Link to comment Share on other sites More sharing options...
larry29936 Posted April 3, 2020 Author Share Posted April 3, 2020 I'm having a problem with PDO. At the top of my index.php, I have the following: <?php session_start(); // start of script every time. // setup a path for all of your canned php scripts $php_scripts = '/home/foxclone/php/'; // a folder above the web accessible tree // load the pdo connection module require $php_scripts . 'PDO_Connection_Select.php'; require $php_scripts . 'GetUserIpAddr.php'; //******************************* // Begin the script here $ip = GetUserIpAddr(); if (!$pdo = PDOConnect("foxclone_data")): { echo "Failed to connect to database" ; exit; } else: { $sql = "INSERT INTO Downloads (ip_address,filename) VALUES ('$ip','$down')"; // use exec() because no results are returned $pdo->exec($sql); } endif; //exit(); ?> Further into index.php I have a download section that I want to record in the same table: <?php $files = glob('download/*.iso'); $file = $files[count($files) -1]; $filename = basename($file); $md5file = md5_file($file); ?> <div class="container"> <div class="divL"> <h3>Get the "<?php echo "{$filename}";?>" file (approx. 600MB)</h3> <center> <a href="<?php echo "/{$file}";?>"><img src="images/button_get-the-app.png" alt=""></a> </center> <?php $down=$filename $sql = "INSERT INTO Downloads (ip_address,filename) VALUES ('$ip','$down')"; <-- parse error here $pdo->exec($sql); ?> I'm getting the following error: Parse error: syntax error, unexpected '$sql' (T_VARIABLE) in /home/foxclone/test.foxclone.com/index.php but don't understand why. Do I not need the $sql line? Thanks in advance, Larry Quote Link to comment Share on other sites More sharing options...
Barand Posted April 3, 2020 Share Posted April 3, 2020 Try ending the previous line with a ";" Quote Link to comment Share on other sites More sharing options...
larry29936 Posted April 3, 2020 Author Share Posted April 3, 2020 Thanks for catching that. I looked rights past it for a couple of hours. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 3, 2020 Share Posted April 3, 2020 YOu have to start paying attention to the error messages and line numbers. The answer is always pretty close to what it says. Quote Link to comment 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.