Hi all, I'm trying yo do the following - Lets say my database is called 'A', in A i have a table called 'B', in b i have a column called 'C' that records a number count everytime the page loads (visits), I'm trying to figure out how to add up the total count in 'C' and display the result using PHP? The database connection is ok, I just need help with writing a statement and then display the results, Any help would be great, I'm not a coder so don't know where to start! Best Regards
Hi, Thank you, will give it a try, I take it I also need to echo the result using PHP as well? Best Regards
Look up some mysql tutorials and work through them. They'll give you a grounding in how it all ties together.
Oh it looks like you're quite far from having a working code. Like @sarahk said, your best bet is to get a tutorial on querying a mysql database with php. Or hire a developer to assist. If u consider the second option, I'm available.... If you already setup your project, I could also help take a quick look and make edits
Hi, Thank you all for your advice and offer, will take a look online to see if I can figure it out by way of tutorials, if I get stuck will post the code for others to see! Best Regards
Hi all, I have a question, Is this code for automatically connecting to a database? <?php if(!defined('IN_SCRIPT')) die(""); ?> PHP: I have a database table called 'directory_listings', within this table is a column called 'visits' that has a numerical value, I'm trying to add up all the values in 'visits' and display them as a total number but first wondered if above code is the database connection. Best Regards
No, this code has nothing to do with connecting to a database. It just checks if the IN_SCRIPT constant is declared. If it's not, php should terminate execution by dying =D It is usually used to prevent direct access to some php files from the URL. Php files that should only be called internally... but there are many other use cases
Hi, Ok, that explains a lot, i can run a query in the database to get the results i'm looking for and it gives me this code, Just need to figure out how to do on the site, so i now know that i have to create a database connection (many thanks). $sql = "SELECT SUM(visits) FROM directory_listings"; PHP: Best Regards
You should lookup tutorials on Database connections. Search for "How to connect to a mysql database with php PDO". Basically, your code should look like this: <?php $servername = "localhost";//usually localhost $username = "username";//your database user $password = "password";//the password of your database user try { $conn = new PDO("mysql:host=$servername;dbname=nameOfDB", $username, $password); echo "Connected successfully"; $sql = "SELECT SUM(visits) FROM directory_listings"; $query = $conn->prepare($sql); $query->execute(); } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } ?> PHP: Don't forget to close your Database connection otherwise the connection stays open and your hosting company will hate you Please verify the codes cause it's been long I wrote such. I use Frameworks that abstract the connection parts Edit Replace nameOfDB with the name of your DB
Hi, Thank you for taking the time to write your code, I’ve just sorted it out! More trail and error but it’s working. again, thank you! Best Regards
Let me help you.... You need to LEARN PHP if you plan to write PHP. All this Copy & Paste and asking question after question will get you NO WHERE. In fact, it will take you even more time!!!!!! And your username is "phplduser" haha..
Start by learning how to code. If you can read and write, you can code. Coding is nothing more than writing sentences that your computer can understand.