fyurien Posted March 10, 2017 Share Posted March 10, 2017 Hey all, First I know basically 0 about programming, so forgive the obvious mistakes. I have a simple (at least I thought script) that worked in php 5, but no go in php 7. I tried upgrading it to php 7, but can't get it to work. Can someone please help me. <?php function sentence() { global $post; mysqli_connect("localhost", "user", "pass", "database"); mysqli_query('set names "utf8"'); $query="SELECT name FROM seo ORDER BY RAND() LIMIT 10;"; $result=mysqli_query($query); $num=mysqli_num_rows($result); //Begin find and replace while($row = mysqli_fetch_array($result)) { //this is the string to look into $myString = $row['name']; //this is the thing to look for $patterns = array(); $patterns[0] = '/1111/'; //this is what you replace it with $result1 = $post->post_title; //this is the echo echo preg_replace_callback($patterns, $result1, $myString); } mysqli_close(); } ?> Thank you in advance Quote Link to comment Share on other sites More sharing options...
benanamen Posted March 10, 2017 Share Posted March 10, 2017 (edited) You cannot just throw an i onto mysql_* and expect it to magically be mysqli. Start with a basic Mysqli tutorial or better yet, use PDO. https://phpdelusions.net/pdo Edited March 10, 2017 by benanamen Quote Link to comment Share on other sites More sharing options...
fyurien Posted March 10, 2017 Author Share Posted March 10, 2017 Here's what I was able to find... https://www.w3schools.com/php/func_mysqli_query.asp Am on the right track? Quote Link to comment Share on other sites More sharing options...
benanamen Posted March 10, 2017 Share Posted March 10, 2017 Yeah, but you might as well start using PDO now. Study the link I posted. Quote Link to comment Share on other sites More sharing options...
fyurien Posted March 10, 2017 Author Share Posted March 10, 2017 Ok cool. So I can see how to make a connection using PDO... but no clue how to replicate the rest of the function. I have no programming skills unfortunately. Would you be able to help me make sense of it? Quote Link to comment Share on other sites More sharing options...
benanamen Posted March 10, 2017 Share Posted March 10, 2017 (edited) Forget about making a function, just learn how to use PDO. Start with a basic select. * In practice, you will not be using a variable for the table name. This is a really basic example. <?php $hostdb = 'localhost'; $dbname = 'your_DB'; $username = 'root'; $password = ''; $table = 'YOURTABLE'; $pdo = new PDO("mysql:host=$hostdb;dbname=$dbname", $username, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "SELECT * FROM $table"; $stmt = $pdo->prepare($sql); $stmt->execute(); $result = $stmt->fetchAll(); echo '<pre>'; print_r($result); echo '</pre>'; ?> Edited March 10, 2017 by benanamen Quote Link to comment Share on other sites More sharing options...
fyurien Posted March 10, 2017 Author Share Posted March 10, 2017 Holy wow, that's way more complicated than I thought it would be. I will pull a Captain America here an say I understand most of those references. As an aside I stuck that into my program and it seemed to work. Generated a bunch of stuff. I think the entire table actually Quote Link to comment Share on other sites More sharing options...
fyurien Posted March 10, 2017 Author Share Posted March 10, 2017 (edited) I was able to modify your code a bit and it seemed to work... <?php $hostdb = 'localhost'; $dbname = 'your_DB'; $username = 'root'; $password = ''; $table = 'YOURTABLE'; $pdo = new PDO("mysql:host=$hostdb;dbname=$dbname", $username, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //modified this line below// $sql = "SELECT name FROM $table ORDER BY RAND() LIMIT 2;"; $stmt = $pdo->prepare($sql); $stmt->execute(); $result = $stmt->fetchAll(); echo '<pre>'; print_r($result); echo '</pre>'; ?> Edited March 10, 2017 by fyurien Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted March 10, 2017 Share Posted March 10, 2017 Holy wow, that's way more complicated than I thought it would be. I will pull a Captain America here an say I understand most of those references. As an aside I stuck that into my program and it seemed to work. Generated a bunch of stuff. I think the entire table actually Really, it isn't that complicated when you break it down. Define a bunch of variables to whatever you want. $hostdb = 'localhost'; $dbname = 'your_DB'; $username = 'root'; $password = ''; $table = 'YOURTABLE'; Okay, at first these can be a little intimidating. First one just sets up a connection to your database using the host name (likely localhost), and your database name and credentials. The second line tells it how to deal with errors, and you "can" do without, but probably should leave it in. $pdo = new PDO("mysql:host=$hostdb;dbname=$dbname", $username, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); Define a query, prepare it in MySQL PDO, and execute it. Note that I slightly changed your query by adding a WHERE query. The strange part is the silly :id at the end. This just tells PDO to replace it with the value in the array with the same index. Instead of :id, you can also use ?, and use a non-associated array. You can also use some crazy binding and whatnot, but don't worry about that for a while. Queries with one or two things to replace, use ?, otherwise, use the first method. Not only are your queries faster (but who cares), they are also much more secure as they are automatically escaped. $sql = "SELECT * FROM $table WHERE id=:id"; $stmt = $pdo->prepare($sql); $stmt->execute(['id']=>123); Get all your results. You could have used $stmt->fetch(), and it would have pulled only one (such as what you would have wanted to use if you used my example where I had the WHERE clause on the PK. $result = $stmt->fetchAll(); And print them. echo '<pre>'; print_r($result); echo '</pre>'; While there are many benefits of PDO, one of the greatest ones is it is so dang easy. 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.