Skatecrazy1 Posted October 21, 2015 Share Posted October 21, 2015 So I'm just starting this little side project to mess around with the idea of a torrent tracker site. I'm a little rusty with my hand-typed scripts so I'm sure I'm looking right at something here and missing it. Just trying to display table data from 'users'. Worked just fine while I ran it on the test db, but after exporting it from the test db and importing it into the actual db I'm using I get a boolean returned for my $result, which causes my num_rows to fail and throw an error. Thanks in advance for any help. <?php class Sql { public function __construct(){ global $host, $user, $pass, $db, $conn; $host = "localhost"; $user = "admin"; $pass = ""; $db = "blog"; $conn = mysql_connect($host, $user, $pass); mysql_select_db($db); } } class Connect extends Sql { //connect function and verification public function init(){ //this function connects to the database based on //global variables defined in the "Sql" class // globals global $user, $pass, $conn, $host, $db; if(!$conn){ print "Connection failed."; } else { print "<div id = \"connect\">"; print "SQL Connection Successful"; print "</div>"; } } } class Display extends Sql { //this class selects and displays data from a given table public function user_display(){ // globals global $conn; $sql = "SELECT * FROM users"; $result = mysql_query($sql, $conn); if(!$result){ echo $result; } $num = mysql_num_rows($result); echo " <table padding=\"3\" border=\"1\"> <tr> <td> <strong>Name</strong> </td> <td> <strong>Email</strong> </td> <td> <strong>Phone</strong> </td> </tr>"; //loop through the data for($i = 0; $i < $num; $i++){ $data = mysql_fetch_array($result); echo " <tr> <td> ".$data['name']." </td> <td> ".$data['email']." </td> <td> ".$data['phone']." </td> </tr>"; } mysql_close($conn); } } ?> Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 21, 2015 Share Posted October 21, 2015 (edited) You are using obsolete Mysql code that will not work at all in the latest version of Php. You need to use PDO with prepared statements. It has been obsolete for over ten years now. Edited October 21, 2015 by benanamen Quote Link to comment Share on other sites More sharing options...
Skatecrazy1 Posted October 21, 2015 Author Share Posted October 21, 2015 Can you actually elaborate? What parts of my code are obsolete? Try being helpful instead of just confirming what I already know. For what it's worth the code was working perfectly fine until I imported everything into the new db. Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 21, 2015 Share Posted October 21, 2015 (edited) Try being helpful instead of just confirming what I already know. So you already knew that you are using code that has been obsolete for over ten years? If you already know that why are you using it? Edited October 21, 2015 by benanamen Quote Link to comment Share on other sites More sharing options...
Skatecrazy1 Posted October 21, 2015 Author Share Posted October 21, 2015 Thanks for making my point for me. I'm using it because I haven't written any code in a few years. Anyone on this forum who actually wants to look at my code ? Or has there been a large influx of pompous twits in the couple years I've been gone and no one actually helps out here anymore? Quote Link to comment Share on other sites More sharing options...
Skatecrazy1 Posted October 21, 2015 Author Share Posted October 21, 2015 Guess not. Was using the older code because that's what I remember off the top of my head. I'll seek help elsewhere, then. So long phpfreaks, you used to be cool. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 21, 2015 Share Posted October 21, 2015 (edited) benanamen is referring to the use of mysql_() functions. These functions have been deprecated (no longer supported) for a while and is to be removed completely in the upcoming release of PHP7. You have two options you can either use PDO or MySQLi to connect to your mysql database. Now the other problem with your code is not very good. You are not using OOP correctly. Globals has absolutely no use in OOP (or in procedural code to be honest). If a method requires external value(s) in order to perform the task you should be passing those value(s) as arguments, not defining them as global If a object needs to use a value within another method of the same object then you should be saving it as a property. You will use $this to access the objects properties in the other methods of the object. Edited October 21, 2015 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Skatecrazy1 Posted October 21, 2015 Author Share Posted October 21, 2015 Is mysql improved expected to be supported for any length of time? It's closer to what I'm used to. The OOP is terrible because I used to write scripts back in the PHP2-3 days and never used objects with any frequency. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 21, 2015 Share Posted October 21, 2015 Is mysql improved expected to be supported for any length of time? It's closer to what I'm used to. There is not set support length. MySQL improved may look similar to mysql_functions but it is not. Do not come under the illusion all you need to do is add an 'i' after mysql in the function names. There are differences, for example some mysql_() functions took the connection resource as an optional argument. However the mysqli equivalent functions now require it and is usually the first argument. You should study the mysqli documentation for the differences. The OOP is terrible because I used to write scripts back in the PHP2-3 days and never used objects with any frequency. Then maybe stick with procedural programming if you are not used to OOP. Quote Link to comment Share on other sites More sharing options...
Skatecrazy1 Posted October 21, 2015 Author Share Posted October 21, 2015 (edited) I'm used to OOP, but haven't used it much with PHP so the syntax and declarations are a bit hazy. Used it more often with C++ and Java, and whatever bastardized version of C++ arduino runs off of. I have used mysqli in the past so I do know that there are a few things that are different, how it takes arguments, etc. I am trying to get a basic grasp back on OOP syntax and database querying with php after being gone from it for years working in an industrial occupation. Currently rewriting my code with more updated syntax and function libraries. Having issues declaring protected or public variables as they seem to go undefined when it comes time to call them. Now bear with me, declaring your own constructors wasn't really necessary back when I was at it, so I'm still trying to wrap my head around the concept of it. I seem to be failing pretty hard. This code throws me an "UNEXPECTED T_VARIABLE" on line 6. <?php class Sql { protected $host, $user, $pass, $db; $host = "localhost"; $user = "admin"; $pass = ""; $db = "blog"; public function __construct($host, $user, $pass, $db){ $this->host = $host; $this->user = $user; $this->pass = $pass; $this->db = $db; $conn = mysqli_connect($host, $user, $pass); mysqli_select_db($db); } public function userData() { $sql = "SELECT * FROM users"; $result = mysqli_query($conn, $sql); $num = mysqli_num_rows($result); for($i = 0; $i < $num; $i++){ $data = mysqli_fetch_array($result); echo $data['name']; echo " | "; echo $data['email']; echo " | "; echo $data ['phone']; echo "<br />"; } } } ?> Edited October 21, 2015 by Skatecrazy1 Quote Link to comment Share on other sites More sharing options...
Stefany93 Posted October 24, 2015 Share Posted October 24, 2015 If you keep writing queries with mysql_query, everything will be bad, lol. Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 24, 2015 Share Posted October 24, 2015 In a perfect world every server will have Php ver. >=7. Its really the only way we can stop these people. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 24, 2015 Share Posted October 24, 2015 Pdo does not have to be used as OOP. I use it strictly in procedural mode and find it very easy to use. $q = "select * from (table) where key1= :mykeyvalue1"; // note use of : in the key argument here $qst = $pdo_conn->prepare($q); // $pdo_conn is created in a separate piece of connect logic that one can easily write in an included module if (!qst) echo "error doing prepare"; $qst->execute(array('mykeyvalue1'=>$key1)); while ($row = $qst->fetch(PDO::FETCH_ASSOC)) { (process the returned rows) } Hope this makes it clearer. Quote Link to comment Share on other sites More sharing options...
hansford Posted October 25, 2015 Share Posted October 25, 2015 Can you actually elaborate? What parts of my code are obsolete? All of it is obsolete unless you are just practicing or testing code fragments. Other than the manual https://secure.php.net/manual/en/index.php Another good reference is: http://www.phptherightway.com/ 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.