Jump to content

Bad mysql_query()


Skatecrazy1

Recommended Posts

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);
	}
	
	
}
	?>		

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by Ch0cu3r
Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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 by Skatecrazy1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.